MyBatis中if标签正确使用方法(Integer类型)

本文介绍了在MyBatis中使用Integer类型字段时,if标签可能导致的错误及其解决方法。在尝试解决status字段为空或值为0时的查询问题时,发现错误源于对OGNL表达式的误解。当status为0时,被解析为false,导致条件不成立。最终解决方案是正确使用if标签,避免对非String类型字段进行不必要的非空检查。
摘要由CSDN通过智能技术生成

场景

持久层:MyBatis
组合查询一组数据,字段有:String id,String name ,Integer status。
由于三个字段都可能为空,所以mapper文件中这样写的:

SELECT
        re.id,
        re.name,
        re.status
        FROM
        tj_registration_type re
        WHERE
        1=1
        <if test="id!='' and id!= null and id!='undefined'">
            AND re.id= #{id}
        </if>
        <if test="status!='' and status!= null and status!='undefined'">
            AND re.status= #{status}
        </if>
        <if test="name!='' and name!= null and name!='undefined'">
            AND re.name= #{name}
        </if>
        AND
        re.is_delete=0

status取值为0或1,当status字段传值时ÿ

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值