ibatis的一些配置

1.ibatis中使用缓存
首先设置SqlMapConfig.xml中<settings/>节点的属性cacheModelsEnabled="true"
 
  然后在具体sqlmap文件中书写<cacheModel>

Xml代码
<cacheModel id="product-cache" type="LRU">    
  <flushInterval hours="24"/>    
  <flushOnExecute statement="insertProduct"/>  
  <flushOnExecute statement="updateProduct"/>    
  <flushOnExecute statement="deleteProduct"/>    
  <property name="size" value="1000" />    
</cacheModel>  
    <cacheModel id="product-cache" type="LRU">
      <flushInterval hours="24"/>
      <flushOnExecute statement="insertProduct"/>
      <flushOnExecute statement="updateProduct"/>
      <flushOnExecute statement="deleteProduct"/>
      <property name="size" value="1000" />
    </cacheModel>
  
  最后给<select/>节点应用cache

Xml代码
<select id="getAllProducts" cacheModel="product-cache">  
  select * from PRODUCT   
</statement>   
    <select id="getAllProducts" cacheModel="product-cache">
      select * from PRODUCT
    </statement>
   
复杂点的用法

Xml代码
<cacheModel/>节点   
  type="LRU"  
    type属性可以指定cache的类型,ibatis支持3种缓存:   
      MEMORY     没有统一的对象重用模式或内存不足的应用。   
      LRU        经常使用的对象,这是性能最好的选择。   
      FIFO       在短时间内持续引用,而后很可能不再使用。   
    也可以使用外部cache如:   
      type="OSCACHE"  
      
  readOnly="true"  
    默认true时缓存效果最好,可以减少更新。   
       
  serialize="false"  
    默认false,设true可以提高整体应用的性能。   
      serialize只能应用于实现了Serializable接口的对象,而且和lazyLoadingEnabled="true"属性冲突。   
         
  flushInterval   
    自动刷新间隔时间。   
  
  flushOnExecute   
    在特定id的操作后,刷新cache,可选操作。   
  
手动刷新缓存   
  [sqlmap].flushDataCache("product-cache")   
    刷新cache当id="product-cache"  
  [sqlmap].flushDataCache()   
    刷新sqlmap内的所有cache  
  <cacheModel/>节点
    type="LRU"
      type属性可以指定cache的类型,ibatis支持3种缓存:
        MEMORY     没有统一的对象重用模式或内存不足的应用。
        LRU        经常使用的对象,这是性能最好的选择。
        FIFO       在短时间内持续引用,而后很可能不再使用。
      也可以使用外部cache如:
        type="OSCACHE"
    
    readOnly="true"
      默认true时缓存效果最好,可以减少更新。
     
    serialize="false"
      默认false,设true可以提高整体应用的性能。
        serialize只能应用于实现了Serializable接口的对象,而且和lazyLoadingEnabled="true"属性冲突。
       
    flushInterval
      自动刷新间隔时间。

    flushOnExecute
      在特定id的操作后,刷新cache,可选操作。

  手动刷新缓存
    [sqlmap].flushDataCache("product-cache")
      刷新cache当id="product-cache"
    [sqlmap].flushDataCache()
      刷新sqlmap内的所有cache

2.ibatis 拼接sql语句,动态查询
在ibatis中使用安全的拼接语句,动态查询
ibatis比JDBC的优势之一,安全高效
说明文字在注释中

Xml代码
<select id="selectAllProducts" parameterClass="Product" resultMap="ProductResult">  
  select id,note from Product   
     <dynamic prepend="WHERE">  
     <!-- isNotNull判断参数是否存在,Integer类型 -->  
          <isNotNull property="id">  
              <!-- isGreaterThan判断参数是否大于compareValue,isGreaterEquals是大于等于 -->  
              <isGreaterThan prepend=" and " property="id" compareValue="0">  
              id = #id#   
              </isGreaterThan>  
          </isNotNull>  
          <!-- isNotEmpty判断字串不为空,isEmpty可以判断字串为空 -->  
          <isNotEmpty prepend=" and " property="note">  
          <!-- 模糊查询不能用#,#在是用prepareStatement的?插入参数,$是文本替换 -->  
          note like '%$note$%'   
          </isNotEmpty>  
      </dynamic>  
</select>  
  <select id="selectAllProducts" parameterClass="Product" resultMap="ProductResult">
    select id,note from Product
       <dynamic prepend="WHERE">
       <!-- isNotNull判断参数是否存在,Integer类型 -->
            <isNotNull property="id">
                <!-- isGreaterThan判断参数是否大于compareValue,isGreaterEquals是大于等于 -->
                <isGreaterThan prepend=" and " property="id" compareValue="0">
                id = #id#
                </isGreaterThan>
            </isNotNull>
            <!-- isNotEmpty判断字串不为空,isEmpty可以判断字串为空 -->
            <isNotEmpty prepend=" and " property="note">
            <!-- 模糊查询不能用#,#在是用prepareStatement的?插入参数,$是文本替换 -->
            note like '%$note$%'
            </isNotEmpty>
        </dynamic>
  </select>
 
用Map传参数

Xml代码
<select id="selectAllProducts" parameterClass="java.util.HashMap" resultMap="ProductResult">  
  select id,note from Product   
     <dynamic prepend="WHERE">  
     <!-- isPropertyAvailable判断属性是否有效 -->  
        <isPropertyAvailable property="id">  
          <isNotNull property="id">  
              <!-- isLessThan判断参数是否小于compareValue,isLessEquals是小于等于 -->  
              <isLessThan prepend=" and " property="id" compareValue="10">  
              id = #id#   
              </isLessThan>  
          </isNotNull>  
        </isPropertyAvailable>  
      </dynamic>  
</select>  
  <select id="selectAllProducts" parameterClass="java.util.HashMap" resultMap="ProductResult">
    select id,note from Product
       <dynamic prepend="WHERE">
       <!-- isPropertyAvailable判断属性是否有效 -->
          <isPropertyAvailable property="id">
            <isNotNull property="id">
                <!-- isLessThan判断参数是否小于compareValue,isLessEquals是小于等于 -->
                <isLessThan prepend=" and " property="id" compareValue="10">
                id = #id#
                </isLessThan>
            </isNotNull>
          </isPropertyAvailable>
        </dynamic>
  </select>

---------------------------------几个常用属性----------------------------------

Xml代码
<isPropertyAvailable> 属性是存在   
<isNotPropertyAvailable> 属性不存在   
<isNull> 属性值是null   
<isEmpty> 判断Collection.size<1或String.length()<1  
<isEqual> 等于   
<isNotEqual> 不等于   
<isGreaterThan> 大于   
<isGreaterEqual> 大于等于   
<isLessThan> 小于   
<isLessEqual> 小于等于  


原文来自:雨枫技术教程网 http://www.fengfly.com
原文网址:http://www.fengfly.com/plus/view-37415-1.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值