JAVA—咖啡館

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

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


          1.ibatis中使用緩存
          首先設置SqlMapConfig.xml中<settings/>節點的屬性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/>節點應用cache

          Xml代碼 

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


          復雜點的用法

          Xml代碼 

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


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

          Xml代碼 

           1<select id="selectAllProducts" parameterClass="Product" resultMap="ProductResult">   
           2  select id,note from Product    
           3     <dynamic prepend="WHERE">   
           4     <!-- isNotNull判斷參數是否存在,Integer類型 -->   
           5          <isNotNull property="id">   
           6              <!-- isGreaterThan判斷參數是否大于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的?插入參數,$是文本替換 -->   
          14          note like '%$note$%'    
          15          </isNotEmpty>   
          16      </dynamic>   
          17</select>   


          用Map傳參數

          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判斷參數是否小于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)  編輯  收藏 所屬分類: 【開源技術】
          主站蜘蛛池模板: 莫力| 大英县| 浏阳市| 桂平市| 洛隆县| 乌兰浩特市| 内江市| 康保县| 固始县| 清原| 兴海县| 宁都县| 利津县| 新乡市| 双江| 酉阳| 淮安市| 太仓市| 古田县| 江门市| 通山县| 雷州市| 岳西县| 临邑县| 梅河口市| 柳河县| 中西区| 海晏县| 汶上县| 太湖县| 个旧市| 镇江市| 大荔县| 吕梁市| 临湘市| 阿克苏市| 镇平县| 离岛区| 唐山市| 芜湖市| 贵港市|