- eg:
<if test="isOk != null and isOk !=''">
and is_ok = #{isOk}
</if>
此时当isOk为false时,并未查询出is_ok对应的结果来
- 找原因:
- 直接到数据库使用脚本查询
select * from table
where is_ok = false
此时能查出is_ok为0的数据
select * from table where is_ok = true
此时能查出is_ok为1的数据
此时定位问题是否在
<if test="isOk != null and isOk !=''">
通过排查去掉and isOk!=’’,可以正确的查出
所以正确的查询结构是:
<if test="isOk != null">
and is_ok = #{isOk}
</if>
经研究:
mybatis的if判断里面最好不要使用boolean值:
mybatis会默认把空值转为false。所以如果遇见前面传空值,这个字段在mybatis里面永远就是false了,可以使用数字类型代替,但是不要使用0作为参数。
鄙人拙见:若有更好的方法请指教!共同进步!