美文网首页
xml中写sql语句多重if判断时and的处理

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

作者: 笑对浮华 | 来源:发表于2020-12-20 19:00 被阅读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不能放在上一个判断的末尾,而是放在下一个判断的开头,否则不生效。

    相关文章

      网友评论

          本文标题:xml中写sql语句多重if判断时and的处理

          本文链接:https://www.haomeiwen.com/subject/kebknktx.html