關於Mybatis中if,when等判斷條件為單字元的報錯問題
阿新 • • 發佈:2021-03-17
程式碼:
<when test="paramsMap.sort == 'c'">
i.sell_counts desc
</when>
當判斷條件為 ‘a’,‘b’,'c’這種單字元時會報型別轉換錯誤
原因:
這裡的test是雙引號包裹單引號,因為判斷條件是單字元 ‘c’ 這裡mybatis會把它當作char型別,而char型別查詢是根據ASCII碼進行查詢的,所以這裡的c會被當成整數進行查詢,所以就會報錯。
解決:
1、將雙引號包裹單引號改成單引號包裹雙引號
<when test='paramsMap.sort == "c" '>
i.sell_counts desc
</when>
2、將單引號用轉義符代替
<when test="paramsMap.sort == "c"">
i.sell_counts desc
</when>