系列文章目录
Mybatis实用教程之XML实现动态sql
前言
当编写 MyBatis 中复杂动态 SQL 语句时,使用 XML 格式是一种非常灵活的方式。这样做可以根据不同条件动态生成 SQL 查询,更新或删除语句。以下是一篇简要的教程,详细介绍如何使用 MyBatis XML 来编写动态 SQL。
1. 动态条件查询
假设有一个 User 实体,有 id、username 和 email 字段,我们希望根据不同条件查询用户信息。
            
            
              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标签内的内容为空时,不会添加前缀。prefix和suffix属性用于指定插入语句的前缀和后缀。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>