Dynamic SQL mybatis動態sql

Dynamic SQL
動態sql是我們開發中家常便飯,但是我們經常會因爲格式不正確,多一個, 少一個空格等問題帶來諸多煩惱,所以動態sql是我們必須要掌握和了解的
The Dynamic SQL elements should be familiar to anyone who has used JSTL or any similar XML based text processors. In previous versions of MyBatis, there were a lot of elements to know and understand. MyBatis 3 greatly improves upon this, and now there are less than half of those elements to work with. MyBatis employs powerful OGNL based expressions to eliminate most of the other elements:
我們可以知道 mybatis3是使用OGNL表達式
• if
• choose (when, otherwise)
• trim (where, set)
• foreach

1. if

是最常用也是最簡單的不太多贅述

<select id="findActiveBlogWithTitleLike"
     resultType="Blog">
  SELECT * FROM BLOG
  WHERE 1=1 
  <if test="title != null">
    AND title like #{title}
  </if>
</select>

2. choose, when, otherwise

這個我按照Java中switch理解就可以 switch cas default正好相對應,個人想到的場景比較少,不多寫

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

where
where 就是解決我們使用if的時候 如果第一個條件爲空了 後面 條件會 在前面多一個 and或者or這樣的連接符,導致sql錯誤
其實就是我們平時填的 where 1=1 的一個替代方案

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG
  <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>
</select>

Trim

這個也是很簡單但是這個使用的會比較多,個人理解就是將trim快中的sql字符串按照我們的要求進行處理:具體什麼要求主要是下面的四個屬性決定的

<trim prefix="where" prefixOverrides="and | or" suffix=" " suffixOverrides=" , ">
    
</trim>

Prefix : 顧名思義是前綴,給我們trim快中的sql字符串添加前綴,比如where
PrefixOverrides: 如果trim塊中的sql字符串是and 或者or 開頭,去掉 and 或者 or
Suffix: 後綴,和prefix 反着理解就可以了
SuffixOverrides: 和prefixoverrides反着理解

Set

一般在update語句中使用,可以將後面多餘的, 去掉,此時不用我們關係最後一個多餘的,的問題

<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>

Foreach

Foreach大家都知道是一個循環關鍵字但是我們主要什麼情況下使用

  1. 批量插入 insert into value() ()();
  2. 如果條件是一個集合
<foreach collection="list" item="item" 
       index="index" open=" where ids in (" close=")" separator=",">
   
   
</foreach>

Collection: 表示集合如list,map等
Index:如果集合是list index就是List的index,如果是map,index就是key
Item: 如果集合是list,item就是該元素,如果是map, item就是value
Open: 就是foreach sql串開頭添加的字符串,
Close:就是foreach sql串結尾添加的字符串,
Separator:是元素之間的分割符

bind

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

生產環境中我們更推薦在參數中直接拼接好

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章