zhangxiaoyu пре 2 година
родитељ
комит
d568530773

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

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

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

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

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

@@ -19,12 +19,15 @@ import java.util.List;
19 19
 
20 20
 public class Test3 {
21 21
     UserMapper userMapper;
22
+    UserMapper userMapper2;
22 23
     SqlSession sqlSession;
24
+    SqlSession sqlSession2;
23 25
     @Before
24 26
     public void before() throws IOException {
25 27
         InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
26 28
         SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
27 29
         sqlSession = build.openSession(true);
30
+        sqlSession2 = build.openSession(true);
28 31
         userMapper = sqlSession.getMapper(UserMapper.class);
29 32
     }
30 33
     @Test
@@ -43,6 +46,35 @@ public class Test3 {
43 46
 //        integers.add(2);
44 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 78
     @Test
47 79
     public void delete() {
48 80
         userMapper.deleteOne(2);