為了提高MyBatis的性能,有時候我們需要加入緩存支持,目前用的比較多的緩存莫過于ehcache緩存了,ehcache性能強大,而且位各種應用都提供了解決方案,在此我們主要是做查詢緩存,提高查詢的效率.
?
在Mybatis的官網上把集成ehcache的文檔下載下來看了看,說的太簡單了,對于新手很難理解,而且里面說的也不是很清楚,經過一番折騰,終于將ehcache加入了.
?
官網上提供了一個MyBatis-ehcache.jar的包用于整合ehcache緩存,文檔中還說明需要一個ehcache-core.jar的包,除了這兩個包之外有幾個包也是必須的,官方并沒有說明,以下是需要加入的所有和ehcache相關的包:
1.ehcache-core-2.4.4.jar
2.mybatis-ehcache-1.0.0.jar
3.slf4j-api-1.6.1.jar
4.slf4j-log4j12-1.6.2.jar
?
除此之外還有mybatis的jar包,log4j,MySQL驅動,這些大家應該都知道.
?
將上述包加入項目之后,新建一個文件名,該文件名必須為ehcache.xml,放在類路徑下面,內容如下
?
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <ehcache?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
- ????xsi:noNamespaceSchemaLocation="../bin/ehcache.xsd">??
- ????<!--??
- ????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(較少使用)
-
- 備注: 持久化到硬盤的路徑由虛擬機參數"java.io.tmpdir"決定.??例如, 在windows中, 會在此路徑下?C:\Documents and Settings\li\Local Settings\Temp??在linux中, 通常會在: /tmp 下?System.out.println(System.getProperty("java.io.tmpdir"));???
- ????-->??
- ????<defaultCache?overflowToDisk="true"?eternal="false"/>??
- ????<diskStore?path="D:/cache"?/>??
- ????<!--??
- ????????<cache?name="zzugxy"?overflowToDisk="true"?eternal="false"??
- ????????timeToIdleSeconds="300"?timeToLiveSeconds="600"?maxElementsInMemory="1000"??
- ????????maxElementsOnDisk="10"?diskPersistent="true"?diskExpiryThreadIntervalSeconds="300"??
- ????????diskSpoolBufferSizeMB="100"?memoryStoreEvictionPolicy="LRU"?/>??
- ????-->??
- </ehcache>??
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <ehcache?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
- ????xsi:noNamespaceSchemaLocation="../bin/ehcache.xsd">??
- ????<!--??
- ????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?overflowToDisk="true"?eternal="false"/>??
- ????<diskStore?path="D:/cache"?/>??
- ????<!--??
- ????????<cache?name="zzugxy"?overflowToDisk="true"?eternal="false"??
- ????????timeToIdleSeconds="300"?timeToLiveSeconds="600"?maxElementsInMemory="1000"??
- ????????maxElementsOnDisk="10"?diskPersistent="true"?diskExpiryThreadIntervalSeconds="300"??
- ????????diskSpoolBufferSizeMB="100"?memoryStoreEvictionPolicy="LRU"?/>??
- ????-->??
- </ehcache>??
?
該文件是ehcache的配置文件,上面的注釋已經說得很清楚了,這里我用的是默認的配置
至此ehcache已經配置好了,然后只需要在你想要緩存的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.qiuqiu.dao.PersonDao">??
- ??????
- ????<cache?type="org.mybatis.caches.ehcache.LoggingEhcache"/>??
- ??????
- ??????
- ????<select?id="selectUserById"?parameterType="int"?resultType="org.qiuqiu.vo.Person">??
- ????????select?*?from?person?where?id=#{id}???
- ????</select>??
- </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.qiuqiu.dao.PersonDao">??
- ??????
- ????<cache?type="org.mybatis.caches.ehcache.LoggingEhcache"/>??
- ??????
- ??????
- ????<select?id="selectUserById"?parameterType="int"?resultType="org.qiuqiu.vo.Person">??
- ????????select?*?from?person?where?id=#{id}???
- ????</select>??
- </mapper>??
?
這樣就對這個mapper里面的各種結果進行了緩存.程序中不需要修改任何地方.