如果不空null并且不是空字符串才去修改这个值,但这样写只能针对字符串(String)类型,如果是Integer类型的话就会有问题了。 int i = 0; i!=''。 mybatis中会返回tr

mybatis 参数为Integer型数据并赋值0时,有这样一个问题:

mybatis.xml中有if判断条件判断参数不为空时,赋值为0的Integer参数被mybatis判断为空,因此不执行<if test="param != null and param != ''"></if>的sql如果不空null并且不是空字符串才去修改这个值,但这样写只能针对字符串(String)类型,如果是Integer类型的话就会有问题了1

  • 2
  • 3

正常来说,0不为空也不是空字符串。所以,针对这个问题,我的解决办法是:如果类型为Integer类型,我就去掉 != ”的判断,只判断!=null即可。其实这并不是mybatis的语法

javascript里面空字符串就==true==0,
<if test="type!= null and type!= false">这样也是一样,只是他自己要定义成这样,并没有什么别的缘故啊
官网https://commons.apache.org/proper/commons-ognl/language-guide.html
也有说明的
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;
Otherwise, its boolean value is true if and only if it is non-null.
也可以自己下源码看下..
ognl会把字符串转成几个抽象语法树的节点,然后通过getValue调用一些方法将他们计算出来
这里主要是ASTNotEq的getValueBody方法..
其实还是没什么费解的必要,他就是定义成类似0==false==''这样的...不用管他就好
使用的时候用<if test="type!= null and type!= 0">就好理解了

原文地址:https://www.cnblogs.com/root429/p/9251345.html