您的位置:首页 > 其它

mybatis-ehcache 用法配置备忘

2016-07-06 14:17 423 查看
1、配置ehcache文件:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="defaultCache" >

<diskStore path="../temp/allcure/ehcache" />

<!-- 默认缓存配置. -->
<defaultCache maxEntriesLocalHeap="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
overflowToDisk="true" maxEntriesLocalDisk="1000000" memoryStoreEvictionPolicy="LFU"/>

<!-- 系统缓存 -->
<cache name="sysCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true" />

</ehcache>
2、在spring容器增加缓存配置:

<!-- 缓存配置 -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:${ehcache.configFile}" />
</bean>
3、在你想要缓存的mapper配置文件里面加入以下内容,该查询语句得到的结果将会被缓存:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.DemoMapper">
<!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 -->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->

<select id="selectUserById" parameterType="int" resultType="com.demo.User" useCache=true">
select * from user where id=#{id}
</select>
</mapper>
注意:在MyBatis中有flushCache、useCache这两个配置属性,分为下面几种情况:
(1)当为select语句时:

flushCache默认为false,表示任何时候语句被调用,都不会去清空本地缓存和二级缓存。

useCache默认为true,表示会将本条语句的结果进行二级缓存。

(2)当为insert、update、delete语句时:
flushCache默认为true,表示任何时候语句被调用,都会导致本地缓存和二级缓存被清空。

useCache属性在该情况下没有。

参考地址:http://yedward.net/?id=314
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  缓存 ehcache MyBatis