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>
相关推荐
Ln5x9qZC223 分钟前
Flink SQL 元数据持久化实战
大数据·sql·flink
升职佳兴2 小时前
SQL 进阶3:连续登录问题与 ROW_NUMBER 差值法完整解析
java·数据库·sql
身如柳絮随风扬3 小时前
Mybatis分页实现原理与PageHelper插件深度解析
mybatis
希望永不加班3 小时前
SpringBoot 缓存注解:@Cacheable/@CacheEvict 使用
java·spring boot·spring·缓存·mybatis
014-code4 小时前
MySQL 很实用的 SQL 语句清单(排障与日常运维)
sql·mysql
oYD3FlT324 小时前
MyBatis-缓存与注解式开发
java·缓存·mybatis
bIo7lyA8v4 小时前
从 ChangeTracker 到 SQL Batch 的性能诊断与优化
数据库·sql·batch
蒸汽求职4 小时前
低延迟系统优化:针对金融 IT 与高频交易,如何从 CPU 缓存行(Cache Line)对齐展现硬核工程底蕴?
sql·算法·缓存·面试·职场和发展·金融·架构
麦聪聊数据17 小时前
企业数据流通与敏捷API交付实战(五):异构数据跨库联邦与零代码发布
数据库·sql·低代码·restful
R***z10119 小时前
Spring Boot 整合 MyBatis 与 PostgreSQL 实战指南
spring boot·postgresql·mybatis