java:MyBatis动态SQL

1、if标签

if标签的格式

<if test="条件">
    SQL片段
</if>

if标签的作用

当条件为true的时候就拼接SQL片段

例如:根据用户名和性别查询用户,如果用户名称不为空,则用户名称作为查询条件,如果性别不为空,则性别作为查询条件

    <select id="selectByIf" resultType="User">
        select * from user
            where
            <if test="username != null and username != '' ">
                username like #{username}
            </if>
            <if test="sex != null and sex != '' ">
                and sex = #{sex};
            </if>
    </select>

但是这种写法,会发现如果不填写username的内容,这样的拼接机会报错,还有就是两个都不填的情况下,也会报错,因为还有个where在,这样的编写在实际应用情况是非常不符合的

2、where标签

where标签的作用

where需要在有条件的时候充当where关键字,没有查询条件的时候自动消失

  1. 自动补全where这个关键字
  2. 去掉多余的and和or关键字
    <select id="selectByIf" resultType="User">
        select * from user
            <where>
                <if test="username != null and username != '' ">
                    username like #{username}
                </if>
                <if test="sex != null and sex != '' ">
                    and sex = #{sex};
                </if>
            </where>
    </select>
3、set标签

在原本的修改SQL中,添加动态SQL

    <select id="updateByIf" resultType="User">
        update user set
        <if test="username != null and username !=''">
            username = #{username},
        </if>
        <if test="birthday != null and birthday !=''">
            birthday = #{birthday},
        </if>
        <if test="sex != null and sex !=''">
            sex = #{sex},
        </if>
        <if test="address != null and address !=''">
            address = #{address},
        </if>
            where id = #{id};
    </select>

会发现测试运行的时候SQL语句会多出来一个逗号

==>  Preparing: update user set username = ?, sex = ?, where id = ?;

所以我们会选择使用Set标签,可以去掉多余的逗号

    <select id="updateByIf" resultType="User">
        update user
        <set>
            <if test="username != null and username !=''">
                username = #{username},
            </if>
            <if test="birthday != null and birthday !=''">
                birthday = #{birthday},
            </if>
            <if test="sex != null and sex !=''">
                sex = #{sex},
            </if>
            <if test="address != null and address !=''">
                address = #{address},
            </if>
        </set>
            where id = #{id};
    </select>
4、foreach标签
foreach标签的属性 作用
collection 参数名
item 设置变量名,代表每个遍历的元素
separator 遍历一个元素添加的内容
#{变量名} 先使用?占位,后面给?赋值
open 在遍历前添加一次字符
close 在遍历后添加一次字符

批量删除用户

扫描二维码关注公众号,回复: 17158957 查看本文章
    <delete id="deleteByArray" >
        delete from user where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

注意:这边的ids是需要在接口那边进行注解的,不然他会使用默认名

    void deleteByArray(@Param("ids") int[] ids);
5、choose标签

相当于java中的switch

choose标签的属性 作用
when 匹配一个条件
otherwise 所有条件不匹配时执行

设置查询性别默认

    <select id="selectBySex" resultType="org.example.pojo.User">
        select * from user
        <where>
            <choose>
                <when test="sex == 1">
                    sex = '男'
                </when>
                <when test="sex == 0">
                    sex = '女'
                </when>
                 <otherwise>
                     sex = '女'
                 </otherwise>
            </choose>
        </where>
    </select>

猜你喜欢

转载自blog.csdn.net/weixin_53961667/article/details/134352464