mybatis接收Integer参数时,若参数值为0,为被解析为空字符串' '。
平时写法
<if test="status != null and status != ''">
and ppi.status =#{status}
</if>
初步-解决方案:
-
只判断Integer参数不为null,不用判断参数不为空字符串;
<if test="status != null">
and ppi.status =#{status}
</if>
2.可以判断参数不等于0
<if test="status != null and status != '' or status == 0">
and ppi.status =#{status}
</if>
上述两种方法都会存在一个问题,因为int类型默认为0;当前端查询条件为空的时候,访问到后台的接口时,查询到的数据还是用0筛选出来的数据,
问题出现在我的status设计中:
我的status这样定义的:0-未启动,1-进行中,2-延期中,3-已完成
解决方法:
1、改变status定义,变为1-未启动,2-进行中,3-延期中,4-已完成(改动太大,就没用这种)
改动之后sql写法和平时写法一样
2、将int类型的status的默认值改成999。
<if test="status != null and status != 999">
and ppi.status =#{status}
</if>