1.问题描述
status为Integer类型 0/1
<if test="status != null and status != ''">
and t.status = #{status}
</if>
发现status=0时,mybatis构建的sql中where条件没有把status字段拼接上去
但是status=1时,sql中可以看到where中有status字段。
2.问题分析
mybatis在预编译sql解析if标签时,对于Integer类型属性,在判断不等于’‘时,例如status!=’‘,会返回’‘的长度,因此表达式status!=’'被当做status!=0来判断,所以当status为0时,if条件判断不通过
3.解决方案
当参数为数值类型的时候 不需要判断为空串‘’的情况,即将语句改成
<if test="status != null">
and t.status = #{status}
</if>