Mybatis if判断Integer类型的值

问题描述:当传入的   status值为0 时,不进入 if 判断

    <select id="selectAll" parameterType="com.imes.domain.entities.User" resultMap="BaseResultMap">
        select * from user
        <where>          
            <if test="status != null and status != ''">
                and status = #{status}
            </if>
        </where>
    </select>  

执行语句为:

select * from user   

原因:

如果是Integer类型的话,如果变量的值是0,即 status = 0, 、
mybatis在进行 status != '' 的时候会认为  status  的值是空字符串, 即 status == '' 为true。

结论:

如果是Integer类型只需要判断 != null 即可

 

posted @ 2022-02-16 15:08  唏嘘-  阅读(647)  评论(0编辑  收藏  举报