`
xiexd
  • 浏览: 245260 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ibatis 缓存机制初探(转)

    博客分类:
  • JAVA
阅读更多


一,IBATIS 缓存机制使用

 

1,sqlMapConfig.xml中配置

Xml代码
  1. 1.SqlMapConfig.xml中  
  2. < settings   
  3. cacheModelsEnabled = "true"  //设置为true  
  4. enhancementEnabled = "true"   
  5. lazyLoadingEnabled = "true"   
  6. .............  
  7. />    
1.SqlMapConfig.xml中
<settings
cacheModelsEnabled="true" //设置为true
enhancementEnabled="true"
lazyLoadingEnabled="true"
.............
/> 

 注意点,k,必须配置settings这个标签,否则

 

Xml代码
  1. cacheModelsEnabled = "false"    
cacheModelsEnabled="false" 

 

Xml代码
  1. cacheModelsEnabled = "true"   是默认的,无须配置也可,建议配置  
cacheModelsEnabled="true"  是默认的,无须配置也可,建议配置

 

2,配置具体的sqlMap.xml

 

Xml代码
  1. < cacheModel   id = "baby-cache"   type = "LRU"   readOnly = "false"   
  2.         serialize = "true" >   
  3.         < flushInterval   hours = "24"   />   
  4.         < flushOnExecute   statement = "getBabyByParams"   />   
  5.         < property   value = "600"   name = "size"   />   
  6.     </ cacheModel >   
  7.   
  8.   
  9. < select   id = "getBabyByParams"   resultMap = "baby-Result"   cacheModel = "baby-cache" >   
  10.         select * from Baby  
  11.         < dynamic   prepend = "where" >   
  12.             < isPropertyAvailable   property = "name"   prepend = "and" >   
  13.                 name  = #name#  
  14.             </ isPropertyAvailable >   
  15.             < isPropertyAvailable   property = "sex"   prepend = "and" >   
  16.                 sex  =  
  17.                 #sex,jdbcType = VARCHAR , javaType = com .yajun.enumdemo.SexEnum#  
  18.             </ isPropertyAvailable >   
  19.             < isPropertyAvailable   property = "BirthdayBondStart"   
  20.                 prepend = "and" >    
  21.                <![CDATA[  
  22.                      birthday >= cast(#BirthdayBondStart# as datetime)  
  23.                ]]>   
  24.             </ isPropertyAvailable >   
  25.             < isPropertyAvailable   property = "BirthdayBondEnd"   
  26.                 prepend = "and" >   
  27.                  <![CDATA[  
  28.                      birthday <= cast(#BirthdayBondEnd# as datetime)  
  29.                  ]]>   
  30.             </ isPropertyAvailable >   
  31.             < isPropertyAvailable   property = "hobby"   prepend = "and" >   
  32.                 hobby like '%'||#hobby#||'%'  
  33.             </ isPropertyAvailable >   
  34.         </ dynamic >   
  35.     </ select >   
<cacheModel id="baby-cache" type="LRU" readOnly="false"
		serialize="true">
		<flushInterval hours="24" />
		<flushOnExecute statement="getBabyByParams" />
		<property value="600" name="size" />
	</cacheModel>


<select id="getBabyByParams" resultMap="baby-Result" cacheModel="baby-cache">
		select * from Baby
		<dynamic prepend="where">
			<isPropertyAvailable property="name" prepend="and">
				name = #name#
		   	</isPropertyAvailable>
			<isPropertyAvailable property="sex" prepend="and">
				sex =
				#sex,jdbcType=VARCHAR,javaType=com.yajun.enumdemo.SexEnum#
			</isPropertyAvailable>
			<isPropertyAvailable property="BirthdayBondStart"
				prepend="and"> 
		   	   <![CDATA[
		   	         birthday >= cast(#BirthdayBondStart# as datetime)
		   	   ]]>
			</isPropertyAvailable>
			<isPropertyAvailable property="BirthdayBondEnd"
				prepend="and">
				 <![CDATA[
				     birthday <= cast(#BirthdayBondEnd# as datetime)
				 ]]>
			</isPropertyAvailable>
			<isPropertyAvailable property="hobby" prepend="and">
				hobby like '%'||#hobby#||'%'
			</isPropertyAvailable>
		</dynamic>
	</select>
 

二,具体配置项

 

id : cacheModel的id.

type : cache的类型. ibatis目前提供了LRU,MEMORY,FIFO,OSCACHE这四种.

  •       FIFO: com.ibatis.sqlmap.engine.cache.fifo.FifoCacheController
  •       LRU:  com.ibatis.sqlmap.engine.cache.fifo.LruCacheController
  •       MEMORY: com.ibatis.sqlmap.engine.cache.fifo.MemoryCacheController
  •       OSCACHE: com.ibatis.sqlmap.engine.cache.fifo.OSCacheController



      当然,你也可以自己来实现Cache, 你需要做的是让你的Cache类 implements com.ibatis.sqlmap.engine.cache.CacheController.

readOnly : 是否只读. 默认为true, 只读.

serialize : 是否从Cache中读取同一个对象,还是对象的副本.
           只有在readOnly=false才有效.
   因为Cache是只读的,那么为不同session返回的对象肯定是一个.
   只有在Cache是可读写的时候,才需要为每个session返回对象的副本.

flushInterval : Cache刷新间隔. 可以配置hours,minutes,seconds,milliseconds.
           注: 不是说,间隔时间到了,在Cache的statement会自己刷新,而是说,在间隔时间过了后,下次的查询
   将不会从Cache中直接去值,而会用SQL去查.也就是: 如果,间隔时间过了,还没有Cache对应的statement执行
   的话,那么Cache中就会一直是旧的,不用担心Cache数据是旧的,因为下次的查询将会直接从SQL查询,而非Cache,查询的结果也会去更新Cache的值.

flushOnExecute : 当这些statement被执行了,那么下次的查询将会通过SQL去查,同时用查询结果更新Cache.
           注: 和flushInterval的刷新一样,不是主动刷新,而是由下次查询来触发被动刷新.
               在一个cacheModel中可以指定多个flushOnExecute.

property : 这是针对cacheModel的额外的一些属性配置.不同type的cacheModel将会有自己专有的一些property配置.
          FIFO: <property name="size" value="100" />
          LRU: <property name="cache-size" value="100" />
  MEMORY: <property name="reference-type" value="WEAK" />

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics