- 浏览: 4381390 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (163)
- 职场 && 心情 (22)
- Java/Basic (17)
- Java/Compression (7)
- Java/Security (20)
- Java/Maven (3)
- Java/Cache (11)
- Eclipse (4)
- Spring (19)
- ORM/Hibernate (2)
- ORM/iBatis (3)
- DB/NoSQL (11)
- DB/MySQL (7)
- DB/MS SQL Server (4)
- OS/Linux (11)
- OS/Mac (7)
- C/C++ (4)
- Server Architecture/Basic (13)
- Server Architecture/Distributed (17)
- Moblie/Andriod (2)
- WebService (3)
- Objective-C (1)
- Html (1)
- 设计模式 (1)
- Scala (0)
- Kafka (1)
最新评论
-
w47_csdn:
证书安装:在"浏览"选项中选择" ...
Java加密技术(九)——初探SSL -
w47_csdn:
spiritfrog 写道你好,我按照你的步骤,tomcat中 ...
Java加密技术(九)——初探SSL -
liuyachao111:
11楼说的对 用@ControllerAdvicepublic ...
Spring 注解学习手札(八)补遗——@ExceptionHandler -
irayslu:
作者你好, 我把你的源码放在jdk6, jdk7 中运行正常, ...
Java加密技术(五)——非对称加密算法的由来DH -
夏季浅忆-卖小子:
为什么不能解压rar格式的压缩包呢
Java压缩技术(三) ZIP解压缩——Java原生实现
项目完结,整理一些技术方面的相关收获。
已经记不得EhCacheController这个实现类最早来自于那里了,总之稍加修改后非常有效果,大家就这么用了,感谢最初开源的那位兄弟。这里,主要是做个记录,为以后类似扩展(譬如Memcached)做个准备。
iBatis提供CacheController接口,用于实现第三方缓存架构的扩展。
这里以iBatis 2.3.0,EhCache 1.2.3版本为基础,构建iBatis+EhCache实现。
EhCacheController类:
这里默认在根目录下获取ehcache.xml文件,可以通过cacheModel配置进行修改。
在SqlMapConfig.xml文件中配置一个别名,作为全局变量,供其余SqlMap使用。
顺便提一句,要使用缓存注意SqlMapConfig.xml文件中settings节点配置cacheModelsEnabled为true!
接下来,在SqlMap.xml文件中的cacheModel中
如果要变更ehcache.xml文件路径为/config/ehcache.xml,可以在上述节点中下入如下代码:
然后,就可以通过ehcache.xml控制ehcache缓存了!
举例说明iBatis SqlMap & ehcahce.xml,以免有些兄弟混淆!
以Account类为示例,在SqlMap中的配置为:
注意:
<select
id="readByAccountName"
parameterClass="string"
resultMap="accountMap"
cacheModel="cache">
这里的cacheModel="cache",对应的在ehcache.xml中就应该是:
因为,我使用了useStatementNamespaces="true"
自己开发的EhCacheController,非自带。。。。
已经记不得EhCacheController这个实现类最早来自于那里了,总之稍加修改后非常有效果,大家就这么用了,感谢最初开源的那位兄弟。这里,主要是做个记录,为以后类似扩展(譬如Memcached)做个准备。
iBatis提供CacheController接口,用于实现第三方缓存架构的扩展。
这里以iBatis 2.3.0,EhCache 1.2.3版本为基础,构建iBatis+EhCache实现。
EhCacheController类:
package com.ibatis.sqlmap.engine.cache.ehcache; 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: * * <pre> * <code> * <cacheModel id="myCache" readOnly="true" serialize="false" * type="com.ibatis.sqlmap.engine.cache.EhCacheController" > * <property name="configLocation" * value="/path-to-ehcache.xml"/> * </cacheModel> </code> * </pre> * * Alternatively, you can use a type alias in your type attribute and defining * the class with a <code><typeAlias></code> declaration: * * <pre> * <code> * <sqlMapConfig> * <typeAlias alias="EHCACHE" * type="com.ibatis.sqlmap.engine.cache.ehcache.EhCacheController" /> * </sqlMapConfig> * </code> * </pre> * */ public class EhCacheController implements CacheController { /** * The EhCache CacheManager. */ private CacheManager cacheManager; public static final String CONFIG_LOCATION = "configLocation"; /** * Default Configure Location */ public static final String DEFAULT_CONFIG_LOCATION = "/ehcache.xml"; /** * 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; } /** * 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(); } } /** * Configure a cache controller. Initialize the EH Cache Manager as a * singleton. * * @param props * - the properties object continaing configuration information. */ @Override public void configure(Properties props) { String configLocation = props.getProperty(CONFIG_LOCATION); // if can not found ehcache.xml from configLocaion, // use default configure file. if (configLocation == null) { configLocation = DEFAULT_CONFIG_LOCATION; } URL url = getClass().getResource(configLocation); cacheManager = CacheManager.create(url); } }
这里默认在根目录下获取ehcache.xml文件,可以通过cacheModel配置进行修改。
在SqlMapConfig.xml文件中配置一个别名,作为全局变量,供其余SqlMap使用。
<typeAlias alias="EHCACHE" type="com.ibatis.sqlmap.engine.cache.ehcache.EhCacheController" />
顺便提一句,要使用缓存注意SqlMapConfig.xml文件中settings节点配置cacheModelsEnabled为true!
<settings cacheModelsEnabled="true" useStatementNamespaces="true" ... />
接下来,在SqlMap.xml文件中的cacheModel中
<cacheModel id="cache" type="EHCACHE"> ... </cacheModel>
如果要变更ehcache.xml文件路径为/config/ehcache.xml,可以在上述节点中下入如下代码:
<property name="configLocation" value="/config/ehcache.xml" />
然后,就可以通过ehcache.xml控制ehcache缓存了!
举例说明iBatis SqlMap & ehcahce.xml,以免有些兄弟混淆!
以Account类为示例,在SqlMap中的配置为:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="Account"> <cacheModel id="cache" type="EHCACHE"> <flushInterval hours="1" /> <!-- flush操作,需要指明 Namespace --> <flushOnExecute statement="Account.create" /> </cacheModel> <typeAlias alias="account" type="org.zlex.acl.Account" /> <resultMap id="accountMap" class="account"> <result property="accountId" column="accountId" /> <result property="accountName" column="accountName" /> <result property="mail" column="mail" /> <result property="realName" column="realName" /> <result property="status" column="status" /> <result property="lastLoginTime" column="lastLoginTime" /> </resultMap> <select id="readByAccountName" parameterClass="string" resultMap="accountMap" cacheModel="cache"> <![CDATA[ SELECT accountId, accountName, mail, realName, status, lastLoginTime FROM acl_account WHERE accountName = #accountName# ]]> </select> <insert id="create" parameterClass="account"> <![CDATA[ INSERT INTO acl_account( accountName, mail, realName, status, lastLoginTime ) VALUES ( #accountName#, #mail#, #realName#, #status#, #lastLoginTime# ) ]]> <selectKey resultClass="long" keyProperty="accountId"> <![CDATA[select LAST_INSERT_ID() as id ]]> </selectKey> </insert> </sqlMap>
注意:
引用
<select
id="readByAccountName"
parameterClass="string"
resultMap="accountMap"
cacheModel="cache">
这里的cacheModel="cache",对应的在ehcache.xml中就应该是:
<cache name="Account.cache" maxElementsInMemory="10000" eternal="false" maxElementsOnDisk="1000" overflowToDisk="true" timeToIdleSeconds="300" />
因为,我使用了useStatementNamespaces="true"
评论
3 楼
snowolf
2012-10-15
fanfeiyang 写道
哥们,你这是ibatis自带的高速缓存吧,说实话这种配置用着很方便但是随着配置文件的增加,就会很容易漏写一些清空缓存的配置
自己开发的EhCacheController,非自带。。。。
2 楼
fanfeiyang
2012-10-15
哥们,你这是ibatis自带的高速缓存吧,说实话这种配置用着很方便但是随着配置文件的增加,就会很容易漏写一些清空缓存的配置
1 楼
programdolt
2012-04-10
附件在哪里?
发表评论
-
Memcached笔记——(四)应对高并发攻击
2012-09-13 09:48 28926近半个月过得很痛苦,主要是产品上线后,引来无数机器用户恶意 ... -
征服 Redis + Jedis + Spring (二)—— 哈希表操作(HMGET HMSET)
2012-08-29 18:29 82313不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然 ... -
征服 Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL)
2012-08-29 16:30 157451有日子没写博客了,真的是忙得要疯掉。 完成项目基础架构搭建 ... -
征服 Redis + Jedis
2012-08-15 17:49 83410用Memcached,对于缓存对象大小有要求,单个对象不得 ... -
Linux环境小问题——Get HostName Error
2012-08-06 12:55 7388之前使用mysql的时候,在配置文件中,已经指明了ip访问,但 ... -
Memcached笔记——(三)Memcached使用总结
2012-07-04 17:06 44542为了将N个前端数据同步 ... -
Memcached笔记——(二)XMemcached&Spring集成
2012-04-01 09:55 42318今天研究Memcached的Java的Client,使用XMe ... -
Memcached笔记——(一)安装&常规错误&监控
2012-03-09 14:27 3547408年的时候接触过Memcached,当时还对它的客户端产品嗤 ... -
memcached整合ibatis
2012-03-08 09:10 0ibatis自带的本地缓存有FIFO,LRU等,对于分布式缓存 ... -
iBatis整理——Spring环境下批处理实现
2010-09-01 15:56 21069最近做一个小项目,用到Spring+iBatis。突然遇到一个 ... -
Ehcache简单应用——RSSReaderTag
2010-08-04 15:00 3316关于缓存,关于自己运用,大部分情况是通过ORM配置生效。基本上 ... -
iBastis null
2010-01-19 18:15 0最近一年多的时间都是和遗留系统打交道,处理一些诡异问题。不曾料 ... -
iBatis自动生成的主键 (Oracle,MS Sql Server,MySQL)
2008-07-16 22:10 9899iBatis的sqlMap配置文件的selectKey元素有个 ... -
OSCache使用指南
2008-07-09 13:41 6330OSCache是当前运用最广的缓存方案,JBoss,Hiber ...
相关推荐
spring+ibatis+ehcache整合完整例子,数据库MySQL
自己项目的开发包集合,其中包括:缓存处理ehcache相关jar,spring MVC4.0 jar,ehcache依赖jar,以及其他jar(图像处理thumbnailator-0.4.2),包虽然不是很新但可用。实际使用时找包较为麻烦,现在整理出来,希望...
MyBatis生成器(MBG)是MyBatis MyBatis 和iBATIS的代码生成器。它将为MyBatis的所有版本以及版本2.2.0之后的iBATIS生成代码。它将内省一个数据库表(或多个表),并将生成可用于访问表的工件。这减轻了设置对象和...
最近想在最新的Spring5.0中集成ibatis(不是mybatis),发现已经不在支持SqlmapClientTemplate和SqlmapClientFactoryBean,于是搞了这个工具jar来进行支持如下配置 ...
ibatis资料整理.zip ibatis 整理 zip 入门 java 个人学习收集网上资料辛苦整理而成 制作chm老是报错,只好压缩上传了。
在多数情况下不及特定数据库支持的物理分页,而hibernate的分页则是直接组装sql,充分利用了特定数据库的分页机制,效率相 对较高。本文讲述的就是如何在不重新编译ibatis源码的前提下,为ibatis引入hibernate式的...
NULL 博文链接:https://foxpro.iteye.com/blog/435001
ibatis学习 ibatis总结 ibatis ibatis ibatis
MyEclipse上自动生成dao和实体的插件——ibatis
Spring与iBATIS的集成 iBATIS似乎已远离众说纷纭的OR框架之列,通常人们对非常流行的Hibernate情有独钟。但正如Spring A Developer's Notebook作者Bruce Tate 和Justin Gehtland所说的那样,与其他的OR框架相比...
Spring MVC Ibatis Bean 根据mysql数据表——代码生成工具
ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料
公司的大部分项目都开始使用IBatis作为O/R Mapping了,但是在使用的过程中也发现了很多不方便和存在...想了很多办法,都没能很好的避免这个问题,无意间在javaeye上看到了《使ibatis支持hibernate式的物理分页》这篇文章,
ibatis demo,ibatis例子,ibatis示例
iBATIS 级联iBATIS 级联iBATIS 级联
ibatis的原码 ibatis源码 ibatis源码 ibatis源码
用ssi——struts2+spring+ibatis编写,含(登入+增删改查)等功能,适合初学者学习
Ibatis3,手册,Ibatis3手册,Ibatis3使用手册,Ibatis3参考手册
ibatis