需求是查询级别为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">

04-19 21:23
查看更多