參數(shù)為String,if test讀取該參數(shù)代碼
1
2
3
4
5
6
7
8
9
10
11
|
< select id= "getMaxDepartId" parameterType= "java.lang.String" resultType= "java.lang.String" > SELECT MAX (DEPART_ID) FROM T_P_DEPART < where > <if test= "_parameter!=null and _parameter!=''" > AND DEPART_PID = #{departId,jdbcType= VARCHAR } </if> <if test= "_parameter==null or _parameter==''" > AND DEPART_PID IS NULL </if> </ where > </ select > |
參數(shù)為pojo , if test讀取該參數(shù)代碼
1
2
3
4
5
6
7
8
9
10
11
|
< select id= "findShopByName" parameterType= "ShopVo" resultType= "ShopCustomer" > select * from shop < where > <if test= "shopCustomer.shopname!=null and shopCustomer.shopname!=''" > shop.shopname like '%${shopCustomer.shopname}%' </if> <if test= "shopCustomer.shopname==null or shopCustomer.shopname==''" > AND shop.shopname is null </if> </ where > </ select > |
補(bǔ)充:關(guān)于mybatis中 if test的條件怎么寫(xiě)
1.mybatis 中 的 if test寫(xiě)法
1.1官方文檔上對(duì)于if是這么寫(xiě)的
1
2
3
|
<if test= "title != null" > AND title like #{title} </if> |
參考官方文檔:
實(shí)際項(xiàng)目中會(huì)有這種情況: 頁(yè)面上title字段輸入某個(gè)值進(jìn)行查詢,手動(dòng)將輸入框中的值刪除,然后再次查詢,發(fā)現(xiàn)結(jié)果不正確,究其原因是應(yīng)為title傳入了空串" " 這樣在mybatis配置文件中就會(huì)用空串進(jìn)行查詢,導(dǎo)致出現(xiàn)錯(cuò)誤結(jié)果
1.2建議寫(xiě)法
1
2
3
|
<if test= "title != null and title != ''" > AND title like #{title} </if> |
2.使用mybatis 做修改時(shí)將字段置空
if中如果傳入的參數(shù)如果為空,那么將不會(huì)執(zhí)行if中的語(yǔ)句
解決辦法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< update id= "updateObject" parameterType= "*.*.Object" > update table < set > <if test= "Object.fullName == null or Object.fullName ==''" > full_name = null , </if> <if test= "Object.fullName != null and Object.fullName !=''" > full_name = #{companyOrg.fullName}, </if> <if test= "Object.level == null or Object.level ==''" > level = null , </if> <if test= "Object.level == 0 " > level = null , </if> <if test= "Object.level != null and Object.level !='' and Object.level != 0 " > level = #{companyOrg. level }, </if> </ set > where 1=1 and id =#{companyOrg.id} </ update > |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/a241903820/article/details/51607131