1.先查二级缓存中有没有
2.再查一级缓存中有没有
3.再查数据库
测试
可以发现第二次走的缓存,增删改操作会刷新缓存,失效
mybatis 手动清理缓存
sqlSession.clearCache();
二级缓存(全局缓存)
mybatis-config.xml中显式的开启全局缓存
<setting name="cacheEnabled" value="true"></setting>
自定义缓存Ehcache
导入依赖
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.1.0</version>
</dependency>
在Mapper中启用Ehcache
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>