Mybatis——缓存机制

1. 一级缓存:默认开启,无需任何配置,作用域范围,同一个SqlSession

2. 二级缓存:作用域范围,整个configuration

1.在全局配置文件sqlMapConfig.xml中开启二级缓存总开关
<settings>
<!-- 开启二级缓存(总开关) -->
<setting name="cacheEnabled" value="true"/>
</settings>

2.在所有XXXMapper.xml中开启当前mapper文件的缓存
<!-- 开启本mapper文件中的二级缓存 -->
<cache></cache>

所定义语句中: useCache="false" 可关闭二级缓存,默认为true

3.所有需使用二级缓存的pojo对象需实现序列化接口

public class Order implements Serializable{}

3. 第三方缓存框架(ehcache):
1.导入jar包
mybatis-ehcache-1.0.2.jar
ehcache-2.10.1.jar

2.在所有XXXMapper.xml中修改cache指向的type
<!-- 整合第三方框架的二级缓存功能 -->
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>

3.在类路径下配置ehcache的配置文件ehcache.xml

原文地址:https://www.cnblogs.com/ccw95/p/6184108.html