使用Mybatis查询数据时,入参类型为Integer且入参为0,此时使用以下代码均无效:
<if test="brandType != null and brandType != ''">
AND
(
a.brand_type = #{brandType}
<if test="brandType == 0 or brandType == 1">
OR a.brand_type = 2
</if>
)
</if>
原因是因为Mybatis默认把0当做null来处理。
有两种方法:
1.删掉 brandType != ‘’
<if test="brandType != null">
AND
(
a.brand_type = #{brandType}
<if test="brandType == 0 or brandType == 1">
OR a.brand_type = 2
</if>
)
</if>
2.增加 or brandType == 0
<if test="brandType != null and brandType != '' or brandType == 0">
AND
(
a.brand_type = #{brandType}
<if test="brandType == 0 or brandType == 1">
OR a.brand_type = 2
</if>
)
</if>