今天,根据库位类型查询 库位列表 出现问题。
当库位类型functions传值:0时,并没有按照"0"进行帅选,而是查出了所有的数据。
-----实际执行的查询---
select id id, shelf_order shelfOrder,functions functions,customer_id customerId,state state from location where state=1 and deleted=0;
---期望下面的查询---
select id id, shelf_order shelfOrder,functions functions,customer_id customerId,state state from location where state=1 and deleted=0 and functions=0;
mapper.xml内容:
select id id, shelf_order shelfOrder,functions functions,customer_id customerId,state state from location
where state=1 and deleted=0
<if test="functions!=null and functions!=''">
and functions = #{functions}
</if>
改成下面:
select id id, shelf_order shelfOrder,functions functions,customer_id customerId,state state from location
where state=1 and deleted=0
<if test="functions!=null and (functions!='' or functions ==0)">
and functions = #{functions}
</if>
以前出现过类似的错误,但是又再次犯错。特此记录一下。
问题分析:
首先他会获取两个判断对象的类型,当拿一个字符串和一个数字判断的时候,因为类型不一样嘛,当Mybatis发现,这个字符串是可以转换成数字的,那么就会把这个字符串转成数字,然后再和这个数字判断。
那么问题就来了,这个空字符串会转换成什么数字呢?
从源码
OgnlOps.compareWithConversion
就可以看出,这个空字符串,是会被转成0的.所以现在一切豁然开朗.
参考文章;
https://www.jianshu.com/p/732839a2f532