场景:
数据表里面有一个整型字段 type,参数为 0 或 1,使用 mybatis 的 xml 文件写 sql 时发现传参为0时不起作用。
1.部分代码
<if test="userType != null and userType != ''">
AND s.`type` = #{userType}
</if>
userType 被声明为 Integer且传参为 0 时,userType=’’ 被判断为 true,里面包裹的sql语句就会被跳过去不执行。
2.解决
1.最简单的解决就是去掉 userType != ‘’
2.换一种方式,把Integer换成String,这样 0 就变成了 字符串 “0”