引子:
上一篇文章讲了mybatis的二级缓存方式,但同时mybatis也支持其他的缓存框架,只要实现了mybatis下的cache接口即可。
利用ehcache缓存框架首先首先就是基本的jar包,包括ehcache.jar和mybatis-ehcache.jar以及slf4j.下面就来看看ehcache的缓存框架使用方式
首先配置文件:
ehcache.xml放在classpath根目录下即可
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
其次在要使用ehache的mapper下设置缓存的类型:
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
最后在调用这个mapper时如果有二级缓存时便会执行,来看看结果:
可以看到是整合成功的