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>
復雜點的用法
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
默認true時緩存效果最好,可以減少更新。
12
13
serialize="false"
14
默認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當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> 小于等于