MyBatis 动态 SQL

动态 SQL

动态 SQL 是 MyBatis 的强大特性之一。动态 SQL 可以根据运行时提供的条件,包括 参数值、逻辑判断、循环 等,动态生成SQL语句的各个部分的具体内容。

对动态SQL的学习主要是学习几个重要的标签,下面我们详细介绍:

1、<if> 标签

<if> 标签的作用是根据条件判断是否生成相应的 SQL 片段或语句。

例:根据可选参数插入用户信息

java 复制代码
    <insert id="insert2" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into userinfo(username, password
        <if test="photo != null">
            ,photo
        </if>
        ) values (
        #{username},
        #{password}
        <if test="photo != null">
            ,#{photo}
        </if>
        )
    </insert>

: test 中的 字段名,是传入对象中的 属性,不是数据库字段。


2、<trim> 标签

虽然上面的 <if> 标签可以实现一些动态 SQL,但是如果所有的参数都是可选参数的情况下,只使用 <if> 标签就可能会出错。例如:

java 复制代码
    <insert id="insert2" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into userinfo(
        <if test="username != null">
            username
        </if>
        <if test="password != null">
            ,password
        </if>
        ) values (
        <if test="username != null">
            #{username}
        </if>
        <if test="password != null">
            ,#{password}
        </if>
        )
    </insert>

上述这种情况下,如果只传入 username 参数,那么最终拼接的 SQL 语句为:insert into userinfo(,password)......显然,这是一个错误的 SQL 语句。

此时我们可以使用 <trim> 标签结合 <if> 标签,解决上述问题:

trim 标签中的重要属性:

  • prefix:表示整个语句块,以prefix的值作为前缀。
  • suffix:表示整个语句块,以suffix的值作为后缀。
  • prefixOverrides:表示整个语句块要去除掉的前缀。
  • suffixOverrides:表示整个语句块要去除掉的后缀。

使用 <trim> 标签解决上述问题:

java 复制代码
    <insert id="insert2" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into userinfo
        <trim prefix="(" suffix=")" prefixOverrides=",">
            <if test="username != null">
                username
            </if>
            <if test="password != null">
                ,password
            </if>
        </trim>
        values
        <trim prefix="(" suffix=")" prefixOverrides=",">
            <if test="username != null">
                #{username}
            </if>
            <if test="password != null">
                ,#{password}
            </if>
        </trim>
    </insert>

3、<where> 标签

  1. <where> 标签内部的条件会根据实际情况自动生成 where 子句。
  2. <where>标签会自动去除最前面的"and"和"or"关键字。
  3. 根据以上特性,<where>标签也可以使用 <trim prefix="where" prefixOverrides="and"> 或 <trim prefix="where" prefixOverrides="or">替换。

例:根据可选参数查询用户信息

java 复制代码
    <select id="getUserInfoByWhere" resultType="com.example.demo.model.UserInfo">
        select * from userinfo
        <where>
            <if test="id > 0">
                id=#{id}
            </if>
            <if test="photo != null">
                and photo=#{photo}
            </if>
        </where>
    </select>


4、<set> 标签

  1. <set> 标签内部的条件会根据实际情况自动生成 set 子句。
  2. <set>标签会自动去除最后面的,
  3. 根据以上特性,<set> 标签也可以使⽤ <trim prefix="set" suffixOverrides=","> 替换

例:设置可选参数更新用户信息

java 复制代码
    <update id="updateBySet">
        update userinfo
        <set>
            password=#{password},
            <if test="username != null">
                username=#{username},
            </if>
            <if test="photo != null">
                photo=#{photo}
            </if>
        </set>
        where id=#{id}
    </update>

5、<foreach> 标签

<foreach> 标签用于在 SQL 语句中循环遍历集合或数组,并将其元素应用到 SQL 的特定部分。

下面是 <foreach> 标签中的重要属性:

  • collection:绑定方法参数中的集合,如 List,Set,Map或数组对象.
  • item:遍历时的每⼀个对象.
  • open:语句块开头的字符串.
  • close:语句块结束的字符串.
  • separator:每次遍历之间间隔的字符串.

例如根据用户 id 批量删除用户信息:

接口:

java 复制代码
int deleteByIds(List<Integer> ids);

xml 实现:

java 复制代码
    <delete id="deleteByIds">
        delete from userinfo
        where id in
        <foreach collection="ids" open="(" close=")" item="id" separator=",">
            #{id}
        </foreach>
    </delete>
相关推荐
聪明蛋子哟7 小时前
Stagehand v3多语言SDK:Python/Go/Rust/Java下的浏览器自动化统一方案
python·golang·rust
今天AI了吗8 小时前
Python 基础语法(一):常量、变量、输入输出与运算符
开发语言·数据库·人工智能·python·sql·深度学习·机器学习
卷无止境8 小时前
Windows 上丝滑开发 Python,并稳定构建 Docker 镜像
后端·python·docker
TELL5218 小时前
selenium webdriver 第二次初始化的异常
开发语言·python
Csvn9 小时前
📊 SQL 入门 Day 22:视图与物化视图
后端·sql
Csvn9 小时前
🐍 Day 4: Python 控制流 — 条件、循环与推导式的艺术
后端·python
大鹏说大话11 小时前
从爬虫到决策引擎:大数据下自媒体如何用Python挖掘用户痛点
开发语言·爬虫·python
Niuguangshuo11 小时前
silero-vad:超轻量级开源 VAD 实践指南
开发语言·python
2501_9446761612 小时前
揭秘!WORDTIP公司靠不靠谱?小白必看
大数据·人工智能·python
Minner-Scrapy12 小时前
Scrapy 2.17 源码解析:Scheduler 调度器与磁盘/内存双队列
java·爬虫·python·scrapy·网络爬虫·twisted