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包如下:
2 EHCache與mybatis集成
EHCache是一種廣泛使用java分布式緩存通用緩存,JavaEE中的一個輕量級的容器。
EHCache集成是基于ehcache-core,沒有任何其它第三方應(yīng)用程序。
想使用EHCache到她們的應(yīng)用程序的用戶,必須下載EHCache的zip bundle,解壓它添加這些jars到classpath路徑下。使用Apache Maven的用戶只需要在pom.xml文件中添加如下內(nèi)容:
<dependencies> ? ... ? <dependency> ??? <groupId>org.mybatis.caches</groupId> ??? <artifactId>mybatis-ehcache</artifactId> ??? <version>1.0.2</version> ? </dependency> ? ... </dependencies>接著需要在mapper的xml文件中配置如下內(nèi)容:
<mapper namespace="org.acme.FooMapper">
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>...
</mapper>
你也可以提供可以再運行動態(tài)變更的如下參數(shù):
<mapper namespace="org.acme.FooMapper">
? ??<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
??? <property name="timeToIdleSeconds" value="3600"/><!--1hour-->
??? <property name="timeToLiveSeconds" value="3600"/><!--1hour-->
??? <property name="maxEntriesLocalHeap" value="1000"/>
??? <property name="maxEntriesLocalDisk" value="10000000"/>
??? <property name="memoryStoreEvictionPolicy" value="LRU"/>
? </cache>
? ...
</mapper>
如果用戶需要日志緩存操作,可以插入Cachelogging version:
<mapper namespace="org.acme.FooMapper">
? <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
? ...
</mapper>
??? 需要通xml配置文件配置EHCache的用戶,必須要在classpath路徑下放置/ehcache.xml資源。
若/ehcache.xml資源沒有發(fā)現(xiàn)或者在加載/ehcache.xml的時候發(fā)生錯誤,將會使用默認(rèn)的配置。
3 /ehcache.xml配置文件參數(shù)配置
| <?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的唯一標(biāo)識 ??? maxElementsInMemory:內(nèi)存中最大緩存對象數(shù) ??? maxElementsOnDisk:磁盤中最大緩存對象數(shù),若是0表示無窮大 ??? eternal:Element是否永久有效,一但設(shè)置了,timeout將不起作用 ??? overflowToDisk:配置此屬性,當(dāng)內(nèi)存中Element數(shù)量達(dá)到maxElementsInMemory時,Ehcache將會Element寫到磁盤中 ??? timeToIdleSeconds:設(shè)置Element在失效前的允許閑置時間。僅當(dāng)element不是永久有效時使用,可選屬性,默認(rèn)值是0,也就是可閑置時間無窮大 ??? timeToLiveSeconds:設(shè)置Element在失效前允許存活時間。最大時間介于創(chuàng)建時間 ??????? 和失效時間之間。僅當(dāng)element不是永久有效時使用,默認(rèn)是0.,也就是element存活時間無窮大? ??? diskPersistent:是否緩存虛擬機重啟期數(shù)據(jù)? ??? diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認(rèn)是120秒 ??? diskSpoolBufferSizeMB:這個參數(shù)設(shè)置DiskStore(磁盤緩存)的緩存區(qū)大小。默認(rèn)是30MB。每個Cache都應(yīng)該有自己的一個緩沖區(qū) ??? memoryStoreEvictionPolicy:當(dāng)達(dá)到maxElementsInMemory限制時,Ehcache將會根據(jù)指定的策略去清理內(nèi)存。默認(rèn)策略是LRU(最近最少使用)。你可以設(shè)置為FIFO(先進(jìn)先出)或是LFU(較少使用)? ??? --> ?? <defaultCache maxElementsInMemory="10000" ????? eternal="false" ????? timeToIdleSeconds="120" ????? timeToLiveSeconds="120" ????? overflowToDisk="true" ????? maxElementsOnDisk="10000000" ????? diskPersistent="false" ????? diskExpiryThreadIntervalSeconds="120" ????? memoryStoreEvictionPolicy="LRU" /> </ehcache> |
sqlMapConfig.xml中的<settings>設(shè)置如下:
| <!-- 開啟延遲加載 --> <settings> ?? <!-- 全局的延遲加載的開關(guān)必須要開啟 --> ?? <setting name="lazyLoadingEnabled" value="true"/> ?? <!-- 積極加載設(shè)置成false --> ?? <setting name="aggressiveLazyLoading" value="false"/> ?? <!-- 開啟二級緩存, 緩存中只要是需要配置的針對的都是二級緩存 --> ?? <setting name="cacheEnabled" value="true"/> </settings> |
??? mapper文件中的內(nèi)容如下:
總結(jié)
以上是生活随笔為你收集整理的01_MyBatis EHCache集成及所需jar包,ehcache.xml配置文件参数配置及mapper中的参数配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 04springMVC结构,mvc模式,
- 下一篇: 02_MyBatis项目结构,所需jar