问题描述
在mybatis的xml中,当Integer为0的时候判断不生效,例如:
<if test="type != null and type != ''">
and type = #{type}
</if>
原因分析:
mybatis在预编译sql时,使用OGNL表达式来解析if标签,对于Integer类型属性,在判断不等于’‘时,例如type != ‘’,OGNL会返回’'的长度,源码:(s.length() == 0) ? 0.0 : Double.parseDouble( s ),因此表达式type != ''被当做type != 0来判断,所以当type为0时,if条件判断不通过。
解决方案:
根据业务逻辑判断,如果只需要判断为空的情况的话,只需要判断 type != null,如果必须要判断此参数为0的情况,就需要修改为 type != null and type != 0
参考资料: https://blog.csdn.net/qq_30038111/article/details/82665763