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关键字,自动去除多余逗号 动态更新语句
相关推荐
我叫张小白。8 小时前
基于Redis的缓存架构与一致性保障体系
数据库·redis·缓存·架构
Omics Pro8 小时前
基因泰克:检测级虚拟细胞基准!大语言模型+智能体
大数据·数据库·人工智能·机器学习·语言模型·自然语言处理·r语言
Quincy_Freak8 小时前
工具分享|基于 SQLiteGo 的国产系统离线数据处理方案
大数据·数据库·数据分析·arm·国产系统·银河麒麟·aarch64
我是一颗柠檬8 小时前
【Redis】数据类型详解Day2(2026年)
数据库·redis·后端·缓存
Trouvaille ~8 小时前
【Redis篇】List 列表:双端队列与消息队列的完美实现
数据库·redis·list·双端队列·后端开发·quicklist·zoplist
Cloud_Shy6188 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第一章 Item 4 - 6)
android·数据库·论文阅读·python
土狗TuGou8 小时前
SQL内功笔记 · 第7篇:CTE&临时表&递归
数据库·笔记·后端·sql·mysql
XiYang-DING8 小时前
【Spring】日志
java·数据库·spring
我是唐青枫8 小时前
MySQL EXISTS 详解:存在性判断、NOT EXISTS 与实战示例
数据库·mysql
weixin_468466858 小时前
Airtable 零基础快速上手与实战指南
数据库·人工智能·python·深度学习·ai·大模型