1. Cache basics:
Cache: cache
The role of the cache: to improve the execution efficiency of the program by reducing IO
mybatis cache includes:
Level 1 cache: The data of speech query is stored in SqlSession
Second-level cache: store the queried data in SqlSessionFactory
Or integrate a third-party cache: such as EhCache...
Mybatis cache is only for DQL statements, that is to say, the cache mechanism only corresponds to select statements.
2. Level 1 cache:
The first-level cache is enabled by default, and no configuration is required
Principle: As long as the same SQL statement is executed using the same SqlSession object, the cache will be taken
// @test
public static void main(String[] args) {
SqlSession sqlSession = SqlSessionUtil.openSession();
// mapper1和mapper2的sqlsession都是相同的, 因此会走缓存
CarMapper mapper1 = sqlSession.getMapper(CarMapper.class);
Car car1 = mapper1.selectById(16L);
// 会缓存
CarMapper mapper2 = sqlSession.getMapper(CarMapper.class);
Car car2 = mapper2.selectById(16L);
sqlSession.close();
}
When is the cache not taken?
The SqlSession object is not the same, and the cache must not be taken
The query conditions are different, and the cache must not be taken
When is the L1 cache invalidated?
Doing any of the following between the first DQL and the second DQL will clear the first-level cache
The clearCache() method of sqlSession is executed, which is to manually clear the cache
The INSERT/UPDATE/DELETE statement is executed, no matter which table is operated, the first-level cache will be cleared
3. Second level cache:
The scope of the second level cache is SqlSessionFactory
The following conditions are required to use the second-level cache
Globally enable or disable any cache configured in all mapping configuration files, the default is true, no need to set
Add configuration to the SqlMapper.xml file that needs to use the second-level cache:
The entity class object using the second-level cache must be serializable, that is, it must implement the java.io.Serializable interface
After the SqlSession object is closed or submitted, the data in the first-level cache will be written to the second-level cache, and the second-level cache will be available at this time.
// @test
public static void main(String[] args) {
SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().
build(Resources.getResourceAsStream("mybatis-config.xml"));
SqlSession session1 = sqlsessionfactory.openSession();
SqlSession session2 = sqlsessionfactory.openSession();
CarMapper mapper1 = sqlSession1.getMapper(CarMapper.class);
CarMapper mapper2 = sqlSession2.getMapper(CarMapper.class);
// 这行代码执行结束之后, 实际上数据是缓存到一级缓存当中. (sqlSession1是一级缓存)
Car car1 = mapper1.selectById(16L);
System.out.println(car1);
// 这里如果不关闭sqlSession1对象的话, 二级缓存中还是没有数据
// 如果执行了这行代码, sqlSession1的一级缓存中的数据会放到二级缓存当中
sqlSession1.close();
// 这行代码执行结束之后, 实际上数据是缓存到一级缓存当中. (sqlSession2是一级缓存)
Car car2 = mapper2.selectById(16L);
System.out.println(car1);
// 程序执行到这里的时候, 会将sqlSession1这个一级缓存中的数据写入到二级缓存当中
sqlSession1.close();
// 程序执行到这里的时候, 会将sqlSession2这个一级缓存中的数据写入到二级缓存当中
sqlSession2.close();
}
Invalidation of the second-level cache: It only needs to add, delete, and modify operations between two queries, and the second-level cache will be invalid (the first-level cache will also be invalid)