Browse Source

mybatis project change4

zhang 2 years ago
parent
commit
82b5234466

+ 0 - 1
mybatis_train/src/main/java/com/mapper/AccountMapper.java

@@ -7,7 +7,6 @@ import org.springframework.stereotype.Repository;
7
 
7
 
8
 import java.util.List;
8
 import java.util.List;
9
 
9
 
10
-@Mapper
11
 public interface AccountMapper {
10
 public interface AccountMapper {
12
     List<AccountEntity> findAll(Integer id);
11
     List<AccountEntity> findAll(Integer id);
13
     void updateOne(AccountEntity accountEntity);
12
     void updateOne(AccountEntity accountEntity);

+ 0 - 1
mybatis_train/src/main/resources/mybatisConfig.xml

@@ -25,7 +25,6 @@
25
     </environments>
25
     </environments>
26
 
26
 
27
     <mappers>
27
     <mappers>
28
-
29
         <mapper resource="mapper/AccountMapper.xml"/>
28
         <mapper resource="mapper/AccountMapper.xml"/>
30
 <!--        使用class方式引入-->
29
 <!--        使用class方式引入-->
31
 <!--        <mapper class="com.mapper.AccountMapper"></mapper>-->
30
 <!--        <mapper class="com.mapper.AccountMapper"></mapper>-->

+ 13 - 3
mybatis_train/src/test/java/Test1.java

@@ -1,3 +1,4 @@
1
+import com.mapper.AccountMapper;
1
 import com.pojo.entity.AccountEntity;
2
 import com.pojo.entity.AccountEntity;
2
 import org.apache.ibatis.io.Resources;
3
 import org.apache.ibatis.io.Resources;
3
 import org.apache.ibatis.session.SqlSession;
4
 import org.apache.ibatis.session.SqlSession;
@@ -7,6 +8,7 @@ import org.junit.Test;
7
 
8
 
8
 import java.io.IOException;
9
 import java.io.IOException;
9
 import java.io.InputStream;
10
 import java.io.InputStream;
11
+import java.util.List;
10
 
12
 
11
 public class Test1 {
13
 public class Test1 {
12
 
14
 
@@ -24,13 +26,21 @@ public class Test1 {
24
 //        sqlSession.insert("com.mapper.AccountMapper.insertOne",accountEntity);
26
 //        sqlSession.insert("com.mapper.AccountMapper.insertOne",accountEntity);
25
         AccountEntity updateEntity = new AccountEntity();
27
         AccountEntity updateEntity = new AccountEntity();
26
         updateEntity.setId(9);
28
         updateEntity.setId(9);
27
-        updateEntity.setUser_name("我是");
29
+        updateEntity.setUser_name("我是12");
28
         updateEntity.setBalance(100.0);
30
         updateEntity.setBalance(100.0);
29
          // namespace + functionName
31
          // namespace + functionName
30
-        sqlSession.update("accountMapper.updateOne",updateEntity);
32
+        sqlSession.update("com.mapper.AccountMapper.updateOne",updateEntity);
31
         // mybatis 如果执行了更新操作需要提交事务
33
         // mybatis 如果执行了更新操作需要提交事务
32
 //        sqlSession.commit();
34
 //        sqlSession.commit();
33
-        sqlSession.rollback();
34
         sqlSession.close();
35
         sqlSession.close();
35
     }
36
     }
37
+    // 使用代理的模式
38
+    @Test
39
+    public void useProxy() throws IOException {
40
+        InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
41
+        SqlSessionFactory sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder().build(resourceAsStream);
42
+        SqlSession sqlSession = sqlSessionFactoryBuilder.openSession(true);
43
+        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
44
+        List<AccountEntity> all = mapper.findAll(9);
45
+    }
36
 }
46
 }

+ 1 - 0
mybatis_train/src/test/java/Test2.java

@@ -10,6 +10,7 @@ public class Test2 {
10
 //        ClassPathXmlApplicationContext classPathXmlApplicationContext =
10
 //        ClassPathXmlApplicationContext classPathXmlApplicationContext =
11
 //                new ClassPathXmlApplicationContext("SpringConfig.xml");
11
 //                new ClassPathXmlApplicationContext("SpringConfig.xml");
12
         AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
12
         AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
13
+        annotationConfigApplicationContext.scan("com");
13
         annotationConfigApplicationContext.refresh();
14
         annotationConfigApplicationContext.refresh();
14
         AccountServiceImpl bean = annotationConfigApplicationContext.getBean(AccountServiceImpl.class);
15
         AccountServiceImpl bean = annotationConfigApplicationContext.getBean(AccountServiceImpl.class);
15
     }
16
     }