瀏覽代碼

mybatis project change4

zhang 2 年之前
父節點
當前提交
82b5234466

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

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

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

@@ -25,7 +25,6 @@
25 25
     </environments>
26 26
 
27 27
     <mappers>
28
-
29 28
         <mapper resource="mapper/AccountMapper.xml"/>
30 29
 <!--        使用class方式引入-->
31 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 2
 import com.pojo.entity.AccountEntity;
2 3
 import org.apache.ibatis.io.Resources;
3 4
 import org.apache.ibatis.session.SqlSession;
@@ -7,6 +8,7 @@ import org.junit.Test;
7 8
 
8 9
 import java.io.IOException;
9 10
 import java.io.InputStream;
11
+import java.util.List;
10 12
 
11 13
 public class Test1 {
12 14
 
@@ -24,13 +26,21 @@ public class Test1 {
24 26
 //        sqlSession.insert("com.mapper.AccountMapper.insertOne",accountEntity);
25 27
         AccountEntity updateEntity = new AccountEntity();
26 28
         updateEntity.setId(9);
27
-        updateEntity.setUser_name("我是");
29
+        updateEntity.setUser_name("我是12");
28 30
         updateEntity.setBalance(100.0);
29 31
          // namespace + functionName
30
-        sqlSession.update("accountMapper.updateOne",updateEntity);
32
+        sqlSession.update("com.mapper.AccountMapper.updateOne",updateEntity);
31 33
         // mybatis 如果执行了更新操作需要提交事务
32 34
 //        sqlSession.commit();
33
-        sqlSession.rollback();
34 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 10
 //        ClassPathXmlApplicationContext classPathXmlApplicationContext =
11 11
 //                new ClassPathXmlApplicationContext("SpringConfig.xml");
12 12
         AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
13
+        annotationConfigApplicationContext.scan("com");
13 14
         annotationConfigApplicationContext.refresh();
14 15
         AccountServiceImpl bean = annotationConfigApplicationContext.getBean(AccountServiceImpl.class);
15 16
     }