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 阅读(182) 评论(0)  编辑  收藏 所属分类: 【开源技术】

只有注册用户登录后才能发表评论。


网站导航: