Browse Source

mybatis change 5

zhangxiaoyu 2 years ago
parent
commit
d568530773

+ 3 - 1
mybatis_train/src/main/java/com/pojo/entity/UserEntity.java

@@ -2,8 +2,10 @@ package com.pojo.entity;
2
 
2
 
3
 import lombok.Data;
3
 import lombok.Data;
4
 
4
 
5
+import java.io.Serializable;
6
+
5
 @Data
7
 @Data
6
-public class UserEntity {
8
+public class UserEntity implements Serializable {
7
     private Integer id;
9
     private Integer id;
8
     private String name;
10
     private String name;
9
     private Integer age;
11
     private Integer age;

+ 1 - 0
mybatis_train/src/main/resources/com/mapper/UserMapper.xml

@@ -4,6 +4,7 @@
4
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5
 <mapper namespace="com.mapper.UserMapper">
5
 <mapper namespace="com.mapper.UserMapper">
6
 <!--    sql语句抽取-->
6
 <!--    sql语句抽取-->
7
+    <cache />
7
     <sql id="all">select * from user</sql>
8
     <sql id="all">select * from user</sql>
8
 <select id="findUserListByCondition" resultType="user"
9
 <select id="findUserListByCondition" resultType="user"
9
         parameterType="user" >
10
         parameterType="user" >

+ 32 - 0
mybatis_train/src/test/java/Test3.java

@@ -19,12 +19,15 @@ import java.util.List;
19
 
19
 
20
 public class Test3 {
20
 public class Test3 {
21
     UserMapper userMapper;
21
     UserMapper userMapper;
22
+    UserMapper userMapper2;
22
     SqlSession sqlSession;
23
     SqlSession sqlSession;
24
+    SqlSession sqlSession2;
23
     @Before
25
     @Before
24
     public void before() throws IOException {
26
     public void before() throws IOException {
25
         InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
27
         InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
26
         SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
28
         SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
27
         sqlSession = build.openSession(true);
29
         sqlSession = build.openSession(true);
30
+        sqlSession2 = build.openSession(true);
28
         userMapper = sqlSession.getMapper(UserMapper.class);
31
         userMapper = sqlSession.getMapper(UserMapper.class);
29
     }
32
     }
30
     @Test
33
     @Test
@@ -43,6 +46,35 @@ public class Test3 {
43
 //        integers.add(2);
46
 //        integers.add(2);
44
         List<UserEntity> userForEach = userMapper.findUserForEach(integers);
47
         List<UserEntity> userForEach = userMapper.findUserForEach(integers);
45
     }
48
     }
49
+
50
+    /**
51
+     * 测试一级缓存
52
+     */
53
+    @Test
54
+    public void testCache() {
55
+        userMapper2 = sqlSession.getMapper(UserMapper.class);
56
+        ArrayList<Integer> integers = new ArrayList<>();
57
+//        integers.add(1);
58
+//        integers.add(2);
59
+        List<UserEntity> userForEach1 = userMapper.findUserForEach(integers);
60
+        List<UserEntity> userForEach2 = userMapper2.findUserForEach(integers);
61
+    }
62
+
63
+    /**
64
+     * 二级缓存 sqlSessionFactory级别
65
+     */
66
+    @Test
67
+    public void testTwoCache() {
68
+        ArrayList<Integer> integers = new ArrayList<>();
69
+        UserMapper mapper1 = sqlSession.getMapper(UserMapper.class);
70
+        UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
71
+        List<UserEntity> userForEach1 = mapper1.findUserForEach(integers);
72
+        sqlSession.close();
73
+        List<UserEntity> userForEach2 = mapper2.findUserForEach(integers);
74
+        sqlSession2.close();
75
+
76
+    }
77
+
46
     @Test
78
     @Test
47
     public void delete() {
79
     public void delete() {
48
         userMapper.deleteOne(2);
80
         userMapper.deleteOne(2);