1. 程式人生 > >Mybatis if test 動態判斷數字時需要注意的問題

Mybatis if test 動態判斷數字時需要注意的問題

一 錯誤案例

mapper 程式碼

<if test="filter == 1">//filter型別為Integer
    and r.provider_code  != #{providerCode}
</if>

結果:將條件去掉,查詢條件放入資料庫桌面應用Navicat中查詢,發現沒有錯誤。

推想:if test動態判斷數字時出現的錯誤。

二 例子詳解(參考)

mybatis做if判斷注意

  mybatis做if 判斷 注意:下面這種寫法只適用於 id 型別為字串.

<if test="id != null and id != '' ">

      id
= #{id}   </if>

  如果id型別為int 當id=0時 這個判斷不會進入.可以這樣寫
  

<if test="id != null and id != '' or id==0">

三 解決方案

<if test="filter != null and filter != '' or filter==1">
      ...
</if>