踩坑系列之《一》----- mybatis踩坑之0变空串

    xiaoxiao2022-07-07  214

    踩坑:

    在与前端的一次对接接口的过程中,发现前端传过来的一个过滤条件orderType为0时的筛选效果与不传该字段即查询所有的筛选效果是一样的,但是传1或者2或者其他值时可以匹配到相应的结果。

    之前我的xml是这么写的:

    <if test="orderType != null and orderType != ''">AND o.order_type = #{orderType}</if>

    但是mybatis会把0当做空串处理,所以也是没有进该条件,即相当于查询全部!

     

    解决办法:

    1>

    <if test="orderType != null">AND o.order_type = #{orderType}</if>

    2>

    <if test="orderType != null and orderType != '' or orderType == '0'.toString()"> AND o.order_type = #{orderType} </if>

     

    最新回复(0)