MyBatis的缓存
2026/1/15大约 4 分钟MyBatis缓存性能优化
MyBatis的缓存
1. 一级缓存
一级缓存是 SqlSession 级别的,通过同一个 SqlSession 查询的数据会被缓存,下次查询相同的数据,就会从缓存中直接获取,不会从数据库重新访问。
使一级缓存失效的四种情况
- 不同的 SqlSession 对应不同的一级缓存
- 同一个 SqlSession 但是查询条件不同
- 同一个 SqlSession 两次查询期间执行了任何一次增删改操作
- 同一个 SqlSession 两次查询期间手动清空了缓存
// 手动清空缓存
sqlSession.clearCache();测试代码
@Test
public void testFirstLevelCache() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
CacheMapper mapper = sqlSession.getMapper(CacheMapper.class);
// 第一次查询
Emp emp1 = mapper.getEmpByEmpId(1);
System.out.println(emp1);
// 第二次查询(从缓存获取)
Emp emp2 = mapper.getEmpByEmpId(1);
System.out.println(emp2);
System.out.println(emp1 == emp2); // true
}2. 二级缓存
二级缓存是 SqlSessionFactory 级别,通过同一个 SqlSessionFactory 创建的 SqlSession 查询的结果会被缓存;此后若再次执行相同的查询语句,结果就会从缓存中获取。
二级缓存开启的条件
- 在核心配置文件中,设置全局配置属性
cacheEnabled="true",默认为 true,不需要设置 - 在映射文件中设置标签
<cache /> - 二级缓存必须在 SqlSession 关闭或提交之后有效
- 查询的数据所转换的实体类类型必须实现序列化的接口(Serializable)
配置示例
核心配置文件:
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>映射文件:
<mapper namespace="com.example.mapper.CacheMapper">
<cache />
<select id="getEmpByEmpId" resultType="Emp">
select * from t_emp where emp_id = #{empId}
</select>
</mapper>实体类:
public class Emp implements Serializable {
private static final long serialVersionUID = 1L;
// ...
}使二级缓存失效的情况
两次查询之间执行了任意的增删改,会使一级和二级缓存同时失效。
测试代码
@Test
public void testSecondLevelCache() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession1 = sqlSessionFactory.openSession(true);
CacheMapper mapper1 = sqlSession1.getMapper(CacheMapper.class);
Emp emp1 = mapper1.getEmpByEmpId(1);
System.out.println(emp1);
sqlSession1.close(); // 关闭后数据才会写入二级缓存
SqlSession sqlSession2 = sqlSessionFactory.openSession(true);
CacheMapper mapper2 = sqlSession2.getMapper(CacheMapper.class);
Emp emp2 = mapper2.getEmpByEmpId(1); // 从二级缓存获取
System.out.println(emp2);
sqlSession2.close();
}3. 二级缓存的相关配置
在 mapper 配置文件中添加的 cache 标签可以设置一些属性:
eviction 属性:缓存回收策略
| 策略 | 说明 |
|---|---|
| LRU | 最近最少使用的:移除最长时间不被使用的对象(默认) |
| FIFO | 先进先出:按对象进入缓存的顺序来移除它们 |
| SOFT | 软引用:移除基于垃圾回收器状态和软引用规则的对象 |
| WEAK | 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象 |
flushInterval 属性:刷新间隔
单位毫秒。默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新。
size 属性:引用数目
正整数,代表缓存最多可以存储多少个对象,太大容易导致内存溢出。
readOnly 属性:只读
| 值 | 说明 |
|---|---|
| true | 只读缓存;会给所有调用者返回缓存对象的相同实例。因此这些对象不能被修改。这提供了很重要的性能优势 |
| false | 读写缓存;会返回缓存对象的拷贝(通过序列化)。这会慢一些,但是安全,因此默认是 false |
配置示例
<cache eviction="LRU" flushInterval="60000" size="512" readOnly="true"/>4. MyBatis缓存查询的顺序
- 先查询二级缓存,因为二级缓存中可能会有其它程序已经查出来的数据,可以拿来直接使用
- 如果二级缓存没有命中,再查询一级缓存
- 如果一级缓存也没有命中,则查询数据库
- SqlSession 关闭之后,一级缓存中的数据会写入二级缓存
5. 整合第三方缓存EHCache
添加依赖
<!-- MyBatis EHCache整合包 -->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.2.1</version>
</dependency>
<!-- slf4j日志门面 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>创建EHCache配置文件
在 src/main/resources 目录下创建 ehcache.xml:
<?xml version="1.0" encoding="utf-8" ?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<!-- 磁盘保存路径 -->
<diskStore path="D:\ehcache"/>
<defaultCache
maxElementsInMemory="1000"
maxElementsOnDisk="10000000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>设置二级缓存的类型
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>EHCache配置文件说明
| 属性名 | 说明 |
|---|---|
| maxElementsInMemory | 在内存中缓存的 element 的最大数目 |
| maxElementsOnDisk | 在磁盘上缓存的 element 的最大数目 |
| eternal | 设定缓存的 elements 是否永远不过期 |
| overflowToDisk | 当内存中缓存达到 maxElementsInMemory 时,是否将溢出的对象写到磁盘 |
| timeToIdleSeconds | 缓存数据的钝化时间(秒) |
| timeToLiveSeconds | 缓存数据的生存时间(秒) |
| diskExpiryThreadIntervalSeconds | 磁盘缓存的清理线程运行间隔 |
| memoryStoreEvictionPolicy | 内存存储与释放策略 |
