解決mybatis where-if中if不能識別大寫AND,OR的問題
mybatis報錯:
Caused by: org.apache.ibatis.ognl.ParseException: Encountered " "AND “” at line 1
錯誤代碼:
<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為大寫,大寫不能識別,應(yīng)改為小寫。
<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判斷遇到的坑
最近在項目開發(fā)的過程中,遇到了Mybatis的一個坑(也許是Mybatis有意這樣設(shè)計的),對于Integer或者Long這種引用數(shù)據(jù)類型,在做if判斷的時候,如果引用數(shù)據(jù)類型為0,則mybatis將會視為”“空字符串,所以走不進判斷邏輯里。
以下余額字段為Long類型,availableAmount值為0時,將走不進判斷方法內(nèi)的示例截圖:

解決方法:
在test判斷條件中添加”or availableAmount==0“即可,以下是示例截圖:

或者在業(yè)務(wù)場景允許的情況下,只判斷availableAmount!=null
<if test="availableAmount!=null"> ... </if>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
如何解決getReader() has already been called&
這篇文章主要介紹了如何解決getReader() has already been called for this request問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
關(guān)于SpringMVC中控制器如何處理文件上傳的問題
這篇文章主要介紹了關(guān)于SpringMVC中控制器如何處理文件上傳的問題,在 Web 應(yīng)用程序中,文件上傳是一個常見的需求,例如用戶上傳頭像、上傳文檔等,本文將介紹 Spring MVC 中的控制器如何處理文件上傳,并提供示例代碼,需要的朋友可以參考下2023-07-07
Java中如何動態(tài)創(chuàng)建接口的實現(xiàn)方法
這篇文章主要介紹了Java中如何動態(tài)創(chuàng)建接口的實現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-09-09
Mybatis-plus自動填充不生效或自動填充數(shù)據(jù)為null原因及解決方案
本文主要介紹了Mybatis-plus自動填充不生效或自動填充數(shù)據(jù)為null原因及解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

