mybatis if标签 test中判断0错误

Long 和 Integer 在mybatis if标签 test中判断0错误问题

1、错误写法

<if test=“code != null and code == 0>and code = #{code}</if>

2、正确写法

<if test=“code != null or code == 0>and code = #{code}</if>

查看官网发现:
https://commons.apache.org/proper/commons-ognl/language-guide.html

Any object can be used where a boolean is required. OGNL interprets objects as booleans like this:
任何对象都可以使用布尔值。
• If the object is a Boolean, its value is extracted and returned;
如果对象是布尔布尔值,则提取并返回其值
• If the object is a Number, its double-precision floating-point value is compared with zero; non-zero is treated as true, zero as false;
如果对象是一个数,它的双精度浮点值与零;零视为真实的,零是错误的;
• If the object is a Character, its boolean value is true if and only if its char value is non-zero;
如果对象是一个字符,则其布尔值为真,当且仅当其char值为非零时
• Otherwise, its boolean value is true if and only if it is non-null.
否则,它只is true if布尔值,如果它是非空的。

原因: 0和“”都会被转成double进行比较,都会变成0.0,这就是mybati中 0 判定为false的原因

这里有必要再提一个“坑”,如果你有类似于String str =“A”; 这样的写法时,你要小心了。因为单引号内如果为单个字符时,OGNL将会识别为Java 中的 char类型,显然String 类型与char类型做==运算会返回false,从而导致表达式不成立。解决方法很简单,修改为< if test = ’ str != null and str == “A” '>即可

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值