`
x98zero
  • 浏览: 107791 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Ibatis.....

阅读更多
1.ibatis中使用缓存
首先设置SqlMapConfig.xml中<settings/>节点的属性cacheModelsEnabled="true"
 
  然后在具体sqlmap文件中书写<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
    <select id="getAllProducts" cacheModel="product-cache">
      select * from PRODUCT
    </statement> 

   
复杂点的用法
  <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的优势之一,安全高效
说明文字在注释中
  <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传参数
  <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>


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


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics