隐藏

Mybatis处理select中where后面的and的两种方式

发布:2021/7/29 10:51:59作者:管理员 来源:本站 浏览次数:1404

第一种方式:后面跟1=1,使条件绝对成立:

    <select id="selectStudent">
        select * from student where 1=1
        <if test=" id !=null and id !='' ">
            and id = #{id}
        </if>
        <if test=" name !=null and name !='' ">
            and  name = #{name}
        </if>
    </select>

第二种方式:后面跟<where>标签,将第一个and过滤(不会过滤第二个):

    <select id="selectStudent">
        select * from student where
        <where>
            <if test=" id !=null and id !='' ">
                and id = #{id}
            </if>
            <if test=" name !=null and name !='' ">
                and  name = #{name}
            </if>
        </where>
    </select>