1. 程式人生 > 實用技巧 >第十二章、動態SQL

第十二章、動態SQL

第十二章、動態SQL

什麼是動態SQL

動態SQL就是根據不同的條件生成不同的SQL語句



1 如果你之前用過 JSTL 或任何基於類 XML 語言的文字處理器,
2  你對動態 SQL 元素可能會感覺似曾相識。在 MyBatis 之前的版本中,
3  需要花時間瞭解大量的元素。藉助功能強大的基於 OGNL 的表示式,
4  MyBatis 3 替換了之前的大部分元素,大大精簡了元素種類,
5  現在要學習的元素種類比原來的一半還要少。
6  if
7  choose (when, otherwise)
8  trim (where, set)
9  foreach

搭建環境

1  CREATE
TABLE `blog`( 2 `id` VARCHAR(50) NOT NULL COMMENT '部落格id', 3 `title` VARCHAR(100) NOT NULL COMMENT '部落格標題', 4 `author` VARCHAR(30) NOT NULL COMMENT '部落格作者', 5 `create_time` DATETIME NOT NULL COMMENT '建立時間', 6 `views` INT(30) NOT NULL COMMENT '瀏覽量' 7 )ENGINE=
INNODB DEFAULT CHARSET=utf8

IF

 1  <select id="queryBlog" resultType="blog" parameterType="map">
 2      select * from blog 
 3      where 1=1
 4      <if test="title != null">
 5          and title = #{title}
 6      </if>
 7      <if test="author != null">
 8          and
author = #{author} 9 </if> 10 </select>

choose (when, otherwise)

 1  <select id="queryBlogChoose" resultType="blog" parameterType="map">
 2      select * from blog
 3      <where>
 4          <choose>
 5              <when test="title != null">
 6                  title = #{title}
 7              </when>
 8              <when test="author != null">
 9                  and author = #{author}
10              </when>
11              <otherwise>
12                  and views = #{views}
13              </otherwise>
14          </choose>
15      </where>
16  </select>

trim (where, set)



 1 <select id="queryBlog" resultType="blog" parameterType="map">
 2      select * from blog
 3      <where>
 4          <if test="title != null">
 5              title = #{title}
 6          </if>
 7          <if test="author != null">
 8              and author = #{author}
 9          </if>
10      </where>
11  </select>

 1  <update id="updateBlog" parameterType="map">
 2      update blog
 3      <set>
 4          <if test="title != null">
 5              title = #{title},
 6          </if>
 7          <if test="author">
 8              author = #{author}
 9          </if>
10      </set>
11      where id = #{id}
12  </update>
13  

所謂的動態SQL,本質還是SQL語句,只是我們可以在SQL層面,去執行一個邏輯程式碼

SQL片段

有的時候,我們可能會將一些功能的部分抽取出來,方便複用

  • 使用SQL標籤抽取公共部分



1 <sql id="if-title-authou">
2      <if test="title != null">
3          title = #{title},
4      </if>
5      <if test="author">
6          author = #{author}
7      </if>
8  </sql>

  • 在需要使用的地方使用Include標籤引用

1  <update id="updateBlog" parameterType="map">
2      update blog
3      <set>
4          <include refid="if-title-authou"></include>
5      </set>
6      where id = #{id}
7  </update>

注意事項:

  • 最好基於單表來定義SQL片段

  • 不要存在where標籤

Foreach



1 <select id="queryBlogForeach" parameterType="map" resultType="blog">
2      select * from blog
3      <where>
4          <foreach collection="ids" item="id" open="id in (" close=")" separator=",">
5             #{id}
6          </foreach>
7      </where>
8  </select>

動態SQL就是在拼接SQL語句,我們只要保證SQL的正確性,按照SQL的格式,去排列組合就可以了

建議

  • 現在Mysql中寫出完整的SQL,在對應的去修改成為我們的動態SQL實現通用即可