代码改变世界

Mybaits传入参数为0时无法识别

2025-11-23 12:07  猎手家园  阅读(5)  评论(0)    收藏  举报

1. 在MyBatis中,如果参数类型为Integer或者Long时,传入参数为 0 将默认为 false(为空:例 status = ’’)不生效。

<if test="status != null and status != ''">
    and status = #{status}
</if>

以上示例中,如果查询条件为:status = 0 时,则不会生效。

2. 解决方案:去除为空字符判断

<if test="status != null">
    and status = #{status}
</if>