MyBatis之动态SQL

目录

一、<if>标签

二、<trim>标签

三、<where>标签

四、<set>标签

五、<foreach>标签


一、<if>标签

当我们在某个平台提交某些信息时,可能都会遇到这样的问题,有些信息是必填信息,有些信息是非必填信息,例如,使用CSDN发布博客时:

那么程序猿在不确定某个字段是否会被用户传入时,该如何拼接SQL呢?这就需要使用<if>标签来判断某个字段是否被用户输入

代码示例:

    <insert id="insertUser">
        insert into userinfo(username, password
        <if test="photo != null">
            ,photo
        </if>
        ) values(#{username}, #{password}
        <if test="photo != null">
            ,#{photo}
        </if>
        )
    </insert>

单元测试代码:

    @Test
    void insertUser() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("apple");
        userInfo.setPassword("123");
        userInfo.setPhoto(null);
        int ret = userMapper.insertUser(userInfo);
        System.out.println(ret);
    }

代码执行结果:

可以看到,photo为空时,最终构造的SQL语句并没有拼接photo字段。 

注意: 

(1) <if>标签必须包含test属性,如果test属性中的内容为true,则执行<if>标签语句中的内容;如果为false,则不执行,此时数据库中该字段的值为默认值。

(2) <if>标签中判断的字段必须是可以为null的字段,如果判断的字段不允许为null,程序会在执行期间报错。


二、<trim>标签

如果所有字段都是非必填信息,那么就需要使用<trim>标签结合<if>标签,对多个字段都采取动态生成的方式。

代码示例:

    <insert id="insertUser2">
        insert into userinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username!=null">
                username,
            </if>
            <if test="password!=null">
                password,
            </if>
            <if test="photo!=null">
                photo,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="username!=null">
                #{username},
            </if>
            <if test="password!=null">
                #{password},
            </if>
            <if test="photo!=null">
                #{photo},
            </if>
        </trim>
    </insert>

<trim>标签中的4个属性所表示的含义:

prefix:表示整个语句块以prefix的值作为前缀;

suffix:表示整个语句块以suffix的值作为后缀;

prefixOverrides:表示整个语句块要去掉的前缀;

suffixOverrides:表示整个语句块要去掉的后缀。

三、<where>标签

<where>标签需要配合<if>标签来使用,用来接收动态参数进行查询数据库操作。

代码示例:

    <select id="selectUsers" resultType="com.example.demo.model.UserInfo">
        select * from userinfo
        <where>
            <if test="username != null">
                username=#{username}
            </if>
            <if test="password != null">
                and password=#{password}
            </if>
        </where>
    </select>

注意:

(1) <where>标签会删除语句块的前缀内容(and);

(2) <where>标签中的字段如果都为null,那么就不会生成where关键字。

上述示例代码也可以使用<trim prefix="where" prefixOverrides="and">替换。 

四、<set>标签

<set>标签可以配合<if>标签,根据用户传入的参数来修改数据,用法和<where>标签类似。

代码示例:

    <update id="updateUser">
        update userinfo
        <set>
            <if test="username != null">
                username=#{username},
            </if>
            <if test="password != null">
                password=#{password},
            </if>
            <if test="photo != null">
                photo=#{photo},
            </if>
        </set>
        where id=#{id};
    </update>

注意:

(1) <set>标签会删除语句块的后缀内容(,);

(2) <set>标签中的字段如果都为null,那么就不会生成set关键字。

上述示例代码也可以使用<trim prefix="set" suffixOverrides=",">替换。

五、<foreach>标签

<foreach>标签用于对集合进行遍历,例如根据多个文章id删除文章。

代码示例:

    <delete id="deleteUsers">
        delete from userinfo where id in
        <foreach collection="list" open="(" close=")" item="id" separator=",">
            #{id}
        </foreach>
    </delete>

<foreach>标签中的5个必要属性:

collection:集合的名称(对应方法中的集合的参数名);

item:遍历集合时,集合中每个对象的名字;

separator:每次遍历之间以什么字符串间隔;

open:语句块的前缀内容;

close:语句块的后缀内容。

用一段伪代码来解释上述5个属性:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃点橘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值