1. 程式人生 > >MyBatis 動態SQL

MyBatis 動態SQL

互斥 pda 1.3 null del 復制代碼 條件 rri mybatis

MyBatis動態SQL:

  動態SQL是在運行時生成和執行SQL的編程方法

   動態是和靜態相對而言的。靜態SQL指的是在代碼編譯時刻就已經包含在代碼中的那些已經充分明確的固定的SQL語句

MyBatis中的動態Sql是怎樣設定的?如何使用?

  MyBatis的動態SQL是基於OGNL表達式的,通過表達式進行判斷,對sql進行靈活拼接、組裝

  常用的節點有:if、choose when otherwise、where、set、trim、foreach、sql片段等 

  1.1 if

技術分享
    <select id="selectActiveBlogByTitle" parameterType="Blog" resultMap="blogResultMap">
        
        SELECT * FROM blog
        WHERE state = ‘ACTIVE‘

        <if test="title != null and title != ‘‘">
            AND title LIKE #{title}
        </if>
        
    </select>
技術分享

  1.2 choose when otherwise

技術分享
    <!-- 幾個查詢條件互斥 -->
    <select id="selectActiveBlogByTitleOrStyle" parameterType="Blog" resultMap="blogResultMap">
        select * from blog 
        WHERE state = ‘ACTIVE‘
        
        <choose>
            <when test="title != null and title != ‘‘">
                and lower(title) like lower(#{title})
            </when>
            <when test="style != null and style != ‘‘">
                and style = #{style}
            </when>
            <otherwise>
                and featured = true
            </otherwise>
        </choose>
    </select>
技術分享

  1.3 where

技術分享
    <!-- where:
                自動修補查詢條件,查詢語句中的 where 關鍵字使用 <where> 標簽替代
                註意:不能省略 and 或 or 關鍵字
         -->
    <select id="selectBlogByCondition" parameterType="Blog" resultMap="blogResultMap">
        select * from blog 
        
        <where>
            <if test="state != null and state != ‘‘">
                state = #{state}
            </if>
            <if test="title != null and title != ‘‘">
                and lower(title) like lower(#{title})
            </if>
            <if test="featured != null">
                and featured = #{featured}
            </if>
        </where>
    </select>
技術分享

   1.4 set

技術分享
  <!-- set:    會自動去掉語句後面多余的逗號  -->

    <update id="upadteBlogByCondition" parameterType="Blog">
        UPDATE 
          `blog` 
        <set>
          <if test="title != null">`title` = #{title},</if>
          <if test="authorId != null">`author_id` = #{authorId},</if>
          <if test="state != null">`state` = #{state},</if>
          <if test="featured != null">`featured` = #{featured},</if>
          <if test="style != null">`style` = #{style} </if>
        </set>
        WHERE `id` = #{id}
    </update> 
技術分享

   1.4 trim

技術分享
  <!-- trim:  trim可以實現set和where的功能 -->

    <select id="selectBlogByConditionTrim" parameterType="Blog" resultMap="blogResultMap">
        select * from blog 
        <trim prefix="where" prefixOverrides="and | or">
            <if test="state != null and state != ‘‘">
                state = #{state}
            </if>
            <if test="title != null and title != ‘‘">
                and lower(title) like lower(#{title})
            </if>
            <if test="featured != null">
                and featured = #{featured}
            </if>
        </trim>
    </select>
技術分享

  1.5 foreach

    <delete id="deleteBlogList" parameterType="list">
        delete from blog where id in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </delete>

  1.6 sql片段

技術分享
    <!-- sql片段 -->
    <sql id="aliasColumn">
        
    </sql>
    
        
    <!-- 引用 -->
    <include refid="aliasColumn">
    
    </include>
技術分享

MyBatis 動態SQL