1.问题写法
<if test="orderStatus != null and orderStatus != '' ">
and fpr.status=#{orderStatus}
</if>
如图在orderStatus != ’ ’ 中,当orderStatus 值为0时,sql语句未通过条件判断,这是因为mybatis在预编译sql时,使用OGNL表达式来解析if标签,对于Integer类型属性,在判断不等于 ’ ’ 时,例如orderStatus != ‘’,OGNL会返回’'的长度,源码:(s.length() == 0) ? 0.0 : Double.parseDouble( s ),因此表达式orderStatus != ''被当做age != 0来判断,所以当orderStatus 为0时,if条件判断不通过。
2.正确写法
<if test="orderStatus != null and orderStatus != '' or orderStatus==0">
and fpr.status=#{orderStatus}
</if>
加入判断or orderStatus==0;