Mybatis实用教程之XML实现动态sql

系列文章目录

1、mybatis简介及数据库连接池

2、mybatis中selectOne的使用

3、mybatis简单使用

4、mybatis中resultMap结果集的使用


Mybatis实用教程之XML实现动态sql


前言


当编写 MyBatis 中复杂动态 SQL 语句时,使用 XML 格式是一种非常灵活的方式。这样做可以根据不同条件动态生成 SQL 查询,更新或删除语句。以下是一篇简要的教程,详细介绍如何使用 MyBatis XML 来编写动态 SQL。

1. 动态条件查询

假设有一个 User 实体,有 idusernameemail 字段,我们希望根据不同条件查询用户信息。

xml 复制代码
<!-- 在 Mapper XML 文件中编写动态 SQL 查询 -->
<select id="selectUsers" resultType="User">
  SELECT * FROM users
  <where>
    <if test="id != null">
      AND id = #{id}
    </if>
    <if test="username != null and username != ''">
      AND username = #{username}
    </if>
    <if test="email != null and email != ''">
      AND email = #{email}
    </if>
  </where>
</select>
  • <where> 标签用于将动态生成的条件组合到 WHERE 子句中。
  • <if> 标签根据条件的存在与否来动态生成查询条件。

上面的方法可以根据id、username、email进行条件查询,当test后面的语句为true的时候,会将if标签内的语句拼接。

2. 动态更新语句

假设我们想根据不同的条件更新用户信息。

xml 复制代码
<!-- 在 Mapper XML 文件中编写动态 SQL 更新 -->
<update id="updateUser" parameterType="User">
  UPDATE users
  <set>
    <if test="username != null">
      username = #{username},
    </if>
    <if test="email != null">
      email = #{email},
    </if>
  </set>
  WHERE id = #{id}
</update>
  • <set> 标签用于指定要更新的字段。
  • <if> 标签根据条件动态设置要更新的字段。

3. 动态插入语句

如果要根据不同情况插入不同的字段,也可以使用动态 SQL。

xml 复制代码
<!-- 在 Mapper XML 文件中编写动态 SQL 插入 -->
<insert id="insertUser" parameterType="User">
  INSERT INTO users
  <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
      id,
    </if>
    <if test="username != null and username != ''">
      username,
    </if>
    <if test="email != null and email != ''">
      email,
    </if>
  </trim>
  <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
    <if test="id != null">
      #{id},
    </if>
    <if test="username != null and username != ''">
      #{username},
    </if>
    <if test="email != null and email != ''">
      #{email},
    </if>
  </trim>
</insert>
  • <trim> 标签用于动态设置插入的字段和对应的值,当trim标签内的内容为空时,不会添加前缀。
  • prefixsuffix 属性用于指定插入语句的前缀和后缀。
  • suffixOverrides 属性用于去除最后一个不必要的逗号。

4、其他标签的使用

基础的语法使用如下所示。choose、when、otherwise 有点像if-else if -else的感觉

java 复制代码
<!-- 使用 choose、when、otherwise 标签实现条件选择 -->
<select id="getUserByIdOrUsername" resultType="User">
    SELECT * FROM users
    <where>
        <choose>
            <when test="id != null">
                AND id = #{id}
            </when>
            <when test="username != null and username != ''">
                AND username = #{username}
            </when>
            <otherwise>
                AND 1=1
            </otherwise>
        </choose>
    </where>
</select>


<!-- 使用 foreach 标签进行遍历操作 -->
<select id="getUsersByIdList" resultType="User">
    SELECT * FROM users
    WHERE id IN
    <foreach collection="ids" item="id" open="(" separator="," close=")">
        #{id}
    </foreach>
</select>
相关推荐
Csvn3 小时前
📊 SQL 入门 Day 7:子查询 — 查询中的查询
后端·sql
Devin~Y11 小时前
互联网大厂Java面试实战:Spring Boot、MyBatis、Redis、Kafka、JWT、Spring Cloud 与 AI 场景追问
java·spring boot·redis·spring cloud·kafka·mybatis·spring security
Ethan010712 小时前
mybatis插件
mybatis
OneNobody12 小时前
Spark SQL AQE工作原理源码剖析
大数据·sql·spark
kali-Myon13 小时前
深入 MySQL 内核:从临时哈希表分配机制详解 Floor 报错注入核心原理
数据库·sql·mysql·安全·web
Mikowoo00713 小时前
批量汇总XML格式的发票信息
xml·python
梦想画家15 小时前
用SQL驱动AI:Trino AI Functions跨源情报分析实战指南
sql·ai·trino
逃逸线LOF1 天前
xml文件如何加载properties文件(spring容器加载properties文件)
xml
Csvn1 天前
📊 SQL 入门 Day 6:多表查询 — JOIN 的四种姿势
后端·sql
扎Zn了老Fe1 天前
MyBatis-Plus必知必会:告别低效CRUD,高效开发持久层
后端·mybatis