MyBatis 缓存机制
缓存是内存当中一块存储数据的区域,目的是提高查询效率。MyBatis 会将查询结果存储在缓存当中,当下次执行相同的 SQL 是不访问数据库,而是直接从缓存中获取结果,从而减少了服务器的压力。
简而言之,经常查询一些不经常发生变化的数据,使用缓存来提高查询效率。
MyBatis 分为 一级缓存 和 二级缓存。
一、一级缓存
- MyBatis 一级缓存也叫本地缓存。SqlSession 对象中包含一个 Executor 对象,Executor 对象中包含一个 PrepetualCache 对象,一级缓存数据就存放在该对象中。
- 一级缓存是 SqlSession 级别的缓存,即只有使用同一个 SqlSession 对象操作数据库时才能共享一级缓存。
- MyBatis 的一级缓存是默认开启的,不需要任何的配置。
具体测试代码如下:
@Test
public void testFirstCache() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(is);
SqlSession session = factory.openSession();
// 使用相同的SqlSession查询
StudentMapper mapper1 = session.getMapper(StudentMapper.class);
StudentMapper mapper2 = session.getMapper(StudentMapper.class);
Student student1 = mapper1.selectById(1);
Student student2 = mapper2.selectById(1);
System.out.println(student1.hashCode() == student2.hashCode());//true
}
由于 MyBatis 的一级缓存是存在 SqlSession 中的,所以在调用一些方法或者操作后,会清空一级缓存,即一级缓存会失效。具体如以下情况:
- 不是同一个 SqlSession
- 同一个 SqlSession 但是查询条件发生了变化
- 同一个 SqlSession 两次查询期间执行了任何一次增删改操作
- 同一个 SqlSession 两次查询期间提交了事务,即调用 commit() 方法
- 同一个 SqlSession 两次查询期间手动清空了缓存,即调用 clearCache() 方法
二、二级缓存
- MyBatis 二级缓存也叫全局缓存。数据存放在 SqlSessionFactory 中,只要是同一个工厂中对象创建的 SqlSession,在进行查询时都能共享数据。一般在项目中只有一个 SqlSessionFactory 对象,所以二级缓存的数据是全项目共享的。
- MyBatis 一级缓存存放的是对象,二级缓存存放的是数据。所以二级缓存要求存放的 Entity 必须是可序列化的,即实现 Serializable 接口。
- MyBatis 的二级缓存默认不开启,手动开启后数据先存放在一级缓存中,只有一级缓存数据清空后,数据才会存到二级缓存中。**注:**SqlSession 调用 clearCache() 方法是无法将数据存到二级缓存中的。
- 在映射文件添加
<cache />
标签,该映射文件下的所有方法都支持二级缓存。该标签有 size 属性,可以设置缓存中的对象数量,默认是 1024 个。
具体的测试代码如下:
@Test
public void testSecondCache() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(is);
SqlSession session1 = factory.openSession();
SqlSession session2 = factory.openSession();
StudentMapper mapper1 = session1.getMapper(StudentMapper.class);
StudentMapper mapper2 = session2.getMapper(StudentMapper.class);
Student student1 = mapper1.selectById(1);
// 让一级缓存失效
session1.commit();
Student student2 = mapper2.selectById(1);
System.out.println(user1.hashCode() == user2.hashCode());//true
}
三、查询顺序
- 一级缓存:SqlSession 级别
- 二级缓存:SqlSessionFactory 级别
第二次查询的顺序为:
- 先查询二级缓存,因为二级缓存中可能会有其他程序已经查出来的数据,可以拿来直接使用。
- 如果二级缓存没有命中,再查询一级缓存
- 如果一级缓存也没有命中,则查询数据库
- SqlSession 关闭之前,一级缓存中的数据会写入二级缓存