天天看点

01_MyBatis EHCache集成及所需jar包,ehcache.xml配置文件参数配置及mapper中的参数配置



1

与mybatis集成时需要的jar

ehcache-core-2.6.5.jar

mybatis-ehcache-1.0.2.jar

mybatis、日志、ehcache所需要的jar包如下:

01_MyBatis EHCache集成及所需jar包,ehcache.xml配置文件参数配置及mapper中的参数配置

2 ehcache与mybatis集成

ehcache是一种广泛使用java分布式缓存通用缓存,javaee中的一个轻量级的容器。

ehcache集成是基于ehcache-core,没有任何其它第三方应用程序。

想使用ehcache到她们的应用程序的用户,必须下载ehcache的zip

bundle,解压它添加这些jars到classpath路径下。使用apache

maven的用户只需要在pom.xml文件中添加如下内容:

接着需要在mapper的xml文件中配置如下内容:

<mapper

namespace="org.acme.foomapper">

<cache

type="org.mybatis.caches.ehcache.ehcachecache"/>...

</mapper>

你也可以提供可以再运行动态变更的如下参数:

  <cache

type="org.mybatis.caches.ehcache.ehcachecache"/>

<property

name="timetoidleseconds"

value="3600"/><!--1hour-->

name="timetoliveseconds"

name="maxentrieslocalheap"

value="1000"/>

name="maxentrieslocaldisk"

value="10000000"/>

name="memorystoreevictionpolicy"

value="lru"/>

</cache>

...

如果用户需要日志缓存操作,可以插入cachelogging version:

type="org.mybatis.caches.ehcache.loggingehcache"/>

需要通xml配置文件配置ehcache的用户,必须要在classpath路径下放置/ehcache.xml资源。

3 /ehcache.xml配置文件参数配置

<?xml

version="1.0"

encoding="utf-8"?>

<ehcache

xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"

xsi:nonamespaceschemalocation="ehcache.xsd"

updatecheck="true"

monitoring="autodetect"

dynamicconfig="true">

<diskstore

path="java.io.tmpdir"

/>

<!-- 

name:cache的唯一标识

maxelementsinmemory:内存中最大缓存对象数

maxelementsondisk:磁盘中最大缓存对象数,若是0表示无穷大

eternal:element是否永久有效,一但设置了,timeout将不起作用

overflowtodisk:配置此属性,当内存中element数量达到maxelementsinmemory时,ehcache将会element写到磁盘中

timetoidleseconds:设置element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大

timetoliveseconds:设置element在失效前允许存活时间。最大时间介于创建时间

和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大 

diskpersistent:是否缓存虚拟机重启期数据 

diskexpirythreadintervalseconds:磁盘失效线程运行时间间隔,默认是120秒

diskspoolbuffersizemb:这个参数设置diskstore(磁盘缓存)的缓存区大小。默认是30mb。每个cache都应该有自己的一个缓冲区

memorystoreevictionpolicy:当达到maxelementsinmemory限制时,ehcache将会根据指定的策略去清理内存。默认策略是lru(最近最少使用)。你可以设置为fifo(先进先出)或是lfu(较少使用) 

-->

<defaultcache

maxelementsinmemory="10000"

eternal="false"

timetoidleseconds="120"

timetoliveseconds="120"

overflowtodisk="true"

maxelementsondisk="10000000"

diskpersistent="false"

diskexpirythreadintervalseconds="120"

memorystoreevictionpolicy="lru"

</ehcache>

sqlmapconfig.xml中的<settings>设置如下:

<!--

开启延迟加载 -->

<settings>

全局的延迟加载的开关必须要开启 -->

<setting

name="lazyloadingenabled"

value="true"/>

积极加载设置成false -->

name="aggressivelazyloading"

value="false"/>

开启二级缓存,

缓存中只要是需要配置的针对的都是二级缓存 -->

name="cacheenabled"

</settings>

mapper文件中的内容如下:

01_MyBatis EHCache集成及所需jar包,ehcache.xml配置文件参数配置及mapper中的参数配置