MyBatis學習總結(六)——動態SQL

jopen 8年前發布 | 17K 次閱讀 MyBatis3 持久層框架

MyBatis的動態SQL是基于OGNL表達式的,它可以幫助我們方便的在SQL語句中實現某些邏輯。

MyBatis中用于實現動態SQL的元素主要有:

 

  • if
  • choose(when,otherwise)
  • foreach
  • where
  • set
  • trim
下面我們主要說 where set trim 這三個標簽


1,where標簽


<!-- 查詢學生list,like姓名,=性別 -->     
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">     
    SELECT * from STUDENT_TBL ST      
        WHERE      
        <if test="studentName!=null and studentName!='' ">     
            ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
        </if>     
        <if test="studentSex!= null and studentSex!= '' ">     
            AND ST.STUDENT_SEX = #{studentSex}      
        </if>     
</select>

 如果上面例子,參數studentName為null或’’,則或導致此sql組合成“WHERE AND”之類的關鍵字多余的錯誤SQL。
 這時我們可以使用where動態語句來解決。這個“where”標簽會知道如果它包含的標簽中有返回值的話,它就插入一個‘where’。此外,如果標簽返回的內容是以AND 或OR 開頭的,則它會剔除掉。


使用where標簽

<!-- 查詢學生list,like姓名,=性別 -->     
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">     
    SELECT * from STUDENT_TBL ST      
    <where>     
        <if test="studentName!=null and studentName!='' ">     
            ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
        </if>     
        <if test="studentSex!= null and studentSex!= '' ">     
            AND ST.STUDENT_SEX = #{studentSex}      
        </if>     
    </where>     
</select>

2,set標簽


當在update語句中使用if標簽時,如果前面的if沒有執行,則或導致逗號多余錯誤。使用set標簽可以將動態的配置SET 關鍵字,和剔除追加到條件末尾的任何不相關的逗號。
沒有使用if標簽時,如果有一個參數為null,都會導致錯誤,如下示例:


<!-- 更新學生信息 -->     
<update id="updateStudent" parameterType="StudentEntity">     
    UPDATE STUDENT_TBL      
       SET STUDENT_TBL.STUDENT_NAME = #{studentName},      
           STUDENT_TBL.STUDENT_SEX = #{studentSex},      
           STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
           STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
</update>

使用set+if標簽修改后,如果某項為null則不進行更新,而是保持數據庫原值。如下示例:


<!-- 更新學生信息 -->     
<update id="updateStudent" parameterType="StudentEntity">     
    UPDATE STUDENT_TBL      
    <set>     
        <if test="studentName!=null and studentName!='' ">     
            STUDENT_TBL.STUDENT_NAME = #{studentName},      
        </if>     
        <if test="studentSex!=null and studentSex!='' ">     
            STUDENT_TBL.STUDENT_SEX = #{studentSex},      
        </if>     
        <if test="studentBirthday!=null ">     
            STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
        </if>     
        <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     
            STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
        </if>     
    </set>     
    WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
</update>

3,trim標簽


trim是更靈活的去處多余關鍵字的標簽,他可以實踐where和set的效果。

where例子的等效trim語句:


<!-- 查詢學生list,like姓名,=性別 -->     
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">     
    SELECT * from STUDENT_TBL ST      
    <trim prefix="WHERE" prefixOverrides="AND|OR">     
        <if test="studentName!=null and studentName!='' ">     
            ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
        </if>     
        <if test="studentSex!= null and studentSex!= '' ">     
            AND ST.STUDENT_SEX = #{studentSex}      
        </if>     
    </trim>     
</select>

set例子的等效trim語句:

<!-- 更新學生信息 -->     
<update id="updateStudent" parameterType="StudentEntity">     
    UPDATE STUDENT_TBL      
    <trim prefix="SET" suffixOverrides=",">     
        <if test="studentName!=null and studentName!='' ">     
            STUDENT_TBL.STUDENT_NAME = #{studentName},      
        </if>     
        <if test="studentSex!=null and studentSex!='' ">     
            STUDENT_TBL.STUDENT_SEX = #{studentSex},      
        </if>     
        <if test="studentBirthday!=null ">     
            STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
        </if>     
        <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     
            STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
        </if>     
    </trim>     
    WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
</update>

總結:


其實在真正應用時,我們更多的是使用查詢,上面介紹了動態SQL,下面我們也來對比一下


-- 方式一
<select id="selectResouceInfoByNotNullAttributes" resultMap="ExpandResultMap" parameterType="bean類名">    
select * from table_name where 1=1      
<if test="resourceId != null">        
and resource_id = #{resourceId,jdbcType=INTEGER}      
</if>      
<if test="appid != null">       
and  appid = #{appid,jdbcType=TINYINT}      
</if>      
<if test="resourceUrl != null">       
and  resource_url = #{resourceUrl,jdbcType=VARCHAR}      
</if>      
<if test="resourceDesc != null">       
and  resource_desc = #{resourceDesc,jdbcType=VARCHAR}      
</if>  
</select>
這種方式的要點在where后面的1=1,加上這個能夠避免第一個if條件后面是否需要加and的選擇困境。


-- 方式二
<select id="selectResouceInfoByNotNullAttributes" resultMap="ExpandResultMap" parameterType="bean類名">    
select * from 表名    
<where>      
<if test="resourceId != null">        
and resource_id = #{resourceId,jdbcType=INTEGER}      
</if>      
<if test="appid != null">       
and  appid = #{appid,jdbcType=TINYINT}      
</if>      
<if test="resourceUrl != null">       
and  resource_url = #{resourceUrl,jdbcType=VARCHAR}      
</if>      
<if test="resourceDesc != null">       
and  resource_desc = #{resourceDesc,jdbcType=VARCHAR}      
</if>    
</where>  
</select>

這種方式比第一種多了一個where標簽,而且不需要在where后面顯示的加一個1=1的字段。


-- 方式三
<select id="selectResouceInfoByNotNullAttributes" resultMap="ExpandResultMap" parameterType="bean類名">    
select * from 表名    
<trim prefix = "where" prefixOverrides="and|or">      
<if test="resourceId != null">        
and resource_id = #{resourceId,jdbcType=INTEGER}      
</if>      
<if test="appid != null">       
and  appid = #{appid,jdbcType=TINYINT}      
</if>      
<if test="resourceUrl != null">       
and  resource_url = #{resourceUrl,jdbcType=VARCHAR}      
</if>      
<if test="resourceDesc != null">       
and  resource_desc = #{resourceDesc,jdbcType=VARCHAR}      
</if>    
</trim>  
</select>
第三種方式是最值得提倡的方式,其中的trim標簽中標記該標簽是以where為前綴的,即where條件的字句。后面的prefixOverrides="and|or"是說如果where標簽中包含的內容開頭是and或者or,那么久忽略and或者or。還有另外一個:suffixOverrides="and|or",表示where字句中是and或者or結尾則忽略and或者or的意思。


來自: http://blog.csdn.net/hejingyuan6/article/details/50195291

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!