mybatis報錯:
1
|
caused by: org.apache.ibatis.ognl.parseexception: encountered " " and “” at line 1 |
錯誤代碼:
1
2
3
4
5
6
7
8
9
10
11
12
|
<select id= "selectaccountlist" resultmap= "baseresultmap" > select ct.customer_name customername,sam.city_code,sam.user_name,sam.account_name from sys_account_manager sam left join sys_customer ct on ct.id = sam.customer_id where sam.deleted = 0 < if test= "customername != null and customername != '' " > and ct.customer_name like concat( '%' ,#{customername}, '%' ) </ if > < if test= "citycode != null and citycode != '' " > and locate(#{citycode},sam.city_code) </ if > order by status,account_validity_time desc </select> |
正確代碼:
原因是:
if條件中and為大寫,大寫不能識別,應改為小寫。
1
2
3
4
5
6
7
8
9
10
11
12
|
<select id= "selectaccountlist" resultmap= "baseresultmap" > select ct.customer_name customername,sam.city_code,sam.user_name,sam.account_name from sys_account_manager sam left join sys_customer ct on ct.id = sam.customer_id where sam.deleted = 0 < if test= "customername != null and customername != '' " > and ct.customer_name like concat( '%' ,#{customername}, '%' ) </ if > < if test= "citycode != null and citycode != '' " > and locate(#{citycode},sam.city_code) </ if > order by status,account_validity_time desc </select> |
補充:mybatis中if判斷遇到的坑
最近在項目開發的過程中,遇到了mybatis的一個坑(也許是mybatis有意這樣設計的),對于integer或者long這種引用數據類型,在做if判斷的時候,如果引用數據類型為0,則mybatis將會視為”“空字符串,所以走不進判斷邏輯里。
以下余額字段為long類型,availableamount值為0時,將走不進判斷方法內的示例截圖:
解決方法:
在test判斷條件中添加”or availableamount==0“即可,以下是示例截圖:
或者在業務場景允許的情況下,只判斷availableamount!=null
1
2
3
|
< if test= "availableamount!=null" > ... </ if > |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/weixin_39093006/article/details/91041819