mybatis查询参数为0时无法识别问题

最近在工作中遇到一个mybatis参数问题,主要是列表查询按照状态进行过滤,其中已完成状态值是0,被退回是1.如图所示

,

然后Mapper里面是和平常一样的写法<if test="status != null and status != ''>AND t1.status =#{status}</if>,发先现参数为1时过滤正常,参数为0时无法正常过滤,经过多次测试,终于找到了原因:

mybatis的Integer类型参数如果有为0的可能,在if标签判断中还应该单独加入判断,否则参数0会当成空字符串处理,所以应该改写为这种写法:

<if test="status != null and status != '' or status==0">AND t1.status =#{status}</if>

原文地址:https://www.cnblogs.com/houzheng/p/8745390.html