工作中遇到的坑,随时补充。欢迎留言
一、mybatis 非空判断数字0为什么是false
<if test="id !=null and id !='' ">
and fr.id = #{id}
</if>
如上截取的mybatis的代码片段,当id的值为数字0时,test的判断结果是false。以上写法对id为String类型的变量没有问题。
<if test=" 0 !=null ">
的判断结果是true, <if test=" 0 !='' ">
的判断结果是false。
在mybatis的源码中0和 ''
都会被转成double类型的0.0,
所以<if test=" 0 =='' ">
等价于 <if test=" 0.0 ==0.0 ">
因此判断结果是true。