使用 Mybatis 傳入參數 integer 爲 0 的問題

 需求是查詢狀態 0 的訂單,status 類型 integer 類型,可是沒有查詢出 status 爲 0 的訂單。mybatis

<if test="status!= null and status!= '' ">  
     and status= #{status}   
</if>

通過排查發現 integer 類型的參數爲 0 時,mybatis 會把 0 解析成 ‘’空字符串,若是不空 null 而且不是‘’空字符串才當作查詢條件,但這樣寫只能針對字符串 String 類型。code

解決方法:xml

一、簡單處理把 type != '' 去掉ip

<if test="status!= null">  
     and status= #{status}   
</if>

二、增長 or status == 0 判斷字符串

<if test="status!= null and status !=  '' or status == 0">  
     and status= #{status}   
</if>

親測好用,詳細緣由可自行分析 mybatis 源碼,在此記錄下此問題。源碼

相關文章
相關標籤/搜索