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>
相关推荐
何中应1 小时前
MyBatis-Plus字段类型处理器使用
java·数据库·后端·mybatis
程序边界1 小时前
AI时代如何高效学习Python:从零基础到项目实战de封神之路(2025升级版)
人工智能·python·学习
炬火初现2 小时前
SQL语句——高级字符串函数 / 正则表达式 / 子句
数据库·sql
TTGGGFF2 小时前
云端服务器使用指南:利用Python操作mysql数据库
服务器·数据库·python
jie*2 小时前
小杰深度学习(four)——神经网络可解释性、欠拟合、过拟合
人工智能·python·深度学习·神经网络·scikit-learn·matplotlib·sklearn
编程充电站pro3 小时前
SQL 性能优化:为什么少用函数在 WHERE 条件中?
数据库·sql
似水流年,是谁苍白了等待3 小时前
Spring Boot + MyBatis plus + MySQL 实现位置直线距离实时计算
spring boot·mysql·mybatis
TDengine (老段)3 小时前
TDengine 时序函数 DERIVATIVE 用户手册
大数据·数据库·sql·物联网·时序数据库·iot·tdengine
TDengine (老段)3 小时前
TDengine 时序函数 STATEDURATION 用户手册
大数据·数据库·sql·物联网·时序数据库·iot·tdengine
jie*4 小时前
小杰深度学习(five)——正则化、神经网络的过拟合解决方案
人工智能·python·深度学习·神经网络·numpy·matplotlib