为了提高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里面的各种结果进行了缓存.程序中不需要修改任何地方.