lkjhgd 发表于 2016-7-7 09:36:58

mybatis-ehcache 用法配置备忘

1、配置ehcache文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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容器增加缓存配置:


1
2
3
4
<!-- 缓存配置 -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
   <property name="configLocation" value="classpath:${ehcache.configFile}" />
</bean>




3、在你想要缓存的mapper配置文件里面加入以下内容,该查询语句得到的结果将会被缓存:

1
2
3
4
5
6
7
8
9
10
11
<?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属性在该情况下没有。



页: [1]
查看完整版本: mybatis-ehcache 用法配置备忘