1. 整合需要的ehcache的jar包
ehcache-core 包依赖 sfl4j-api,slf4j-log4j12包
mybatis-ehcache是将mybatis和ehcache适配起来的包
2.整合还需要一个ehcache.xml文件
xml文件配置如下
<ehcache>
<!--
磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
path:指定在硬盘上存储对象的路径
-->
<diskStore path="E:\ehcache" />
<!--
defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
eternal:代表对象是否永不过期
timeToIdleSeconds:最大的发呆时间
timeToLiveSeconds:最大的存活时间
overflowToDisk:是否允许对象被写入到磁盘
-->
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
<!--
cache:为指定名称的对象进行缓存的特殊配置
name:指定对象的完整名
-->
<cache name="com.zbaccp.entity.Person" maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />
</ehcache>
3. 在mapper.xml中使用缓存即可
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>