Mybatis中 Integer 值為0時,默認為空字符串的解決辦法。
阿新 • • 發佈:2019-03-01
and del use 解決辦法 rom flag tis myba jdbc </select>
查資料得知:mybatis規定,mybatis在進行判斷時,會將integer=0的參數默認為‘’(空串)。
需求是查詢級別為0的用戶
User對象裏的level字段的值為0,查詢時居然沒有查到為level為0的用戶。
<select id="selectSelective" parameterType="com.agri.entity.User" resultMap="map">
select * from sys_user where del_flag = 1
<if test="level != null and level != ‘‘">
and level = #{level,jdbcType=INTEGER}
</if>
查資料得知:mybatis規定,mybatis在進行判斷時,會將integer=0的參數默認為‘’(空串)。
解決辦法
方法一:
去掉<if test="level != null and level != ‘‘">中的and level != ” 即:
<if test="level != null">
方法二:
在 <if test="level != null and level != ‘‘">中加入or level==0,即:
<if test="level != null and level != ‘‘ or level==0">
Mybatis中 Integer 值為0時,默認為空字符串的解決辦法。