JAVA—咖啡館

          ——歡迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術(shù),交流工作經(jīng)驗,分享JAVA帶來的快樂!本網(wǎng)站部分轉(zhuǎn)載文章,如果有版權(quán)問題請與我聯(lián)系。

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks


          1.ibatis中使用緩存
          首先設(shè)置SqlMapConfig.xml中<settings/>節(jié)點的屬性cacheModelsEnabled="true"
           
            然后在具體sqlmap文件中書寫<cacheModel>

          Xml代碼 

          1<cacheModel id="product-cache" type="LRU">     
          2  <flushInterval hours="24"/>     
          3  <flushOnExecute statement="insertProduct"/>   
          4  <flushOnExecute statement="updateProduct"/>     
          5  <flushOnExecute statement="deleteProduct"/>     
          6  <property name="size" value="1000" />     
          7</cacheModel>  


            最后給<select/>節(jié)點應(yīng)用cache

          Xml代碼 

          1<select id="getAllProducts" cacheModel="product-cache">   
          2  select * from PRODUCT    
          3</statement>    


          復(fù)雜點的用法

          Xml代碼 

           1<cacheModel/>節(jié)點    
           2  type="LRU"   
           3    type屬性可以指定cache的類型,ibatis支持3種緩存:    
           4      MEMORY     沒有統(tǒng)一的對象重用模式或內(nèi)存不足的應(yīng)用。    
           5      LRU        經(jīng)常使用的對象,這是性能最好的選擇。    
           6      FIFO       在短時間內(nèi)持續(xù)引用,而后很可能不再使用。    
           7    也可以使用外部cache如:    
           8      type="OSCACHE"   
           9       
          10  readOnly="true"   
          11    默認(rèn)true時緩存效果最好,可以減少更新。    
          12        
          13  serialize="false"   
          14    默認(rèn)false,設(shè)true可以提高整體應(yīng)用的性能。    
          15      serialize只能應(yīng)用于實現(xiàn)了Serializable接口的對象,而且和lazyLoadingEnabled="true"屬性沖突。    
          16          
          17  flushInterval    
          18    自動刷新間隔時間。    
          19   
          20  flushOnExecute    
          21    在特定id的操作后,刷新cache,可選操作。    
          22   
          23手動刷新緩存    
          24  [sqlmap].flushDataCache("product-cache")    
          25    刷新cache當(dāng)id="product-cache"   
          26  [sqlmap].flushDataCache()    
          27    刷新sqlmap內(nèi)的所有cache   
          28


          2.ibatis 拼接sql語句,動態(tài)查詢
          在ibatis中使用安全的拼接語句,動態(tài)查詢
          ibatis比JDBC的優(yōu)勢之一,安全高效
          說明文字在注釋中

          Xml代碼 

           1<select id="selectAllProducts" parameterClass="Product" resultMap="ProductResult">   
           2  select id,note from Product    
           3     <dynamic prepend="WHERE">   
           4     <!-- isNotNull判斷參數(shù)是否存在,Integer類型 -->   
           5          <isNotNull property="id">   
           6              <!-- isGreaterThan判斷參數(shù)是否大于compareValue,isGreaterEquals是大于等于 -->   
           7              <isGreaterThan prepend=" and " property="id" compareValue="0">   
           8              id = #id#    
           9              </isGreaterThan>   
          10          </isNotNull>   
          11          <!-- isNotEmpty判斷字串不為空,isEmpty可以判斷字串為空 -->   
          12          <isNotEmpty prepend=" and " property="note">   
          13          <!-- 模糊查詢不能用#,#在是用prepareStatement的?插入?yún)?shù),$是文本替換 -->   
          14          note like '%$note$%'    
          15          </isNotEmpty>   
          16      </dynamic>   
          17</select>   


          用Map傳參數(shù)

          Xml代碼 

           1<select id="selectAllProducts" parameterClass="java.util.HashMap" resultMap="ProductResult">   
           2  select id,note from Product    
           3     <dynamic prepend="WHERE">   
           4     <!-- isPropertyAvailable判斷屬性是否有效 -->   
           5        <isPropertyAvailable property="id">   
           6          <isNotNull property="id">   
           7              <!-- isLessThan判斷參數(shù)是否小于compareValue,isLessEquals是小于等于 -->   
           8              <isLessThan prepend=" and " property="id" compareValue="10">   
           9              id = #id#    
          10              </isLessThan>   
          11          </isNotNull>   
          12        </isPropertyAvailable>   
          13      </dynamic>   
          14</select>   


          ---------------------------------幾個常用屬性----------------------------------

          Xml代碼

           1<isPropertyAvailable> 屬性是存在    
           2<isNotPropertyAvailable> 屬性不存在    
           3<isNull> 屬性值是null    
           4<isEmpty> 判斷Collection.size<1或String.length()<1   
           5<isEqual> 等于    
           6<isNotEqual> 不等于    
           7<isGreaterThan> 大于    
           8<isGreaterEqual> 大于等于    
           9<isLessThan> 小于    
          10<isLessEqual> 小于等于   
          posted on 2010-06-30 16:58 rogerfan 閱讀(194) 評論(0)  編輯  收藏 所屬分類: 【開源技術(shù)】
          主站蜘蛛池模板: 扶绥县| 平邑县| 宣城市| 渝中区| 托里县| 洛扎县| 曲水县| 靖西县| 西青区| 定南县| 红桥区| 深州市| 浮山县| 东阿县| 北川| 招远市| 荔浦县| 南投县| 甘孜| 扎鲁特旗| 剑河县| 屯留县| 陆河县| 丹寨县| 新泰市| 界首市| 招远市| 伊春市| 澳门| 马公市| 铁岭市| 漾濞| 吐鲁番市| 武城县| 昌吉市| 马尔康县| 枣强县| 太湖县| 绍兴市| 塔城市| 偏关县|