MyBatis實戰之動態SQL

若是使用JDBC或者其餘框架,不少時候你得根據須要去拼接SQL,這是一個麻煩的事情,而MyBatis提供對SQL語句動態的組裝能力,並且它只有幾個基本的元素,很是簡單明瞭,大量的判斷均可以在MyBatis的映射XML文件裏面配置,以達到許多咱們須要大量代碼才能實現的功能,大大減小了咱們編寫代碼的工做量,這體現了MyBatis的靈活性、高度可配置性和可維護性。MyBatis也能夠在註解中配置SQL,可是因爲註解中配置功能受限,對於複雜的SQL而言可讀性不好,因此使用較少。html

關於MyBatis的註解使用能夠參考個人這篇文章MyBatis實戰之初步java

今天主要講解這麼幾個經常使用的動態SQL:數據庫

1.if數組

2.choose、when、otherwisemybatis

3.trim、where、set框架

4.foreachide

5.test性能

6.bind單元測試

 

1、if測試

if一般與where一塊兒連用比較多

以下代碼所示:

<select id="selectName"  parameterType="String" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from `user` <where>
       <if test="userName!=''"> and user_name = #{userName} </if>
       </where>
    </select>

<if>中的test一般判斷的條件是不等於Null或者空字符串,除此以外還能夠進行比較判斷好比大於等於小於等這樣的。

 

2、choose、when、otherwise

<select id="selectName"  parameterType="String" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from `user` <choose>
       <when test="userName!=null and userName!=''"> where userName = #{userName} </when>
       <otherwise> where sex = #{sex} </otherwise>
       </choose>
    </select>

 

你能夠將<choose><when></when><otherwise></otherwise></choose>理解爲Java中的if-else或者是if-else if-else

 

3、trim、where、set

trim+where示例:

<select id="selectName"  parameterType="String" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from `user` <trim prefix="where" prefixOverrides="AND |OR">
          <if test="userName !=null and userName!=''"> and user_name = #{userName} </if>
       </trim>
    </select>

prefix:前綴      

prefixoverride:去掉第一個and或者是or

trim+set示例:

 update user   <trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">

    <if test="name != null and name.length()>0"> name=#{name} , </if>

    <if test="gender != null and gender.length()>0"> gender=#{gender} ,  </if>

  </trim>

prefix同trim+where的意思是同樣,都是前綴。

suffixoverride:去掉最後一個逗號(也能夠是其餘的標記,就像是上面前綴中的and同樣)

suffix:後綴

 

4、foreach

批量更新示例:

<update id="udpateUserLogoStatu" parameterType="java.util.List">
   
 <foreach collection="users" item="user" index="index" separator=";"> update `user` <set> logo = 1
        </set> where logo = #{user.logo} </foreach>     
 
 </update>

能夠參考這篇文章:mybatis的批量更新實例

foreach相關參數解釋:

collection配置的users是傳遞進來的參數名稱,它能夠是一個數組或者List、Set等集合;

item配置的是循環中當前的元素;

index配置的是當前元素在集合的位置下標;

separator是各個元素的間隔符;

還有代碼中沒有展現的open和close,它們的含義是以什麼符號將這些元素包裝起來。

 

在SQL中對於in語句咱們經常使用,對於大量數據的in語句須要咱們特別注意,由於它會消耗大量的性能,還有一些數據庫的SQL對執行的SQL長度也有限制。因此咱們使用它的時候須要預估一下這個collection對象的長度。

 

5、test

至於test就不提太多了,在<where>+<if>中用到很是頻繁。

 

6、bind

bind一般用於綁定參數,而後引用,在實際開發中用的也很多。

bind的示例:

<select id="getUserList" resultType="com.blog.entity.User">
          <!-- bind:能夠將OGNL表達式的值綁定到一個變量中,方便後來引用這個變量的值 -->
          <bind name="userName" value="'%'+userName+'%'"/> eName是employee中一個屬性值 SELECT * FROM `user` <if test="userName!=null"> where ename like #{userName} </if>
</select>

 

 

動態SQL通常常見的問題,就是查詢條件出錯,通常要麼是參數問題,或者是多個字段and拼接出問題。

另外還有一點要提醒,在要用動態SQL以前,最好仍是將SQL先執行一遍,肯定沒有問題後,再改成動態SQL,最後再單元測試多個邊界條件測試下,確保沒有問題後,再編寫對應的邏輯Controller。

相關文章
相關標籤/搜索