mybatis中的参数0被当做null值处理解决方案

2019-08-20 11:42:42  卢浮宫  版权声明:本文为站长原创文章,转载请写明出处


一、今天接到一个bug反馈,末功能在进行数据查询时写入条件后不能正确查询。


二、问题排查发现这个条件传递了参数0,但是在mybatis中被当做null值处理了。


三、原因

    在mybatis的文档有这样一段话

    Interpreting Objects as Booleans 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; Otherwise, its boolean value is true if and only if it is non-null.

    mybatis在处理参数时会与将对象解释为布尔值,非零是视为真,为零视为假。当你传入整型的0是就被解析为null了;


四、处理方案(附前后对比)


    ①这是最初的代码(出问题的那个!!!

        <if test="vendorType != null and vendorType != ''>
             and vendor_type = #{vendorType} 
        </if>

    ②最终修改

        <if test="vendorType != null and vendorType != '' or vendorType == '0'>
             and vendor_type = #{vendorType}
        </if>


五、针对这种情况可以用以下方案解决

    ①去除  !=''  判断

    ②增加  or XXX = '0'

    ③还有一种定义类型的(代码有点多,不写了)





更多精彩请关注guangmuhua.com


最新评论: