前言
本文主要給大家講如何在MyBatis中使用OGNL的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),感興趣的朋友們下面來一起看看詳細(xì)的介紹:
如果我們搜索OGNL相關(guān)的內(nèi)容,通常的結(jié)果都是和Struts有關(guān)的,你肯定搜不到和MyBatis有關(guān)的,雖然和Struts中的用法類似但是換種方式理解起來就有難度。
MyBatis常用OGNL表達(dá)式
- e1 or e2
- e1 and e2
- e1 == e2,e1 eq e2
- e1 != e2,e1 neq e2
- e1 lt e2:小于
- e1 lte e2:小于等于,其他gt(大于),gte(大于等于)
- e1 in e2
- e1 not in e2
- e1 + e2,e1 * e2,e1/e2,e1 - e2,e1%e2
- !e,not e:非,求反
- e.method(args)調(diào)用對象方法
- e.property對象屬性值
- e1[ e2 ]按索引取值,List,數(shù)組和Map
- @class@method(args)調(diào)用類的靜態(tài)方法
- @class@field調(diào)用類的靜態(tài)字段值
上述內(nèi)容只是合適在MyBatis中使用的OGNL表達(dá)式,完整的表達(dá)式點(diǎn)擊這里。
MyBatis中什么地方可以使用OGNL?
如果你看過深入學(xué)習(xí)MyBatis參數(shù),也許會有印象,因?yàn)檫@篇文章中提到了OGNL和一些特殊用法。
如果沒看過,建議找時(shí)間看看,上面這篇博客不是很容易理解,但是理解后會很有用。
MyBatis中可以使用OGNL的地方有兩處:
- 動態(tài)SQL表達(dá)式中
- ${param}參數(shù)中
上面這兩處地方在MyBatis中處理的時(shí)候都是使用OGNL處理的。
下面通過舉例來說明這兩種情況的用法。
1.動態(tài)SQL表達(dá)式中
例一,MySQL like 查詢:
1
2
3
4
5
6
7
8
|
<select id= "xxx" ...> select id,name,... from country <where> < if test= "name != null and name != ''" > name like concat( '%' , #{name}, '%' ) </ if > </where> </select> |
上面代碼中test的值會使用OGNL計(jì)算結(jié)果。
例二,通用 like 查詢:
1
2
3
4
5
6
7
8
9
|
<select id= "xxx" ...> select id,name,... from country <bind name= "nameLike" value= "'%' + name + '%'" /> <where> < if test= "name != null and name != ''" > name like #{nameLike} </ if > </where> </select> |
這里<bind>的value值會使用OGNL計(jì)算。
注:對<bind參數(shù)的調(diào)用可以通過#{}
或 ${}
方式獲取,#{}可以防止注入。
在通用Mapper中支持一種UUID的主鍵,在通用Mapper中的實(shí)現(xiàn)就是使用了<bind>標(biāo)簽,這個(gè)標(biāo)簽調(diào)用了一個(gè)靜態(tài)方法,大概方法如下:
1
2
|
<bind name= "username_bind" value= '@java.util.UUID@randomUUID().toString().replace("-", "")' /> |
這種方式雖然能自動調(diào)用靜態(tài)方法,但是沒法回寫對應(yīng)的屬性值,因此使用時(shí)需要注意。
2.${param}參數(shù)中
上面like的例子中使用下面這種方式最簡單
1
2
3
4
5
6
7
8
|
<select id= "xxx" ...> select id,name,... from country <where> < if test= "name != null and name != ''" > name like '${' % ' + name + ' % '}' </ if > </where> </select> |
這里注意寫的是${'%' + name + '%'}
,而不是%${name}%
,這兩種方式的結(jié)果一樣,但是處理過程不一樣。
在MyBatis中處理${}
的時(shí)候,只是使用OGNL計(jì)算這個(gè)結(jié)果值,然后替換SQL中對應(yīng)的${xxx}
,OGNL處理的只是${這里的表達(dá)式}。
這里表達(dá)式可以是OGNL支持的所有表達(dá)式,可以寫的很復(fù)雜,可以調(diào)用靜態(tài)方法返回值,也可以調(diào)用靜態(tài)的屬性值。
例子:使用OGNL實(shí)現(xiàn)單表的分表功能
上面說的是OGNL簡單的使用方法。這里舉個(gè)OGNL實(shí)現(xiàn)數(shù)據(jù)庫分表的例子。
分表這個(gè)功能是通用Mapper中的新功能,允許在運(yùn)行的時(shí)候指定一個(gè)表名,通過指定的表名對表進(jìn)行操作。這個(gè)功能實(shí)現(xiàn)就是使用了OGNL。
首先并不是所有的表都需要該功能,因此定義了一個(gè)接口,當(dāng)參數(shù)(接口方法只有實(shí)體類一個(gè)參數(shù))對象繼承該接口的時(shí)候,就允許使用動態(tài)表名。
1
2
3
4
5
6
7
8
9
|
public interface IDynamicTableName { /** * 獲取動態(tài)表名 - 只要有返回值,不是null和'',就會用返回值作為表名 * * @return */ String getDynamicTableName(); } |
然后在XML中寫表名的時(shí)候使用:
1
2
3
4
5
6
7
8
9
10
|
< if test="@tk.mybatis.mapper.util.OGNL@isDynamicParameter(_parameter) and dynamicTableName != null and dynamicTableName != ''"> ${dynamicTableName} </ if > < if test="@tk.mybatis.mapper.util.OGNL@isNotDynamicParameter(_parameter) or dynamicTableName == null or dynamicTableName == ''"> defaultTableName </ if > |
由于我需要判斷_parameter是否繼承了IDynamicTableName接口,簡單的寫法已經(jīng)無法實(shí)現(xiàn),所以使用了靜態(tài)方法,這兩個(gè)方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/** * 判斷參數(shù)是否支持動態(tài)表名 * * @param parameter * @return true支持,false不支持 */ public static boolean isDynamicParameter(Object parameter) { if (parameter != null && parameter instanceof IDynamicTableName) { return true ; } return false ; } /** * 判斷參數(shù)是否b支持動態(tài)表名 * * @param parameter * @return true不支持,false支持 */ public static boolean isNotDynamicParameter(Object parameter) { return !isDynamicParameter(parameter); } |
根據(jù)<if>判斷的結(jié)果來選擇使用那個(gè)表名。
另外注意XML判斷中有一個(gè)dynamicTableName,這個(gè)參數(shù)是根據(jù)getDynamicTableName方法得到的,MyBatis使用屬性對應(yīng)的getter方法來獲取值,不是根據(jù)field來獲取值。
最后
如果你真想了解MyBatis中的OGNL用法,自己多寫幾個(gè)例子測試玩玩,動手測試是一種好的學(xué)習(xí)方式。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:http://blog.csdn.net/isea533/article/details/50061705