spring boot --07

一、什么是动态 SQL?

动态 SQL 是指随着用户输入或外部条件变化而动态拼接、生成的 SQL 语句,主要用于解决多条件查询、动态更新等场景,避免写大量重复的固定 SQL 语句。


二、核心标签详解

1. <if> 标签

作用 :用于条件判断,当test属性中的条件为true时,才会拼接标签内的 SQL 片段。语法示例

xml

复制代码
<if test="name != null">
    name like concat('%', #{name}, '%')
</if>

多条件查询示例

xml

复制代码
<select id="list" resultType="com.itheima.pojo.Emp">
    select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time 
    from emp
    where
    <if test="name != null">
        name like concat('%', #{name}, '%')
    </if>
    <if test="gender != null">
        and gender = #{gender}
    </if>
    <if test="begin != null and end != null">
        and entrydate between #{begin} and #{end}
    </if>
    order by update_time desc
</select>

⚠️ 注意:如果所有<if>条件都不满足,where关键字后会没有条件,导致 SQL 语法错误,此时需要配合<where>标签使用。


2. <where> 标签

作用

  • 仅当子标签(如<if>)有内容时,才会插入WHERE关键字;
  • 自动去除子句开头多余的ANDOR,避免语法错误。

优化后的多条件查询

xml

复制代码
<select id="list" resultType="com.itheima.pojo.Emp">
    select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time 
    from emp
    <where>
        <if test="name != null">
            name like concat('%', #{name}, '%')
        </if>
        <if test="gender != null">
            and gender = #{gender}
        </if>
        <if test="begin != null and end != null">
            and entrydate between #{begin} and #{end}
        </if>
    </where>
    order by update_time desc
</select>

3. <set> 标签

作用

  • 动态插入SET关键字;
  • 自动去除更新语句中多余的逗号,避免语法错误,常用于动态更新场景。

动态更新示例:需求:更新员工信息时,只更新用户传递了值的字段,未传递的字段不更新。

xml

复制代码
<update id="update">
    update emp
    <set>
        <if test="username != null">username = #{username},</if>
        <if test="name != null">name = #{name},</if>
        <if test="gender != null">gender = #{gender},</if>
        <if test="image != null">image = #{image},</if>
        <if test="job != null">job = #{job},</if>
        <if test="entrydate != null">entrydate = #{entrydate},</if>
        <if test="deptId != null">dept_id = #{deptId},</if>
        <if test="updateTime != null">update_time = #{updateTime}</if>
    </set>
    where id = #{id}
</update>

对比:如果不使用<set>标签,直接在set后写多个<if>,可能会出现最后一个字段后多余逗号的语法错误。


三、核心标签对比表

表格

标签 核心作用 典型使用场景
<if> 条件判断,动态拼接 SQL 片段 多条件查询、动态更新、动态插入
<where> 动态插入WHERE关键字,自动去除开头的AND/OR 多条件查询语句
<set> 动态插入SET关键字,自动去除多余逗号 动态更新语句
相关推荐
敲代码的嘎仔10 分钟前
互动问答系统实战:两级评论模型、ES 搜索集成、Caffeine 多级缓存全记录
java·开发语言·数据库·elasticsearch·缓存·mybatis·高并发
倔强的石头_11 分钟前
SQL Server数据库迁移,为什么 KES V9R4C019 能把改代码变成改连接
数据库
黑色的白兔No123 分钟前
deepin 25安装mysql
数据库·mysql·debian
cspttty37 分钟前
2026年财务分析岗JD中的Excel、SQL、BI与业务分析要求
大数据·sql·excel
cc57250265338 分钟前
2026 秋招采购数据分析校招 JD、面试真题与项目准备|2027 届求职复盘
数据库
cspttty42 分钟前
HR数字化校招准备路线:Excel、SQL、BI和AI工具怎么学
人工智能·sql·excel
风哥2号2 小时前
数据库教程FGMT28‑Windows‑MySQL5.7‑8.0‑8.4-9.7安装配置
数据库·mysql
2601_9622186110 小时前
万象生鲜系统业财一体化底层打通技术自动生成经营账单
大数据·数据库·人工智能·python·算法
李高钢10 小时前
Python FastAPI 框架入门:从零搭建你的第一个高性能 API 服务
数据库·python·fastapi
yuzhiboyouye12 小时前
那xml对应的sql语法,列举一下
xml·数据库·sql