在項目中偶然發現一個判斷數字的if沒有起任何作用,代碼如下
<if test="timeType !=null and timeType!='' and timeType == '3'">
AND
...
</if>
經過查詢資料發現,mybatis是用OGNL表達式來解析的,在OGNL的表達式中,數字’3’會被解析成字符,java是強類型的,char 和 一個string 會導致不等,所以if標籤中的sql不會被解析。
所以應該改爲:
<if test="timeType !=null and timeType!='' and timeType == '3'.toString() ">
AND
...
</if>
然後發現確實解決了這個問題,特此記錄。