xml中写sql语句多重if判断时and的处理

2020-12-20  本文已影响0人  笑对浮华

在写sql语句时,遇到多个判断条件,当某一个字段不传时导致and多余的问题,解决方法:
使用<trim>判断语句</trim>标签对and进行过滤,具体实例如下:

select * from g_search
        <where>
            <trim prefixOverrides="and">
                <if test="terms != null and terms != ''">
                    term=#{terms}
                </if>
                <if test="goods_price != null and goods_price > 0">
                    and goods_price between 0 and #{goods_price}
                </if>
                <if test="is_top != null and is_top != ''">
                    and tags like '%#{is_top}%'
                </if>
            </trim>
        </where>

这里需要注意一个地方,就是and不能放在上一个判断的末尾,而是放在下一个判断的开头,否则不生效。

上一篇 下一篇

猜你喜欢

热点阅读