需要实现CacheController接口并通过一系列的配置委托iBATIS使用EHCache进行缓存.
-
package com.ibatis.sqlmap.engine.cache.EhCacheController;
-
import java.net.URL;
-
import java.util.Properties;
-
import net.sf.ehcache.Cache;
-
import net.sf.ehcache.CacheManager;
-
import net.sf.ehcache.Element;
-
import com.ibatis.sqlmap.engine.cache.CacheController;
-
import com.ibatis.sqlmap.engine.cache.CacheModel;
-
-
* EhCache Implementation of the {@link com.ibatis.sqlmap.engine.cache.CacheController} interface to be able to use
-
* EhCache as a cache implementation in iBatis. You can configure your cache model as follows, by example, in your
-
* sqlMapping files:
-
* <cacheModel id="myCache" type="nl.rabobank.springproject.ibatis.EhCacheController" readOnly="true" serialize="false">
-
* <property name="configFile" value="/path-to-ehcache.xml"/>
-
* </cacheModel>
-
* Alternatively, you can use a type alias in your type attribute and defining the class with a
-
* <TypeAlias> declaration, see iBatis documentation on how to do this.
-
*/
-
public class EhCacheController implements CacheController {
-
-
private CacheManager cacheManager;
-
-
* Flush a cache model.
-
* @param cacheModel - the model to flush.
-
*/
-
public void flush(CacheModel cacheModel) {
-
getCache(cacheModel).removeAll();
-
}
-
-
* Get an object from a cache model.
-
* @param cacheModel - the model.
-
* @param key - the key to the object.
-
* @return the object if in the cache, or null(?).
-
*/
-
public Object getObject(CacheModel cacheModel, Object key) {
-
Object result = null;
-
Element element = getCache(cacheModel).get(key);
-
if (element != null) {
-
result = element.getObjectValue();
-
}
-
return result;
-
}
-
-
* Put an object into a cache model.
-
* @param cacheModel - the model to add the object to.
-
* @param key - the key to the object.
-
* @param object - the object to add.
-
*/
-
public void putObject(CacheModel cacheModel, Object key, Object object) {
-
getCache(cacheModel).put(new Element(key, object));
-
}
-
-
* Remove an object from a cache model.
-
* @param cacheModel - the model to remove the object from.
-
* @param key - the key to the object.
-
* @return the removed object(?).
-
*/
-
public Object removeObject(CacheModel cacheModel, Object key) {
-
Object result = this.getObject(cacheModel, key);
-
getCache(cacheModel).remove(key);
-
return result;
-
}
-
-
* Configure a cache controller. Initialize the EH Cache Manager as a singleton.
-
* @param props - the properties object continaing configuration information.
-
*/
-
public void setProperties(Properties props) {
-
URL url = getClass().getResource(props.getProperty("configFile"));
-
cacheManager = CacheManager.create(url);
-
}
-
-
* Gets an EH Cache based on an iBatis cache Model.
-
* @param cacheModel - the cache model.
-
* @return the EH Cache.
-
*/
-
private Cache getCache(CacheModel cacheModel) {
-
String cacheName = cacheModel.getId();
-
Cache cache = cacheManager.getCache(cacheName);
-
return cache;
-
}
-
-
* Shut down the EH Cache CacheManager.
-
*/
-
public void finalize() {
-
if (cacheManager != null) {
-
cacheManager.shutdown();
-
}
-
}
-
}
package com.ibatis.sqlmap.engine.cache.EhCacheController;import java.net.URL;import java.util.Properties;import net.sf.ehcache.Cache;import net.sf.ehcache.CacheManager;import net.sf.ehcache.Element;import com.ibatis.sqlmap.engine.cache.CacheController;import com.ibatis.sqlmap.engine.cache.CacheModel;/** * EhCache Implementation of the {@link com.ibatis.sqlmap.engine.cache.CacheController} interface to be able to use * EhCache as a cache implementation in iBatis. You can configure your cache model as follows, by example, in your * sqlMapping files: * <cacheModel id="myCache" type="nl.rabobank.springproject.ibatis.EhCacheController" readOnly="true" serialize="false"> * <property name="configFile" value="/path-to-ehcache.xml"/> * </cacheModel> * Alternatively, you can use a type alias in your type attribute and defining the class with a * <TypeAlias> declaration, see iBatis documentation on how to do this. */public class EhCacheController implements CacheController { /** The EhCache CacheManager. */ private CacheManager cacheManager; /** * Flush a cache model. * @param cacheModel - the model to flush. */ public void flush(CacheModel cacheModel) { getCache(cacheModel).removeAll(); } /** * Get an object from a cache model. * @param cacheModel - the model. * @param key - the key to the object. * @return the object if in the cache, or null(?). */ public Object getObject(CacheModel cacheModel, Object key) { Object result = null; Element element = getCache(cacheModel).get(key); if (element != null) { result = element.getObjectValue(); } return result; } /** * Put an object into a cache model. * @param cacheModel - the model to add the object to. * @param key - the key to the object. * @param object - the object to add. */ public void putObject(CacheModel cacheModel, Object key, Object object) { getCache(cacheModel).put(new Element(key, object)); } /** * Remove an object from a cache model. * @param cacheModel - the model to remove the object from. * @param key - the key to the object. * @return the removed object(?). */ public Object removeObject(CacheModel cacheModel, Object key) { Object result = this.getObject(cacheModel, key); getCache(cacheModel).remove(key); return result; } /** * Configure a cache controller. Initialize the EH Cache Manager as a singleton. * @param props - the properties object continaing configuration information. */ public void setProperties(Properties props) { URL url = getClass().getResource(props.getProperty("configFile")); cacheManager = CacheManager.create(url); } /** * Gets an EH Cache based on an iBatis cache Model. * @param cacheModel - the cache model. * @return the EH Cache. */ private Cache getCache(CacheModel cacheModel) { String cacheName = cacheModel.getId(); Cache cache = cacheManager.getCache(cacheName); return cache; } /** * Shut down the EH Cache CacheManager. */ public void finalize() { if (cacheManager != null) { cacheManager.shutdown(); } }}
接下来一个sql Mapping config的例子
- <!--sp-->xml version="1.0" encoding="UTF-8" ?>
-
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
-
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
-
<sqlMap namespace="myNamespace">
-
-
<typeAlias alias="MyPojo" type="nl.myproject.MyPojo"/>
-
<typeAlias alias="MapCacheController" type="com.ibatis.sqlmap.engine.cache.EhCacheController"/>
-
<cacheModel id="MyPojoCache" type="MapCacheController" readOnly="true" serialize="false">
-
<property name="configFile" value="/ehcache.xml"/>
- cacheModel>
-
<resultMap id="ResultQueryMap" class="MyPojo">
-
...property mappings go here...
- resultMap>
-
<select id="getMyPojoList" resultMap="ResultQueryMap" cacheModel="MyPojoCache">
-
...select query to get your pojo from the database goes here...
- select>
- sqlMap>
<!--l version="1.0" encoding="UTF-8"--> <!-- Use type aliases to avoid typing the full classname every time. -->
...property mappings go here... ...select query to get your pojo from the database goes here...
一个eh-cache.xml配置示例
-
<ehcache>
-
...put your default cache here...
-
<cache
-
name="myNamespace.MyPojoCache"
-
maxElementsInMemory="5"
-
eternal="false"
-
timeToLiveSeconds="60"
-
overflowToDisk="false"
-
memoryStoreEvictionPolicy="LRU"/>
- ehcache>
...put your default cache here...
需要留意的是:
1.节点配置在sqlMap.xml中,需要放在classpath下.
2.你可以在sqlMap.xml中添加多个同时指向一个.如果CacheManager是单例模式,那么这将减少很多不必要的配置,但是这只是一个初级的问题.
3.必须设置sqlMapConfig.xml中节点中的cacheModelsEnabled=true,否则缓存不会被启用
分享到:
相关推荐
spring+ibatis+ehcache整合完整例子,数据库MySQL
自己项目的开发包集合,其中包括:缓存处理ehcache相关jar,spring MVC4.0 jar,ehcache依赖jar,以及其他jar(图像处理thumbnailator-0.4.2),包虽然不是很新但可用。实际使用时找包较为麻烦,现在整理出来,希望...
这是自己整合的Spring 3.0+Struts2+Mybatis 3 + p6spy +ehcache的平台框架,内含一点示例代码,目前ehcache没有使用。直接编译后发布就能用 测试环境基于JDK1.6+Tomcat 6.0. 大家拿到后请根据实际情况修改 ...
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-...
Java中要用到缓存的地方很多,首当其冲的就是持久层缓存,针对持久层谈一下: 要实现java缓存有很多种方式,最简单的无非就是static HashMap,这个显然是基于内存缓存,一个map就可以搞定引用对象的缓存,最简单也...
这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个jar文件是所有应用都要...
(1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...
这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个jar文件是所有应用都要...
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-...
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 ...
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-...
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....
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....
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....
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....
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....
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....
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....
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....
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 ...