mybatis在使用动态标签时传参为0的问题

项目的数据库设置的字段hotel_stopped为int类型,hotel_stopped=0 为已审核,1为已删除,2为未审核。。

做查询的时候要把hotel_stopped作为可选条件

<if test="null!=hotelStopped and !=hotelStopped ">
    AND hotel_stopped=#{hotelStopped}
</if>

然而发现查询的时候数据总是不对,当条件为空,查询全部可以,查询0出来的还是全部。

在网上查询也知道原因,是mybatis自动把0当作空来处理。

按照网上的办法,在后面再加条件或把判空删除。如下:

​<!--1-->
<if test="null!=hotelStopped and !=hotelStopped or 0==hotelStopped">
    AND hotel_stopped=#{hotelStopped}
</if>

<!--2-->
<if test="null!=hotelStopped and (hotelStopped==0 ? 0 : hotelStopped)">
    AND hotel_stopped=#{hotelStopped}
</if>

<!--3-->
<if test="null!=hotelStopped">
    AND hotel_stopped=#{hotelStopped}
</if>

查询0可以了,但是查询全部又变成查询0的结果了。

最后把hotelStopped类型改成Integer。使用第2个格式。。解决了问题。。。。- -

感谢:

经验分享 程序员 微信小程序 职场和发展