濤。

t.

关于mybatis-当if语句判断条件中传入0时,跳过判断的问题


经查资料发现,mybatis判断整数为0时,不进入该条件

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


原因:

在mybatis中会自动把0当成null,所以if判断为false
(而且只有查询字段类型为long 或者 Integer 时有此问题,String类型无此问题)

解决:

让字段的传值为0时判断为true

1.将判断为空串的判断去掉即可

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

2.增加判断条件,字段的传值==0即可

<if test="id != null and id != '' or id == 0 "> and id = #{id} </if>

 

posted on 2021-11-10 18:26  濤。  阅读(544)  评论(0编辑  收藏  举报

导航