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

前言

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

实践

<select id="selectIndexAvgStore" resultMap="IndexShardNumCheck"  useCache="false" flushCache="true">
        <include refid="selectShardNumCheck"/>
        <where>
            <if test="indexPattern != null and indexPattern != ''">
                 index_pattern like concat('%', #{indexPattern, jdbcType=VARCHAR}, '%')
            </if>
            <if test="rationality != null and rationality != ''">
                AND rationality = #{rationality}
            </if>
        </where>
    </select>

但是此时并不能正确的查询到数据

问题原因:

status=‘’
MyBatis解析if标签时其表达式使用OGNL处理的。
Interpreting Objects as Booleans
Any object can be used where a boolean is required. OGNL interprets objects as booleans like this:
If the object is a Boolean, its value is extracted and returned;
If the object is a Number, its double-precision floating-point value is compared with zero; non-zero is treated as true, zero as false;
If the object is a Character, its boolean value is true if and only if its char value is non-zero;
Otherwise, its boolean value is true if and only if it is non-null.

如果对象是Number类型,当传值为0时会被解析成false,否则为true,浮点型0.00也是如此。所以这里直接解析成false。只有String类型才需要判断是否!=’’,其他类型完全没有这个必要。

解决方案:

<select id="selectIndexAvgStore" resultMap="IndexShardNumCheck"  useCache="false" flushCache="true">
        <include refid="selectShardNumCheck"/>
        <where>
            <if test="indexPattern != null and indexPattern != ''">
                 index_pattern like concat('%', #{indexPattern, jdbcType=VARCHAR}, '%')
            </if>
            <if test="rationality != null">
                AND rationality = #{rationality}
            </if>
        </where>
    </select>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值