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>
</select>

查资料得知: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">
原文地址:https://www.cnblogs.com/ZJOE80/p/10456165.html