1. 程式人生 > >mybatis 動態sql講解與示例分析

mybatis 動態sql講解與示例分析

   有些時候,sql語句where條件中,需要一些安全判斷,例如按某一條件查詢時如果傳入的引數是空,此時查詢出的結果很可能是空的,也許我們需要引數為空時,是查出全部的資訊。使用Oracle的序列、mysql的函式生成Id。這時我們可以使用動態sql。

       下文均採用mysql語法和函式(例如字串連結函式CONCAT)。

3.1 selectKey 標籤

       在insert語句中,在Oracle經常使用序列、在MySQL中使用函式來自動生成插入表的主鍵,而且需要方法能返回這個生成主鍵。使用myBatis的selectKey標籤可以實現這個效果。

       下面例子,使用mysql資料庫自定義函式nextval('student'),用來生成一個key,並把他設定到傳入的實體類中的studentId屬性上。所以在執行完此方法後,邊可以通過這個實體類獲取生成的key。

Xml程式碼  收藏程式碼
  1. <!-- 插入學生 自動主鍵-->  
  2. <insert id="createStudentAutoKey" parameterType="liming.student.manager.data.model.StudentEntity" keyProperty="studentId">  
  3.     <selectKey keyProperty="studentId" resultType="String" order="BEFORE">  
  4.         select nextval('student')  
  5.     </selectKey
    >  
  6.     INSERT INTO STUDENT_TBL(STUDENT_ID,  
  7.                             STUDENT_NAME,  
  8.                             STUDENT_SEX,  
  9.                             STUDENT_BIRTHDAY,  
  10.                             STUDENT_PHOTO,  
  11.                             CLASS_ID,  
  12.                             PLACE_ID)  
  13.     VALUES (#{studentId},  
  14.             #{studentName},  
  15.             #{studentSex},  
  16.             #{studentBirthday},  
  17.             #{studentPhoto, javaType=byte[], jdbcType=BLOBtypeHandler=org.apache.ibatis.type.BlobTypeHandler},  
  18.             #{classId},  
  19.             #{placeId})  
  20. </insert>  
 

呼叫介面方法,和獲取自動生成key

Java程式碼  收藏程式碼
  1. StudentEntity entity = new StudentEntity();  
  2. entity.setStudentName("黎明你好");  
  3. entity.setStudentSex(1);  
  4. entity.setStudentBirthday(DateUtil.parse("1985-05-28"));  
  5. entity.setClassId("20000001");  
  6. entity.setPlaceId("70000001");  
  7. this.dynamicSqlMapper.createStudentAutoKey(entity);  
  8. System.out.println("新增學生ID: " + entity.getStudentId());  

selectKey語句屬性配置細節:

屬性 描述 取值
keyProperty selectKey 語句生成結果需要設定的屬性。
resultType 生成結果型別,MyBatis 允許使用基本的資料型別,包括String 、int型別。
order

1:BEFORE,會先選擇主鍵,然後設定keyProperty,再執行insert語句;

2:AFTER,就先執行insert 語句再執行selectKey 語句。

BEFORE

AFTER
statementType MyBatis 支援STATEMENT,PREPARED和CALLABLE 的語句形式, 對應Statement ,PreparedStatement 和CallableStatement 響應

STATEMENT

PREPARED

CALLABLE

3.2 if標籤

 if標籤可用在許多型別的sql語句中,我們以查詢為例。首先看一個很普通的查詢:

Xml程式碼  收藏程式碼
  1. <!-- 查詢學生list,like姓名 -->  
  2. <select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap">  
  3.     SELECT * from STUDENT_TBL ST   
  4. WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')  
  5. </select>  

但是此時如果studentName或studentSex為null,此語句很可能報錯或查詢結果為空。此時我們使用if動態sql語句先進行判斷,如果值為null或等於空字串,我們就不進行此條件的判斷,增加靈活性。

引數為實體類StudentEntity。將實體類中所有的屬性均進行判斷,如果不為空則執行判斷條件。

Xml程式碼  收藏程式碼
  1. <!-- 2 if(判斷引數) - 將實體類不為空的屬性作為where條件 -->  
  2. <select id="getStudentList_if" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">  
  3.     SELECT ST.STUDENT_ID,  
  4.            ST.STUDENT_NAME,  
  5.            ST.STUDENT_SEX,  
  6.            ST.STUDENT_BIRTHDAY,  
  7.            ST.STUDENT_PHOTO,  
  8.            ST.CLASS_ID,  
  9.            ST.PLACE_ID  
  10.       FROM STUDENT_TBL ST   
  11.      WHERE  
  12.     <if test="studentName !=null ">  
  13.         ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')  
  14.     </if>  
  15.     <if test="studentSex != null and studentSex != '' ">  
  16.         AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
  17.     </if>  
  18.     <if test="studentBirthday != null ">  
  19.         AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
  20.     </if>  
  21.     <if test="classId != null and classId!= '' ">  
  22.         AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
  23.     </if>  
  24.     <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">  
  25.         AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
  26.     </if>  
  27.     <if test="placeId != null and placeId != '' ">  
  28.         AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}  
  29.     </if>  
  30.     <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">  
  31.         AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}  
  32.     </if>  
  33.     <if test="studentId != null and studentId != '' ">  
  34.         AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}  
  35.     </if>   
  36. </select>  
 

使用時比較靈活, new一個這樣的實體類,我們需要限制那個條件,只需要附上相應的值就會where這個條件,相反不去賦值就可以不在where中判斷。

Java程式碼  收藏程式碼
  1. public void select_test_2_1() {  
  2.     StudentEntity entity = new StudentEntity();  
  3.     entity.setStudentName("");  
  4.     entity.setStudentSex(1);  
  5.     entity.setStudentBirthday(DateUtil.parse("1985-05-28"));  
  6.     entity.setClassId("20000001");  
  7.     //entity.setPlaceId("70000001");  
  8.     List<StudentEntity> list = this.dynamicSqlMapper.getStudentList_if(entity);  
  9.     for (StudentEntity e : list) {  
  10.         System.out.println(e.toString());  
  11.     }  
  12. }  
 

3.3 if + where 的條件判斷

       當where中的條件使用的if標籤較多時,這樣的組合可能會導致錯誤。我們以在3.1中的查詢語句為例子,當java程式碼按如下方法呼叫時:

Java程式碼  收藏程式碼
  1. @Test  
  2. public void select_test_2_1() {  
  3.     StudentEntity entity = new StudentEntity();  
  4.     entity.setStudentName(null);  
  5.     entity.setStudentSex(1);  
  6.     List<StudentEntity> list = this.dynamicSqlMapper.getStudentList_if(entity);  
  7.     for (StudentEntity e : list) {  
  8.         System.out.println(e.toString());  
  9.     }  
  10. }  

如果上面例子,引數studentName為null,將不會進行STUDENT_NAME列的判斷,則會直接導“WHERE AND”關鍵字多餘的錯誤SQL。

這時我們可以使用where動態語句來解決。這個“where”標籤會知道如果它包含的標籤中有返回值的話,它就插入一個‘where’。此外,如果標籤返回的內容是以AND 或OR 開頭的,則它會剔除掉。

上面例子修改為:

Xml程式碼  收藏程式碼
  1. <!-- 3 select - where/if(判斷引數) - 將實體類不為空的屬性作為where條件 -->  
  2. <select id="getStudentList_whereIf" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">  
  3.     SELECT ST.STUDENT_ID,  
  4.            ST.STUDENT_NAME,  
  5.            ST.STUDENT_SEX,  
  6.            ST.STUDENT_BIRTHDAY,  
  7.            ST.STUDENT_PHOTO,  
  8.            ST.CLASS_ID,  
  9.            ST.PLACE_ID  
  10.       FROM STUDENT_TBL ST   
  11.     <where>  
  12.         <if test="studentName !=null ">  
  13.             ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')  
  14.         </if>  
  15.         <if test="studentSex != null and studentSex != '' ">  
  16.             AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
  17.         </if>  
  18.         <if test="studentBirthday != null ">  
  19.             AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
  20.         </if>  
  21.         <if test="classId != null and classId!= '' ">  
  22.             AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
  23.         </if>  
  24.         <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">  
  25.             AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
  26.         </if>  
  27.         <if test="placeId != null and placeId != '' ">  
  28.             AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}  
  29.         </if>  
  30.         <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">  
  31.             AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}  
  32.         </if>  
  33.         <if test="studentId != null and studentId != '' ">  
  34.             AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}  
  35.         </if>  
  36.     </where>    
  37. </select>  
 

3.4 if + set 的更新語句

當update語句中沒有使用if標籤時,如果有一個引數為null,都會導致錯誤。

當在update語句中使用if標籤時,如果前面的if沒有執行,則或導致逗號多餘錯誤。使用set標籤可以將動態的配置SET 關鍵字,和剔除追加到條件末尾的任何不相關的逗號。

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

Xml程式碼  收藏程式碼
  1. <!-- 4 if/set(判斷引數) - 將實體類不為空的屬性更新 -->  
  2. <update id="updateStudent_if_set" parameterType="liming.student.manager.data.model.StudentEntity">  
  3.     UPDATE STUDENT_TBL  
  4.     <set>  
  5.         <if test="studentName != null and studentName != '' ">  
  6.             STUDENT_TBL.STUDENT_NAME = #{studentName},  
  7.         </if>  
  8.         <if test="studentSex != null and studentSex != '' ">  
  9.             STUDENT_TBL.STUDENT_SEX = #{studentSex},  
  10.         </if>  
  11.         <if test="studentBirthday != null ">  
  12.             STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},  
  13.         </if>  
  14.         <if test="studentPhoto != null ">  
  15.             STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOBtypeHandler=org.apache.ibatis.type.BlobTypeHandler},  
  16.         </if>  
  17.         <if test="classId != '' ">  
  18.             STUDENT_TBL.CLASS_ID = #{classId}  
  19.         </if>  
  20.         <if test="placeId != '' ">  
  21.             STUDENT_TBL.PLACE_ID = #{placeId}  
  22.         </if>  
  23.     </set>  
  24.     WHERE STUDENT_TBL.STUDENT_ID = #{studentId};      
  25. </update>  

3.5 if + trim代替where/set標籤

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

3.5.1trim代替where

Xml程式碼  收藏程式碼
  1. <!-- 5.1 if/trim代替where(判斷引數) - 將實體類不為空的屬性作為where條件 -->  
  2. <select id="getStudentList_if_trim" resultMap="resultMap_studentEntity">  
  3.     SELECT ST.STUDENT_ID,  
  4.            ST.STUDENT_NAME,  
  5.            ST.STUDENT_SEX,  
  6.            ST.STUDENT_BIRTHDAY,  
  7.            ST.STUDENT_PHOTO,  
  8.            ST.CLASS_ID,  
  9.            ST.PLACE_ID  
  10.       FROM STUDENT_TBL ST   
  11.     <trim prefix="WHERE" prefixOverrides="AND|OR">  
  12.         <if test="studentName !=null ">  
  13.             ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')  
  14.         </if>  
  15.         <if test="studentSex != null and studentSex != '' ">  
  16.             AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
  17.         </if>  
  18.         <if test="studentBirthday != null ">  
  19.             AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
  20.         </if>  
  21.         <if test="classId != null and classId!= '' ">  
  22.             AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
  23.         </if>  
  24.         <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">  
  25.             AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
  26.         </if>  
  27.         <if test="placeId != null and placeId != '' ">  
  28.             AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}  
  29.         </if>  
  30.         <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">  
  31.             AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}  
  32.         </if>  
  33.         <if test="studentId != null and studentId != '' ">  
  34.             AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}  
  35.         </if>  
  36.     </trim>     
  37. </select>  
 

3.5.2 trim代替set

Xml程式碼  收藏程式碼
  1. <!-- 5.2 if/trim代替set(判斷引數) - 將實體類不為空的屬性更新 -->  
  2. <update id="updateStudent_if_trim" parameterType="liming.student.manager.data.model.StudentEntity">  
  3.     UPDATE STUDENT_TBL  
  4.     <trim prefix="SET" suffixOverrides=",">  
  5.         <if test="studentName != null and studentName != '' ">  
  6.             STUDENT_TBL.STUDENT_NAME = #{studentName},  
  7.         </if>  
  8.         <if test="studentSex != null and studentSex != '' ">  
  9.             STUDENT_TBL.STUDENT_SEX = #{studentSex},  
  10.         </if>  
  11.         <if test="studentBirthday != null ">  
  12.             STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},  
  13.         </if>  
  14.         <if test="studentPhoto != null ">  
  15.             STUDENT_TBL.STUDENT_PHOTO = #{studentPhoto, javaType=byte[], jdbcType=BLOBtypeHandler=org.apache.ibatis.type.BlobTypeHandler},  
  16.         </if>  
  17.         <if test="classId != '' ">  
  18.             STUDENT_TBL.CLASS_ID = #{classId},  
  19.         </if>  
  20.         <if test="placeId != '' ">  
  21.             STUDENT_TBL.PLACE_ID = #{placeId}  
  22.         </if>  
  23.     </trim>  
  24.     WHERE STUDENT_TBL.STUDENT_ID = #{studentId}  
  25. </update>  
 

3.6 choose (when, otherwise)

    有時候我們並不想應用所有的條件,而只是想從多個選項中選擇一個。而使用if標籤時,只要test中的表示式為true,就會執行if標籤中的條件。MyBatis提供了choose 元素。if標籤是與(and)的關係,而choose比傲天是或(or)的關係。

    choose標籤是按順序判斷其內部when標籤中的test條件出否成立,如果有一個成立,則choose結束。當choose中所有when的條件都不滿則時,則執行otherwise中的sql。類似於Java 的switch 語句,choose為switch,when為case,otherwise則為default。

    例如下面例子,同樣把所有可以限制的條件都寫上,方面使用。choose會從上到下選擇一個when標籤的test為true的sql執行。安全考慮,我們使用where將choose包起來,放置關鍵字多於錯誤。

Xml程式碼  收藏程式碼
  1. <!-- 6 choose(判斷引數) - 按順序將實體類第一個不為空的屬性作為where條件 -->  
  2. <select id="getStudentList_choose" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">  
  3.     SELECT ST.STUDENT_ID,  
  4.            ST.STUDENT_NAME,  
  5.            ST.STUDENT_SEX,  
  6.            ST.STUDENT_BIRTHDAY,  
  7.            ST.STUDENT_PHOTO,  
  8.            ST.CLASS_ID,  
  9.            ST.PLACE_ID  
  10.       FROM STUDENT_TBL ST   
  11.     <where>  
  12.         <choose>  
  13.             <when test="studentName !=null ">  
  14.                 ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')  
  15.             </when >  
  16.             <when test="studentSex != null and studentSex != '' ">  
  17.                 AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
  18.             </when >  
  19.             <when test="studentBirthday != null ">  
  20.                 AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
  21.             </when >  
  22.             <when test="classId != null and classId!= '' ">  
  23.                 AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
  24.             </when >  
  25.             <when test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">  
  26.                 AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
  27.             </when >  
  28.             <when test="placeId != null and placeId != '' ">  
  29.                 AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}  
  30.             </when >  
  31.             <when test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">  
  32.                 AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}  
  33.             </when >  
  34.             <when test="studentId != null and studentId != '' ">  
  35.                 AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}  
  36.             </when >  
  37.             <otherwise>  
  38.             </otherwise>  
  39.         </choose>  
  40.     </where>    
  41. </select>  

3.7 foreach

對於動態SQL 非常必須的,主是要迭代一個集合,通常是用於IN 條件。List 例項將使用“list”做為鍵,陣列例項以“array” 做為鍵。

foreach元素是非常強大的,它允許你指定一個集合,宣告集合項和索引變數,它們可以用在元素體內。它也允許你指定開放和關閉的字串,在迭代之間放置分隔符。這個元素是很智慧的,它不會偶然地附加多餘的分隔符。

注意:你可以傳遞一個List例項或者陣列作為引數物件傳給MyBatis。當你這麼做的時候,MyBatis會自動將它包裝在一個Map中,用名稱在作為鍵。List例項將會以“list”作為鍵,而陣列例項將會以“array”作為鍵。

這個部分是對關於XML配置檔案和XML對映檔案的而討論的。下一部分將詳細討論Java API,所以你可以得到你已經建立的最有效的對映。

3.7.1引數為array示例的寫法

介面的方法宣告:

Java程式碼  收藏程式碼
  1. public List<StudentEntity> getStudentListByClassIds_foreach_array(String[] classIds);  

動態SQL語句:

Xml程式碼  收藏程式碼
  1. <!— 7.1 foreach(迴圈array引數) - 作為where中in的條件 -->  
  2. <select id="getStudentListByClassIds_foreach_array" resultMap="resultMap_studentEntity">  
  3.     SELECT ST.STUDENT_ID,  
  4.            ST.STUDENT_NAME,  
  5.            ST.STUDENT_SEX,  
  6.            ST.STUDENT_BIRTHDAY,  
  7.            ST.STUDENT_PHOTO,  
  8.            ST.CLASS_ID,  
  9.            ST.PLACE_ID  
  10.       FROM STUDENT_TBL ST  
  11.       WHERE ST.CLASS_ID IN   
  12.      <foreach collection="array" item="classIds"  open="(" separator="," close=")">  
  13.         #{classIds}  
  14.