动态SQL

什么是动态SQL:

动态SQL就是指根据不同的条件生成不同的SQL语句

利用动态 SQL,可以彻底摆脱这种痛苦。

动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

动态SQL:

数据库:

public class Blog {
    private int id;
    private String title;
    private String author;
    private Date createTime;  // 属性名和数据库不一致 设置 <setting name="mapUnderscoreToCamelCase" value="true"/>
    private int views;
}

编写Mapper接口与MapperXML:

if

<select id="getBlogIf" parameterType="map" resultType="Blog">
    select * from blog where 1=1
    <if test="title != null">
        and title = #{title}
    </if>
    <if test="author != null">
        and author = #{author}
    </if>
</select>

这条语句提供了可选的查找文本功能。如果不传入 “title”,那么所有数据都会返回

choose(when、otherwise)

<select id="getBlogChoose" parameterType="map" resultType="Blog">
    select * from blog
    <where>
        <choose>
            <when test="title != null">
                title = #{title}
            </when>
            <when test="author != null">
                and author = #{author}
            </when>
            <otherwise>
                and views = #{views}
            </otherwise>
        </choose>
    </where>
</select>

传入了 “title” 就按 “title” 查找,传入了 “author” 就按 “author” 查找的情形。若两者都没有传入,就返回标记为 featured 的 BLOG

trim(where、set)

where

<select id="getBlogChoose" parameterType="map" resultType="Blog">
    select * from blog
    <where>
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </where>
</select>

where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。

set

<update id="updateBlog" parameterType="map">
    update blog
        <set>
            <if test="id != null">id=#{id},</if>
            <if test="title != null">title=#{title},</if>
            <if test="author != null">author=#{author},</if>
            <if test="views != null">views=#{views},</if>
        </set>
    where id=#{id}
</update>

set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号。

sql

<sql id="set-all">
    <set>
        <if test="id != null">id=#{id},</if>
        <if test="title != null">title=#{title},</if>
        <if test="author != null">author=#{author},</if>
        <if test="views != null">views=#{views},</if>
    </set>
</sql>
<update id="updateBlog" parameterType="map">
    update blog
        <include refid="set-all"></include>
    where id=#{id}
</update>

sql 可以提高代码的可用性

foreach

<select id="queryBlogForeach" parameterType="map" resultType="Blog">
    select * from
    <where>
        <foreach collection="ids" item="id" open="and (" close=")" separator="or">
            id = #{id}
        </foreach>
    </where>
</select>

动态 SQL 的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)

它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。

 

posted @ 2021-09-14 16:25  你的小可爱吖  阅读(96)  评论(0编辑  收藏  举报