mybatis判斷Long,Integer類型的條件是否爲空

在使用spring和mybatis時,如果對象的某個屬性是整數類型,最好定義其爲Long,Integer包裝類型,不要使用long,int。因爲long,int不傳值時,默認爲0,這樣作爲過濾條件會導致查詢結果有誤。

一般我們在mybatis中判斷條件是否爲空時寫法如下:

<if test="pinyinCode != null and pinyinCode != ''">
    AND bs.PINYIN_CODE = #{pinyinCode, jdbcType=VARCHAR }
</if>

如果是Long,Integer類型時,如果設置其爲0,那麼在mybatis中 !=‘’ 是不成立的,因此對於Long,Integer類型做如下判斷:
 

 <if test="anesthetist != null">
      AND bs.ANESTHETIST = #{anesthetist, jdbcType=INTEGER }
 </if>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章