if test=的坑

if test=""的坑

原来接口
<select id="getSuperviseContentByType" resultType="java.util.Map">
    select *
    from supervise_accident_content sac
    <where>
        <choose>
            <when test="accidentType !='0'">
              and  sac.accident_catalogy=#{accidentType}
            </when>
            <when test="accidentType ='0'">
              and   1=1
            </when>
            <otherwise>
            </otherwise>
        </choose>
    </where>
</select>

结果就是不管传什么值,传0,1,2最后打印的都是

select * from supervise_accident_content sac WHERE sac.accident_catalogy=? 0or1or2

该问题所在

不管如果传值,最后只走accidentType!='0’这一步

image-20220117184024044

多方尝试

when test=“accidentType !=‘0’”>
and sac.accident_catalogy=#{accidentType}

改为了

when test=“accidentType !=0”>
and sac.accident_catalogy=#{accidentType}

仔细看看上面不同,0的单引号取消了

这里提醒一下accidentType的类型为String类型,所以我当时最开始写将0加上单引号

最后就好了。。。结论:

像if test这种+when test这种。

判断是否为某个值的时候,就算该值为String类型也不要加引号。

就为错的

候,就算该值为String类型也不要加引号。

就为错的

就为对的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值