1. 程式人生 > >Mybatis 學習記錄 (持續)

Mybatis 學習記錄 (持續)

1.動態SQL(  if  , choose , where ,set , foreach   )

 (1)   if

<select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">
      SELECT * FROM BLOG WHERE state = 'ACTIVE'
       <if test="title != null">
              AND title like #{title}
       </if>
       <if test="author != null and author.name != null">
              AND title like #{author.name}
       </if>
</select>

 (2)  choose  :相當於 java中的 switch

choose標籤是按順序判斷其內部when標籤中的test條件出否成立,如果有一個成立,則 choose 結束。

當 choose 中所有 when 的條件都不滿則時,則執行 otherwise 中的sql。

類似於Java 的 switch 語句,choose 為 switch,when 為 case,otherwise 則為 default。

<select id="findActiveBlogLike" parameterType="Blog" 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 title like #{author.name}    
        </when>
        <otherwise>
             AND featured = 1;
        </otherwise>
     </choose>
</select>

(3)  where   : 消除 前置 and 或 or

<select id="findActiveBlogLike" parameterType="Blog" 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 title like #{author.name}
        </if>
     </where>
</select>

 (4)  set   : 動態包含更新的列,而不包含不需要更新的

MyBatis在生成update語句時若使用if標籤,如果前面的if沒有執行,則可能導致有多餘逗號的錯誤。

使用set標籤可以將動態的配置SET 關鍵字,和剔除追加到條件末尾的任何不相關的逗號。 
沒有使用if標籤時,如果有一個引數為null,都會導致錯誤 

<update id="updateAuthorIfNecessary" parameterType="domain.blog.Author">
   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>

  (4)  foreach  :迭代集合

 

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

   

foreach標籤主要用於構建in條件,他可以在sql中對集合進行迭代。如下:

  

<delete id="deleteBatch"> 

    delete from user where id in

    <foreach collection="array" item="id" index="index" open="(" close=")" separator=",">

      #{id}

    </foreach>

  </delete>

  我們假如說引數為----  int[] ids = {1,2,3,4,5}  ----那麼列印之後的SQL如下:

  delete form user where id in (1,2,3,4,5)

  釋義:

    collection :collection屬性的值有三個分別是list、array、map三種,分別對應的引數型別為:List、陣列、map集合,我在上面傳的引數為陣列,所以值為array

    item : 表示在迭代過程中每一個元素的別名

    index :表示在迭代過程中每次迭代到的位置(下標)

    open :字首,表示該語句以什麼開始

    close :字尾,表示以什麼結束

    separator :分隔符,表示迭代時每個元素之間以什麼分隔

我們通常可以將之用到批量刪除、新增等操作中。

 

foreach標籤也可以實現實現批量插入(刪除)資料:

<insert id="addEmps">
        INSERT INTO tb1_emplyee(last_name,email,gender,d_id)
        VALUES 
        <foreach collection="emps" item="emp" separator=",">
            (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
        </foreach>
</insert>

對應的介面:

  public void addEmps(@Param("emps")List<Employee> emps);