zoukankan      html  css  js  c++  java
  • ibatis缓存初探(1)

    一,IBATIS 缓存机制使用

    1,sqlMapConfig.xml中配置

     
    1.SqlMapConfig.xml中
    <settings
    cacheModelsEnabled="true" //设置为true
    enhancementEnabled="true"
    lazyLoadingEnabled="true"
    .............
    /> 

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

    Xml代码

     

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

    2,配置具体的sqlMap.xml

    Xml代码
    <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" />

  • 相关阅读:
    用php爬取网页
    无论我是一只菜鸟笨鸟
    有线网卡与无线网卡同时使用
    scapy 命令理解
    Wireshark Filter
    python OS/pdb 模块及数据类型基础
    scapy down and install
    python 字符操作函数
    python 类型集
    python 科学计算
  • 原文地址:https://www.cnblogs.com/wcyBlog/p/3926831.html
Copyright © 2011-2022 走看看