一、If

  1. 作为判断条件,或者作为where的字句
<if test="title != null">
    AND title like #{title}
  </if>

二、where

where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。
如果只有一个参数,但是需要对信息进行判断,可能出现报错情况,需要使用内置参数名:_parameter 代替

 <select id="selectByQuestionName" resultMap="BaseResultMap" parameterType="java.lang.String">
        select * FROM wisteria.questions
        <where>
            <if test="_parameter != null and _parameter != ''">
                and question_msg = #{_parameter}
            </if>
        </where>
        limit 1
    </select>
<where>
    <if test="state != null">
         state = #{state}
    </if>
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
  </where>

trim 和where类似,不过可以自己设定清理和添加

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>

会移除prefixOverrides里面的东西,(上面空格必要的)

三、Set

set 元素可以用于动态包含需要更新的列,忽略其它不更新的列。

<update id="updateAuthorIfNecessary">
  update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
    </set>
  where id=#{id}
</update>

当然也可以使用trim支持

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

四、foreach

foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。这个元素也不会错误地添加多余的分隔符,看它多智能!

<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT *
  FROM POST P
  WHERE ID in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  </foreach>
</select>
<!--插入-->
<insert id="insertVideoPushList" parameterType="java.util.ArrayList">
        INSERT INTO `videodb`.`video_init_push`(`video_init_id`, `video_calculation_type`) VALUES
              <!--item 就是单个类对象-->
        <foreach collection="list" index="index" separator="," item="item">
            (#{item.videoInitId},#{item.videoCalculationType})
        </foreach>
</insert>

五、script

要在带注解的映射器接口类中使用动态 SQL,可以使用 script 元素。比如:
注解使用动态SQL

    @Update({"<script>",
      "update Author",
      "  <set>",
      "    <if test='username != null'>username=#{username},</if>",
      "    <if test='password != null'>password=#{password},</if>",
      "    <if test='email != null'>email=#{email},</if>",
      "    <if test='bio != null'>bio=#{bio}</if>",
      "  </set>",
      "where id=#{id}",
      "</script>"})
    void updateAuthorValues(Author author);

六、bind

bind 元素允许你在 OGNL 表达式以外创建一个变量,并将其绑定到当前的上下文

<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>

七、多数据库支持

如果配置了 databaseIdProvider,你就可以在动态代码中使用名为 “_databaseId” 的变量来为不同的数据库构建特定的语句。

<insert id="insert">
  <selectKey keyProperty="id" resultType="int" order="BEFORE">
    <if test="_databaseId == 'oracle'">
      select seq_users.nextval from dual
    </if>
    <if test="_databaseId == 'db2'">
      select nextval for seq_users from sysibm.sysdummy1"
    </if>
  </selectKey>
  insert into users values (#{id}, #{name})
</insert>

八、动态 SQL 中的插入脚本语言

按需要配置,这里不阐述….