`
wsqwsq000
  • 浏览: 676465 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

iBATIS中使用EHCache

 
阅读更多

需要实现CacheController接口并通过一系列的配置委托iBATIS使用EHCache进行缓存.

  1. package com.ibatis.sqlmap.engine.cache.EhCacheController;
  2. import java.net.URL;
  3. import java.util.Properties;
  4. import net.sf.ehcache.Cache;
  5. import net.sf.ehcache.CacheManager;
  6. import net.sf.ehcache.Element;
  7. import com.ibatis.sqlmap.engine.cache.CacheController;
  8. import com.ibatis.sqlmap.engine.cache.CacheModel;
  9. /**
  10. * EhCache Implementation of the {@link com.ibatis.sqlmap.engine.cache.CacheController} interface to be able to use
  11. * EhCache as a cache implementation in iBatis. You can configure your cache model as follows, by example, in your
  12. * sqlMapping files:
  13. * <cacheModel id="myCache" type="nl.rabobank.springproject.ibatis.EhCacheController" readOnly="true" serialize="false">
  14. *    <property name="configFile" value="/path-to-ehcache.xml"/>
  15. * </cacheModel>
  16. * Alternatively, you can use a type alias in your type attribute and defining the class with a
  17. * <TypeAlias> declaration, see iBatis documentation on how to do this.
  18. */
  19. public class EhCacheController implements CacheController {
  20.     /** The EhCache CacheManager. */
  21.     private CacheManager cacheManager;
  22.     /**
  23.       * Flush a cache model.
  24.       * @param cacheModel - the model to flush.
  25.       */
  26.     public void flush(CacheModel cacheModel) {
  27.          getCache(cacheModel).removeAll();
  28.      }
  29.     /**
  30.       * Get an object from a cache model.
  31.       * @param cacheModel - the model.
  32.       * @param key         - the key to the object.
  33.       * @return the object if in the cache, or null(?).
  34.       */
  35.     public Object getObject(CacheModel cacheModel, Object key) {
  36.          Object result = null;
  37.          Element element = getCache(cacheModel).get(key);
  38.         if (element != null) {
  39.              result = element.getObjectValue();
  40.          }
  41.         return result;
  42.      }
  43.     /**
  44.       * Put an object into a cache model.
  45.       * @param cacheModel - the model to add the object to.
  46.       * @param key         - the key to the object.
  47.       * @param object      - the object to add.
  48.       */
  49.     public void putObject(CacheModel cacheModel, Object key, Object object) {
  50.          getCache(cacheModel).put(new Element(key, object));
  51.      }
  52.     /**
  53.       * Remove an object from a cache model.
  54.       * @param cacheModel - the model to remove the object from.
  55.       * @param key         - the key to the object.
  56.       * @return the removed object(?).
  57.       */
  58.     public Object removeObject(CacheModel cacheModel, Object key) {
  59.          Object result = this.getObject(cacheModel, key);
  60.          getCache(cacheModel).remove(key);
  61.         return result;
  62.      }
  63.     /**
  64.       * Configure a cache controller. Initialize the EH Cache Manager as a singleton.
  65.       * @param props - the properties object continaing configuration information.
  66.       */
  67.     public void setProperties(Properties props) {
  68.          URL url = getClass().getResource(props.getProperty("configFile"));
  69.          cacheManager = CacheManager.create(url);
  70.      }
  71.     /**
  72.       * Gets an EH Cache based on an iBatis cache Model.
  73.       * @param cacheModel - the cache model.
  74.       * @return the EH Cache.
  75.       */
  76.     private Cache getCache(CacheModel cacheModel) {
  77.          String cacheName = cacheModel.getId();
  78.          Cache cache = cacheManager.getCache(cacheName);
  79.         return cache;
  80.      }
  81.     /**
  82.       * Shut down the EH Cache CacheManager.
  83.       */
  84.     public void finalize() {
  85.         if (cacheManager != null) {
  86.              cacheManager.shutdown();
  87.          }
  88.      }
  89. }

接下来一个sql Mapping config的例子

  1. <!--sp-->xml version="1.0" encoding="UTF-8" ?>
  2.      PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
  3.      "http://ibatis.apache.org/dtd/sql-map-2.dtd">
  4. <sqlMap namespace="myNamespace">
  5.   <!-- Use type aliases to avoid typing the full classname every time. -->
  6.   <typeAlias alias="MyPojo" type="nl.myproject.MyPojo"/>
  7.   <typeAlias alias="MapCacheController" type="com.ibatis.sqlmap.engine.cache.EhCacheController"/>
  8.   <cacheModel id="MyPojoCache" type="MapCacheController" readOnly="true" serialize="false">
  9.     <property name="configFile" value="/ehcache.xml"/>
  10.   cacheModel>
  11.   <resultMap id="ResultQueryMap" class="MyPojo">
  12.      ...property mappings go here...
  13.   resultMap>
  14.   <select id="getMyPojoList" resultMap="ResultQueryMap" cacheModel="MyPojoCache">
  15.      ...select query to get your pojo from the database goes here...
  16.   select>
  17. sqlMap>

一个eh-cache.xml配置示例

  1. <ehcache>
  2.    ...put your default cache here...
  3.   <cache
  4.     name="myNamespace.MyPojoCache"
  5.     maxElementsInMemory="5"
  6.     eternal="false"
  7.     timeToLiveSeconds="60"
  8.     overflowToDisk="false"
  9.     memoryStoreEvictionPolicy="LRU"/>
  10. ehcache>

需要留意的是:

1.节点配置在sqlMap.xml中,需要放在classpath下.

2.你可以在sqlMap.xml中添加多个同时指向一个.如果CacheManager是单例模式,那么这将减少很多不必要的配置,但是这只是一个初级的问题.

3.必须设置sqlMapConfig.xml中节点中的cacheModelsEnabled=true,否则缓存不会被启用

分享到:
评论

相关推荐

    spring+ibatis+ehcache整合例子

    spring+ibatis+ehcache整合完整例子,数据库MySQL

    spring MVC+ibatis+ehcache开发包集合

    自己项目的开发包集合,其中包括:缓存处理ehcache相关jar,spring MVC4.0 jar,ehcache依赖jar,以及其他jar(图像处理thumbnailator-0.4.2),包虽然不是很新但可用。实际使用时找包较为麻烦,现在整理出来,希望...

    Spring 3.0+Struts2+Mybatis 3 + p6spy 平台框架

    这是自己整合的Spring 3.0+Struts2+Mybatis 3 + p6spy +ehcache的平台框架,内含一点示例代码,目前ehcache没有使用。直接编译后发布就能用 测试环境基于JDK1.6+Tomcat 6.0. 大家拿到后请根据实际情况修改 ...

    学生选课系统中所用的60个jar包

    1.6.jar easymock.jar easymockclassextension.jar ehcache-1.1.jar freemarker.jar hessian-2.1.12.jar hibernate2.jar hibernate3.jar hibernate-annotations.jar ibatis-common-2.jar ibatis-sqlmap-2.jar itext-...

    redis基础.rar

    Java中要用到缓存的地方很多,首当其冲的就是持久层缓存,针对持久层谈一下: 要实现java缓存有很多种方式,最简单的无非就是static HashMap,这个显然是基于内存缓存,一个map就可以搞定引用对象的缓存,最简单也...

    最新最全的spring开发包

    这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个jar文件是所有应用都要...

    spring jar 包详解

    (1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个jar文件是所有应用都要...

    c3p0-0.9.1.2等等

    ehcache-1.1 hibernate3 itext-1.3 jaas jakarta-oro-2.0.8 jaxen-1.1-beta-7 jdbc2_0-stdext jotm json-lib-2.3-jdk15-javadoc jta jxl log4j-1.2.11 log4j-1.2.14 portlet-api spring-agent spring-aop spring-...

    培训体系管理系统-oracle-ssh

    connector.jar cos.jar dom4j-1.6.1.jar dwr.jar ehcache-1.1.jar ehcache-1.2.3.jar FCKeditor-2.3.jar freemarker.jar hibernate3.jar itext-1.3.jar jaas.jar jacc-1_0-fr.jar jakarta-oro-2.0.8.jar ...

    javaJar包大全

    31.ehcache-1.2.3.jar 32.ewebedit.jar 33.FCKeditor-2.3.jar 34.freemarker-2.3.13.jar 35.geronimo-activation_1.1_spec-1.0.1.jar 36.geronimo-stax-api_1.0_spec-1.0.1.jar 37.hessian-3.0.20.jar 38.hibernate-...

    spring-framework-3.0.5.RELEASE-dependencies-5

    org.apache.ibatis org.apache.juli 4号包: org.apache.tiles org.apache.velocity org.apache.xerces org.apache.xml org.apache.xmlbeans org.apache.xmlcommons org.apache.derby org.apache.poi org.apache....

    spring-framework-3.0.5.RELEASE-dependencies-3

    org.apache.ibatis org.apache.juli 4号包: org.apache.tiles org.apache.velocity org.apache.xerces org.apache.xml org.apache.xmlbeans org.apache.xmlcommons org.apache.derby org.apache.poi org.apache....

    spring-framework-3.0.5.RELEASE-dependencies-6

    org.apache.ibatis org.apache.juli 4号包: org.apache.tiles org.apache.velocity org.apache.xerces org.apache.xml org.apache.xmlbeans org.apache.xmlcommons org.apache.derby org.apache.poi org.apache....

    spring-framework-3.0.5.RELEASE-dependencies-4

    org.apache.ibatis org.apache.juli 4号包: org.apache.tiles org.apache.velocity org.apache.xerces org.apache.xml org.apache.xmlbeans org.apache.xmlcommons org.apache.derby org.apache.poi org.apache....

    spring-framework-3.0.5.RELEASE-dependencies-2

    org.apache.ibatis org.apache.juli 4号包: org.apache.tiles org.apache.velocity org.apache.xerces org.apache.xml org.apache.xmlbeans org.apache.xmlcommons org.apache.derby org.apache.poi org.apache....

    spring-framework-3.0.5.RELEASE-dependencies-1

    org.apache.ibatis org.apache.juli 4号包: org.apache.tiles org.apache.velocity org.apache.xerces org.apache.xml org.apache.xmlbeans org.apache.xmlcommons org.apache.derby org.apache.poi org.apache....

    spring-framework-3.0.5.RELEASE-dependencies-8

    org.apache.ibatis org.apache.juli 4号包: org.apache.tiles org.apache.velocity org.apache.xerces org.apache.xml org.apache.xmlbeans org.apache.xmlcommons org.apache.derby org.apache.poi org.apache....

    spring-framework-3.0.5.RELEASE-dependencies-7

    org.apache.ibatis org.apache.juli 4号包: org.apache.tiles org.apache.velocity org.apache.xerces org.apache.xml org.apache.xmlbeans org.apache.xmlcommons org.apache.derby org.apache.poi org.apache....

    springmvc和mybatis集成全部jar包(全)

    springmvc4.3.3和mybatis3.4.1集成最新全部jar包,还包含了其他一些常用的jar包,很全,已经在项目中验证过。 lib/antlr-2.7.2.jar lib/aopalliance-1.0.jar lib/asm-3.3.1.jar lib/aspectjweaver-1.6.5.jar ...

Global site tag (gtag.js) - Google Analytics