解決mybatis where-if中if不能識(shí)別大寫(xiě)AND,OR的問(wèn)題
Caused by: org.apache.ibatis.ognl.ParseException: Encountered ' 'AND “” at line 1錯(cuò)誤代碼:
<select 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為大寫(xiě),大寫(xiě)不能識(shí)別,應(yīng)改為小寫(xiě)。
<select 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>
補(bǔ)充:Mybatis中if判斷遇到的坑
最近在項(xiàng)目開(kāi)發(fā)的過(guò)程中,遇到了Mybatis的一個(gè)坑(也許是Mybatis有意這樣設(shè)計(jì)的),對(duì)于Integer或者Long這種引用數(shù)據(jù)類型,在做if判斷的時(shí)候,如果引用數(shù)據(jù)類型為0,則mybatis將會(huì)視為”“空字符串,所以走不進(jìn)判斷邏輯里。
以下余額字段為L(zhǎng)ong類型,availableAmount值為0時(shí),將走不進(jìn)判斷方法內(nèi)的示例截圖:
在test判斷條件中添加”or availableAmount==0“即可,以下是示例截圖:
或者在業(yè)務(wù)場(chǎng)景允許的情況下,只判斷availableAmount!=null
<if test='availableAmount!=null'> ...</if>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
