需要的朋友直接去官网吧!
http://code.google.com/p/ibatis-with-memcached/
在ibatis源码基础上修改,增加对memcached支持,通过配置IBatis的xml文件即可实现memcached细粒度话缓存,使用简单,缓存效果好。 spring下首先初始化MemcachedManager对象,或者通过程序初始化也一样,不要用ibatis官方的jar包,否则会冲突
<bean class="com.ibatis.sqlmap.engine.cache.memcached.memcachedManager" lazy-init="false" init-method="init" destroy-method="closePool"> <property name="serverlist"> <value> 192.168.0.1:11111, 192.168.0.2:11111 </value> </property> <property name="initConn" value="5"> </property> <property name="minConn" value="5"> </property> <property name="maxConn" value="200"> </property> Unknown end tag for </bean>
然后配置sqlMapConfig.xml文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
" http://ibatis-with-memcached.googlecode.com/files/sql-map-config-2.dtd"> #注意这里,不用官方的,这个dtd文件加了个新属性databaseUrl,区分不同数据库的缓存对象
<sqlmapconfig> <properties resource="cache_config.properties"> </properties> <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="256" maxSessions="256" maxTransactions="150" useStatementNamespaces="true" databaseUrl="数据库名或地址" #新增加的属性 /> ibatis的xml文件:Albums.xml #创建缓存model <cachemodel type="MEMCACHED" id="albumsCache"> <flushinterval hours="12"> </flushInterval > <flushonexecute statement="albums.save"> </flushOnExecute > <flushonexecute statement="albums.update"> </flushOnExecute > <flushonexecute statement="albums.delete"> </flushOnExecute > <property name="pk" value="id"> </property> #可以根据主键进行缓存,可以设置为空,不能不设 <property name="groupField" value="accId"> </property> #可以根据组(比如用户id)进行缓存,更加细粒度化,可以设置为空,不能不设 Unknown end tag for </cachemodel> #加入缓存 <select id="findall" parameterClass="albumsObj" resultClass="albumsObj" cacheModel="albumsCache"> Select ……from albums where accId=1 </select> <select id="load" parameterClass="albumsObj" resultClass="albumsObj" cacheModel="albumsCache"> Select ……from albums where id=1 </select> #删除对象,删除缓存 <delete id="delete" parameterClass="albumsObj"> delete from albums where id=1 and accId=1 #(加上accId可以删除分组缓存) </delete>
Chatopera博客 聊天机器人 机器学习 智能客服 北京华夏春松科技有限公司,为企业交付智能客服系统、智能对话机器人、机器人客服、Chatbot。https://www.chatopera.com