├── README.md
├── src
├── test
│ ├── resources
│ │ ├── local-mysql.properties
│ │ ├── log4j.properties
│ │ └── mybatis-config.xml
│ └── java
│ │ └── mapper
│ │ └── StudentMapperTest.java
├── main
│ ├── java
│ │ ├── mapper
│ │ │ ├── ClassMapper.java
│ │ │ └── StudentMapper.java
│ │ └── entity
│ │ │ ├── Class.java
│ │ │ └── StudentEntity.java
│ └── resources
│ │ └── mapper
│ │ ├── ClassMapper.xml
│ │ └── studentMapper.xml
├── class.sql
├── classroom.sql
└── student.sql
├── .gitignore
└── pom.xml
/README.md:
--------------------------------------------------------------------------------
1 | # mybatis-cache-demo
2 | gitchat-一步步学习mybatis缓存-演示代码
3 |
--------------------------------------------------------------------------------
/src/test/resources/local-mysql.properties:
--------------------------------------------------------------------------------
1 | driver=com.mysql.jdbc.Driver
2 | url=jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
3 | username=root
4 | password=123456
5 |
--------------------------------------------------------------------------------
/src/main/java/mapper/ClassMapper.java:
--------------------------------------------------------------------------------
1 | package mapper;
2 |
3 | import org.apache.ibatis.annotations.Param;
4 |
5 | public interface ClassMapper {
6 |
7 | public int updateClassName(@Param("name") String className, @Param("id") int id);
8 | }
9 |
--------------------------------------------------------------------------------
/src/test/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | # Global logging configuration
2 | log4j.rootLogger=ERROR, stdout
3 | # MyBatis logging configuration...
4 | log4j.logger.mapper.StudentMapper=TRACE
5 | log4j.logger.mapper.ClassMapper=TRACE
6 | # Console output...
7 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender
8 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
9 | log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
--------------------------------------------------------------------------------
/src/main/resources/mapper/ClassMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | UPDATE class SET name = #{name} WHERE id = #{id}
8 |
9 |
10 |
--------------------------------------------------------------------------------
/src/main/java/mapper/StudentMapper.java:
--------------------------------------------------------------------------------
1 | package mapper;
2 |
3 | import org.apache.ibatis.annotations.Param;
4 |
5 | import entity.StudentEntity;
6 |
7 | import java.util.Set;
8 |
9 | public interface StudentMapper {
10 |
11 | public StudentEntity getStudentById(int id);
12 |
13 | public int addStudent(StudentEntity student);
14 |
15 | public int updateStudentName(@Param("name") String name, @Param("id") int id);
16 |
17 | public StudentEntity getStudentByIdWithClassInfo(int id);
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/entity/Class.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | public class Class {
4 |
5 | private int classId;
6 |
7 | private String className;
8 |
9 | @Override
10 | public String toString() {
11 | final StringBuilder sb = new StringBuilder("Class{");
12 | sb.append("classId=").append(classId);
13 | sb.append(", className='").append(className).append('\'');
14 | sb.append('}');
15 | return sb.toString();
16 | }
17 |
18 | public int getClassId() {
19 | return classId;
20 | }
21 |
22 | public void setClassId(int classId) {
23 | this.classId = classId;
24 | }
25 |
26 | public String getClassName() {
27 | return className;
28 | }
29 |
30 | public void setClassName(String className) {
31 | this.className = className;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/resources/mapper/studentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
9 |
10 |
17 |
18 |
19 | INSERT INTO student(name,age) VALUES(#{name}, #{age})
20 |
21 |
22 |
23 | UPDATE student SET name = #{name} WHERE id = #{id}
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by .ignore support plugin (hsz.mobi)
2 | ### JetBrains template
3 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
4 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
5 |
6 | # User-specific stuff:
7 | .idea/**/workspace.xml
8 | .idea/**/tasks.xml
9 | .idea/dictionaries
10 |
11 | # Sensitive or high-churn files:
12 | .idea/**/dataSources/
13 | .idea/**/dataSources.ids
14 | .idea/**/dataSources.xml
15 | .idea/**/dataSources.local.xml
16 | .idea/**/sqlDataSources.xml
17 | .idea/**/dynamic.xml
18 | .idea/**/uiDesigner.xml
19 |
20 | # Gradle:
21 | .idea/**/gradle.xml
22 | .idea/**/libraries
23 |
24 | # Mongo Explorer plugin:
25 | .idea/**/mongoSettings.xml
26 |
27 | ## File-based project format:
28 | *.iws
29 |
30 | ## Plugin-specific files:
31 |
32 | # IntelliJ
33 | /out/
34 |
35 | # mpeltonen/sbt-idea plugin
36 | .idea_modules/
37 |
38 | # JIRA plugin
39 | atlassian-ide-plugin.xml
40 |
41 | # Crashlytics plugin (for Android Studio and IntelliJ)
42 | com_crashlytics_export_strings.xml
43 | crashlytics.properties
44 | crashlytics-build.properties
45 | fabric.properties
46 |
47 | /mybatis-cache-demo.iml
48 | /.idea/*
49 | /target/*
50 | /target
--------------------------------------------------------------------------------
/src/main/java/entity/StudentEntity.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | import java.io.Serializable;
4 |
5 | public class StudentEntity implements Serializable {
6 |
7 | // 学号
8 | private Integer id;
9 |
10 | // 姓名
11 | private String name;
12 |
13 | // 年龄
14 | private Integer age;
15 |
16 | // 班级
17 | private String className;
18 |
19 | public String getClassName() {
20 | return className;
21 | }
22 |
23 | public void setClassName(String className) {
24 | this.className = className;
25 | }
26 |
27 | public Integer getId() {
28 | return id;
29 | }
30 |
31 | public void setId(Integer id) {
32 | this.id = id;
33 | }
34 |
35 | public String getName() {
36 | return name;
37 | }
38 |
39 | public void setName(String name) {
40 | this.name = name;
41 | }
42 |
43 | public Integer getAge() {
44 | return age;
45 | }
46 |
47 | public void setAge(Integer age) {
48 | this.age = age;
49 | }
50 |
51 | @Override
52 | public String toString() {
53 | final StringBuilder sb = new StringBuilder("StudentEntity{");
54 | sb.append("id=").append(id);
55 | sb.append(", name='").append(name).append('\'');
56 | sb.append(", age=").append(age);
57 | sb.append(", className='").append(className).append('\'');
58 | sb.append('}');
59 | return sb.toString();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.kailuncen.gitchat
8 | mybatis-cache-demo
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | mysql
14 | mysql-connector-java
15 | 5.1.42
16 |
17 |
18 | org.mybatis
19 | mybatis
20 | 3.4.4
21 |
22 |
23 | junit
24 | junit
25 | 4.12
26 |
27 |
28 | log4j
29 | log4j
30 | 1.2.17
31 |
32 |
33 |
34 |
35 |
36 | aliyunmaven
37 | http://maven.aliyun.com/nexus/content/groups/public/
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/src/test/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/src/class.sql:
--------------------------------------------------------------------------------
1 | -- MySQL dump 10.13 Distrib 5.7.15, for osx10.11 (x86_64)
2 | --
3 | -- Host: 127.0.0.1 Database: demo
4 | -- ------------------------------------------------------
5 | -- Server version 5.7.15
6 |
7 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
8 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
9 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
10 | /*!40101 SET NAMES utf8 */;
11 | /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
12 | /*!40103 SET TIME_ZONE='+00:00' */;
13 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
14 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
15 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
16 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
17 |
18 | --
19 | -- Table structure for table `class`
20 | --
21 |
22 | DROP TABLE IF EXISTS `class`;
23 | /*!40101 SET @saved_cs_client = @@character_set_client */;
24 | /*!40101 SET character_set_client = utf8 */;
25 | CREATE TABLE `class` (
26 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
27 | `name` varchar(20) COLLATE utf8_bin DEFAULT NULL,
28 | PRIMARY KEY (`id`)
29 | ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
30 | /*!40101 SET character_set_client = @saved_cs_client */;
31 |
32 | --
33 | -- Dumping data for table `class`
34 | --
35 |
36 | LOCK TABLES `class` WRITE;
37 | /*!40000 ALTER TABLE `class` DISABLE KEYS */;
38 | INSERT INTO `class` (`id`, `name`) VALUES (1,'一班'),(2,'二班');
39 | /*!40000 ALTER TABLE `class` ENABLE KEYS */;
40 | UNLOCK TABLES;
41 | /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
42 |
43 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
44 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
45 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
46 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
47 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
48 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
49 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
50 |
51 | -- Dump completed on 2017-06-30 14:43:55
52 |
--------------------------------------------------------------------------------
/src/classroom.sql:
--------------------------------------------------------------------------------
1 | -- MySQL dump 10.13 Distrib 5.7.15, for osx10.11 (x86_64)
2 | --
3 | -- Host: 127.0.0.1 Database: demo
4 | -- ------------------------------------------------------
5 | -- Server version 5.7.15
6 |
7 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
8 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
9 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
10 | /*!40101 SET NAMES utf8 */;
11 | /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
12 | /*!40103 SET TIME_ZONE='+00:00' */;
13 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
14 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
15 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
16 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
17 |
18 | --
19 | -- Table structure for table `classroom`
20 | --
21 |
22 | DROP TABLE IF EXISTS `classroom`;
23 | /*!40101 SET @saved_cs_client = @@character_set_client */;
24 | /*!40101 SET character_set_client = utf8 */;
25 | CREATE TABLE `classroom` (
26 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
27 | `class_id` int(11) DEFAULT NULL,
28 | `student_id` int(11) DEFAULT NULL,
29 | PRIMARY KEY (`id`)
30 | ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
31 | /*!40101 SET character_set_client = @saved_cs_client */;
32 |
33 | --
34 | -- Dumping data for table `classroom`
35 | --
36 |
37 | LOCK TABLES `classroom` WRITE;
38 | /*!40000 ALTER TABLE `classroom` DISABLE KEYS */;
39 | INSERT INTO `classroom` (`id`, `class_id`, `student_id`) VALUES (1,1,1),(2,1,2),(3,2,3),(4,2,4);
40 | /*!40000 ALTER TABLE `classroom` ENABLE KEYS */;
41 | UNLOCK TABLES;
42 | /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
43 |
44 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
45 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
46 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
47 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
48 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
49 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
50 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
51 |
52 | -- Dump completed on 2017-06-30 14:44:05
53 |
--------------------------------------------------------------------------------
/src/student.sql:
--------------------------------------------------------------------------------
1 | -- MySQL dump 10.13 Distrib 5.7.15, for osx10.11 (x86_64)
2 | --
3 | -- Host: 127.0.0.1 Database: demo
4 | -- ------------------------------------------------------
5 | -- Server version 5.7.15
6 |
7 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
8 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
9 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
10 | /*!40101 SET NAMES utf8 */;
11 | /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
12 | /*!40103 SET TIME_ZONE='+00:00' */;
13 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
14 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
15 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
16 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
17 |
18 | --
19 | -- Table structure for table `student`
20 | --
21 |
22 | DROP TABLE IF EXISTS `student`;
23 | /*!40101 SET @saved_cs_client = @@character_set_client */;
24 | /*!40101 SET character_set_client = utf8 */;
25 | CREATE TABLE `student` (
26 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
27 | `name` varchar(200) COLLATE utf8_bin DEFAULT NULL,
28 | `age` tinyint(3) unsigned DEFAULT NULL,
29 | PRIMARY KEY (`id`)
30 | ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
31 | /*!40101 SET character_set_client = @saved_cs_client */;
32 |
33 | --
34 | -- Dumping data for table `student`
35 | --
36 |
37 | LOCK TABLES `student` WRITE;
38 | /*!40000 ALTER TABLE `student` DISABLE KEYS */;
39 | INSERT INTO `student` (`id`, `name`, `age`) VALUES (1,'点点',16),(2,'平平',16),(3,'美美',16),(4,'团团',16);
40 | /*!40000 ALTER TABLE `student` ENABLE KEYS */;
41 | UNLOCK TABLES;
42 | /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
43 |
44 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
45 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
46 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
47 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
48 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
49 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
50 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
51 |
52 | -- Dump completed on 2017-06-30 14:44:13
53 |
--------------------------------------------------------------------------------
/src/test/java/mapper/StudentMapperTest.java:
--------------------------------------------------------------------------------
1 | package mapper;
2 |
3 | import entity.StudentEntity;
4 | import org.apache.ibatis.io.Resources;
5 | import org.apache.ibatis.session.SqlSession;
6 | import org.apache.ibatis.session.SqlSessionFactory;
7 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
8 | import org.junit.Assert;
9 | import org.junit.Before;
10 | import org.junit.Test;
11 |
12 | import java.io.IOException;
13 |
14 | import static org.junit.Assert.*;
15 |
16 | public class StudentMapperTest {
17 |
18 | private SqlSessionFactory factory;
19 |
20 | @Before
21 | public void setUp() throws Exception {
22 | factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
23 |
24 | }
25 |
26 | @Test
27 | public void showDefaultCacheConfiguration() {
28 | System.out.println("本地缓存范围: " + factory.getConfiguration().getLocalCacheScope());
29 | System.out.println("二级缓存是否被启用: " + factory.getConfiguration().isCacheEnabled());
30 | }
31 |
32 | /**
33 | *
34 | *
35 | * @throws Exception
36 | */
37 | @Test
38 | public void testLocalCache() throws Exception {
39 | SqlSession sqlSession = factory.openSession(true); // 自动提交事务
40 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
41 |
42 | System.out.println(studentMapper.getStudentById(1));
43 | System.out.println(studentMapper.getStudentById(1));
44 | System.out.println(studentMapper.getStudentById(1));
45 |
46 | sqlSession.close();
47 | }
48 |
49 | /**
50 | *
51 | *
52 | * @throws Exception
53 | */
54 | @Test
55 | public void testLocalCacheClear() throws Exception {
56 | SqlSession sqlSession = factory.openSession(true); // 自动提交事务
57 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
58 |
59 | System.out.println(studentMapper.getStudentById(1));
60 | System.out.println("增加了" + studentMapper.addStudent(buildStudent()) + "个学生");
61 | System.out.println(studentMapper.getStudentById(1));
62 |
63 | sqlSession.close();
64 | }
65 |
66 | /**
67 | *
68 | *
69 | * @throws Exception
70 | */
71 | @Test
72 | public void testLocalCacheScope() throws Exception {
73 | SqlSession sqlSession1 = factory.openSession(true); // 自动提交事务
74 | SqlSession sqlSession2 = factory.openSession(true); // 自动提交事务
75 |
76 | StudentMapper studentMapper = sqlSession1.getMapper(StudentMapper.class);
77 | StudentMapper studentMapper2 = sqlSession2.getMapper(StudentMapper.class);
78 |
79 | System.out.println("studentMapper读取数据: " + studentMapper.getStudentById(1));
80 | System.out.println("studentMapper读取数据: " + studentMapper.getStudentById(1));
81 | System.out.println("studentMapper2更新了" + studentMapper2.updateStudentName("小岑",1) + "个学生的数据");
82 | System.out.println("studentMapper读取数据: " + studentMapper.getStudentById(1));
83 | System.out.println("studentMapper2读取数据: " + studentMapper2.getStudentById(1));
84 |
85 | }
86 |
87 |
88 | private StudentEntity buildStudent(){
89 | StudentEntity studentEntity = new StudentEntity();
90 | studentEntity.setName("明明");
91 | studentEntity.setAge(20);
92 | return studentEntity;
93 | }
94 |
95 | /**
96 | *
97 | *
98 | * @throws Exception
99 | */
100 | @Test
101 | public void testCacheWithoutCommitOrClose() throws Exception {
102 | SqlSession sqlSession1 = factory.openSession(true); // 自动提交事务
103 | SqlSession sqlSession2 = factory.openSession(true); // 自动提交事务
104 |
105 | StudentMapper studentMapper = sqlSession1.getMapper(StudentMapper.class);
106 | StudentMapper studentMapper2 = sqlSession2.getMapper(StudentMapper.class);
107 |
108 | System.out.println("studentMapper读取数据: " + studentMapper.getStudentById(1));
109 | System.out.println("studentMapper2读取数据: " + studentMapper2.getStudentById(1));
110 |
111 | }
112 |
113 | /**
114 | *
115 | *
116 | * @throws Exception
117 | */
118 | @Test
119 | public void testCacheWithCommitOrClose() throws Exception {
120 | SqlSession sqlSession1 = factory.openSession(true); // 自动提交事务
121 | SqlSession sqlSession2 = factory.openSession(true); // 自动提交事务
122 |
123 | StudentMapper studentMapper = sqlSession1.getMapper(StudentMapper.class);
124 | StudentMapper studentMapper2 = sqlSession2.getMapper(StudentMapper.class);
125 |
126 | System.out.println("studentMapper读取数据: " + studentMapper.getStudentById(1));
127 | sqlSession1.close();
128 | System.out.println("studentMapper2读取数据: " + studentMapper2.getStudentById(1));
129 |
130 | }
131 |
132 | /**
133 | *
134 | *
135 | * @throws Exception
136 | */
137 | @Test
138 | public void testCacheWithUpdate() throws Exception {
139 | SqlSession sqlSession1 = factory.openSession(true); // 自动提交事务
140 | SqlSession sqlSession2 = factory.openSession(true); // 自动提交事务
141 | SqlSession sqlSession3 = factory.openSession(true); // 自动提交事务
142 |
143 |
144 | StudentMapper studentMapper = sqlSession1.getMapper(StudentMapper.class);
145 | StudentMapper studentMapper2 = sqlSession2.getMapper(StudentMapper.class);
146 | StudentMapper studentMapper3 = sqlSession3.getMapper(StudentMapper.class);
147 |
148 |
149 | System.out.println("studentMapper读取数据: " + studentMapper.getStudentById(1));
150 | sqlSession1.close();
151 | System.out.println("studentMapper2读取数据: " + studentMapper2.getStudentById(1));
152 |
153 | studentMapper3.updateStudentName("方方",1);
154 | sqlSession3.commit();
155 | System.out.println("studentMapper2读取数据: " + studentMapper2.getStudentById(1));
156 | }
157 |
158 | /**
159 | *
160 | *
161 | * @throws Exception
162 | */
163 | @Test
164 | public void testCacheWithDiffererntNamespace() throws Exception {
165 | SqlSession sqlSession1 = factory.openSession(true); // 自动提交事务
166 | SqlSession sqlSession2 = factory.openSession(true); // 自动提交事务
167 | SqlSession sqlSession3 = factory.openSession(true); // 自动提交事务
168 |
169 |
170 | StudentMapper studentMapper = sqlSession1.getMapper(StudentMapper.class);
171 | StudentMapper studentMapper2 = sqlSession2.getMapper(StudentMapper.class);
172 | ClassMapper classMapper = sqlSession3.getMapper(ClassMapper.class);
173 |
174 |
175 | System.out.println("studentMapper读取数据: " + studentMapper.getStudentByIdWithClassInfo(1));
176 | sqlSession1.close();
177 |
178 | System.out.println("studentMapper2读取数据: " + studentMapper2.getStudentByIdWithClassInfo(1));
179 |
180 | classMapper.updateClassName("特色一班",1);
181 | sqlSession3.commit();
182 |
183 | System.out.println("studentMapper2读取数据: " + studentMapper2.getStudentByIdWithClassInfo(1));
184 | }
185 |
186 | /**
187 | *
188 | *
189 | * @throws Exception
190 | */
191 | @Test
192 | public void testCacheWithDiffererntNamespaceWithCacheRef() throws Exception {
193 | SqlSession sqlSession1 = factory.openSession(true); // 自动提交事务
194 | SqlSession sqlSession2 = factory.openSession(true); // 自动提交事务
195 | SqlSession sqlSession3 = factory.openSession(true); // 自动提交事务
196 |
197 |
198 | StudentMapper studentMapper = sqlSession1.getMapper(StudentMapper.class);
199 | StudentMapper studentMapper2 = sqlSession2.getMapper(StudentMapper.class);
200 | ClassMapper classMapper = sqlSession3.getMapper(ClassMapper.class);
201 |
202 |
203 | System.out.println("studentMapper读取数据: " + studentMapper.getStudentByIdWithClassInfo(1));
204 | sqlSession1.close();
205 |
206 | System.out.println("studentMapper2读取数据: " + studentMapper2.getStudentByIdWithClassInfo(1));
207 |
208 | classMapper.updateClassName("特色一班",1);
209 | sqlSession3.commit();
210 |
211 | System.out.println("studentMapper2读取数据: " + studentMapper2.getStudentByIdWithClassInfo(1));
212 | }
213 |
214 |
215 | }
--------------------------------------------------------------------------------