zoukankan      html  css  js  c++  java
  • mybatis <if test=""></if>标签0为null 无效的 踩坑

    采过一个坑,写篇日志来记录下

    <if test="orderStatus != null and orderStatus !=''">
    and t.is_ship=#{orderStatus}
    </if>

    当状态值设置为0时,操作完了,数据库没反应,没有设置为0 
    把状态用1和2表示,不使用0,一切正常,问题消失了。 

    MyBatis的表达式是用OGNL处理的。OGNL表达式的规则如下

    1. Interpreting Objects as Booleans
    2. Any object can be used where a boolean is required. OGNL interprets objects as booleans like this:
    3.  
    4. If the object is a Boolean, its value is extracted and returned;
    5.  
    6. 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;
    7.  
    8. If the object is a Character, its boolean value is true if and only if its char value is non-zero;
    9.  
    10. Otherwise, its boolean value is true if and only if it is non-null.

    根据这一条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

      

    state !=''  传入0时,表达式的值为false;所以不执行。 
    解决办法,把这个删掉就可以了。

    <if test="state != null">state = #{state }</if>
    <if test="orderStatus != null ">
    and t.is_ship=#{orderStatus}
    </if>
     

    本来只有String类型的才会进行这样的判断——xx!= '',其实是自己码代码时只顾自制粘贴,自己给自己埋了个坑。

    这里有必要再提一个“坑”,如果类似于String str =”A”; 这样的写法时时,根据第三条规则,OGNL将会识别为Java 中的 char类型,显然String 类型与char类型做==运算会返回false,从而导致表达式不成立。解决方法很简单,修改为

    <if test='str!= null and str == "A"'>

     

     

  • 相关阅读:
    财务自由之路--笔记
    Spring Cloud微服务笔记(一)微服务与云概念
    Java8新特性----Stream
    Docker操作笔记(四)使用网络
    Docker操作笔记(三)数据管理
    Docker操作笔记(二)容器
    Docker操作笔记(一)使用镜像
    信息化领域热词分类分析及解释
    假期第二周总结
    假期第一周总结
  • 原文地址:https://www.cnblogs.com/yangsanluo/p/14573226.html
Copyright © 2011-2022 走看看