ibaties經典16句總結

http://javacrazyer.iteye.com/blog/1135561


(1) 輸入參數爲單個值

Xml代碼  收藏代碼
  1. <delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore"   
  2. parameterClass="long">   
  3. delete from   
  4. MemberAccessLog   
  5. where   
  6. accessTimestamp = #value#   
  7. </delete>   

 
(2) 輸入參數爲一個對象

 

Xml代碼  收藏代碼
  1. <insert id="com.fashionfree.stat.accesslog.MemberAccessLog.insert"   
  2. parameterClass="com.fashionfree.stat.accesslog.model.MemberAccessLog>   
  3. insert into MemberAccessLog   
  4. (   
  5. accessLogId, memberId, clientIP,   
  6. httpMethod, actionId, requestURL,   
  7. accessTimestamp, extend1, extend2,   
  8. extend3   
  9. )   
  10. values   
  11. (   
  12. #accessLogId#, #memberId#,   
  13. #clientIP#, #httpMethod#,   
  14. #actionId#, #requestURL#,   
  15. #accessTimestamp#, #extend1#,   
  16. #extend2#, #extend3#   
  17. )   
  18. </insert>   

 

(3) 輸入參數爲一個java.util.HashMap 

Xml代碼  收藏代碼
  1. <select id="com.fashionfree.stat.accesslog.selectActionIdAndActionNumber"   
  2. parameterClass="hashMap"   
  3. resultMap="getActionIdAndActionNumber">   
  4. select   
  5. actionId, count(*) as count   
  6. from   
  7. MemberAccessLog   
  8. where   
  9. memberId = #memberId#   
  10. and accessTimestamp &gt; #start#   
  11. and accessTimestamp &lt;= #end#   
  12. group by actionId   
  13. </select>  

 

  (4) 輸入參數中含有數組

   

Xml代碼  收藏代碼
  1. <insert id="updateStatusBatch" parameterClass="hashMap">   
  2. update   
  3. Question   
  4. set   
  5. status = #status#   
  6. <dynamic prepend="where questionId in">   
  7. <isNotNull property="actionIds">   
  8. <iterate property="actionIds" open="(" close=")" conjunction=",">   
  9. #actionIds[]#   
  10. </iterate>   
  11. </isNotNull>   
  12. </dynamic>   
  13. </insert>   

   說明:actionIds爲傳入的數組的名字; 
   使用dynamic標籤避免數組爲空時導致sql語句語法出錯;
   使用isNotNull標籤避免數組爲null時ibatis解析出錯

 

   (5)傳遞參數只含有一個數組 
  

Xml代碼  收藏代碼
  1. <select id="com.fashionfree.stat.accesslog.model.StatMemberAction.selectActionIdsOfModule"   
  2. resultClass="hashMap">   
  3. select   
  4. moduleId, actionId   
  5. from   
  6. StatMemberAction   
  7. <dynamic prepend="where moduleId in">   
  8. <iterate open="(" close=")" conjunction=",">   
  9. #[]#   
  10. </iterate>   
  11. </dynamic>   
  12. order by   
  13. moduleId   
  14. </select>   

    說明:注意select的標籤中沒有parameterClass一項
       另:這裏也可以把數組放進一個hashMap中,但增加額外開銷,不建議使用

 

   (6)讓ibatis把參數直接解析成字符串 
  

Xml代碼  收藏代碼
  1. <select id="com.fashionfree.stat.accesslog.selectSumDistinctCountOfAccessMemberNum"   
  2. parameterClass="hashMap" resultClass="int">   
  3. select   
  4. count(distinct memberId)   
  5. from   
  6. MemberAccessLog   
  7. where   
  8. accessTimestamp &gt;= #start#   
  9. and accessTimestamp &lt; #end#   
  10. and actionId in $actionIdString$   
  11. </select>  

    說明:使用這種方法存在sql注入的風險,不推薦使用

 

    (7)分頁查詢 (pagedQuery)

   

Java代碼  收藏代碼
  1. <select id="com.fashionfree.stat.accesslog.selectMemberAccessLogBy"   
  2. parameterClass="hashMap" resultMap="MemberAccessLogMap">   
  3. <include refid="selectAllSql"/>   
  4. <include refid="whereSql"/>   
  5. <include refid="pageSql"/>   
  6. </select>   
  7. <select id="com.fashionfree.stat.accesslog.selectMemberAccessLogBy.Count"   
  8. parameterClass="hashMap" resultClass="int">   
  9. <include refid="countSql"/>   
  10. <include refid="whereSql"/>   
  11. </select>   
  12. <sql id="selectAllSql">   
  13. select   
  14. accessLogId, memberId, clientIP,   
  15. httpMethod, actionId, requestURL,   
  16. accessTimestamp, extend1, extend2,   
  17. extend3   
  18. from   
  19. MemberAccessLog   
  20. </sql>   
  21. <sql id="whereSql">   
  22. accessTimestamp &lt;= #accessTimestamp#   
  23. </sql>   
  24. <sql id="countSql">   
  25. select   
  26. count(*)   
  27. from   
  28. MemberAccessLog   
  29. </sql>   
  30. <sql id="pageSql">   
  31. <dynamic>   
  32. <isNotNull property="startIndex">   
  33. <isNotNull property="pageSize">   
  34. limit #startIndex# , #pageSize#   
  35. </isNotNull>   
  36. </isNotNull>   
  37. </dynamic>   
  38. </sql>   

   說明:本例中,代碼應爲:
   HashMap hashMap = new HashMap();
   hashMap.put(“accessTimestamp”, someValue);
   pagedQuery(“com.fashionfree.stat.accesslog.selectMemberAccessLogBy”, hashMap); 
   pagedQuery方法首先去查找名爲com.fashionfree.stat.accesslog.selectMemberAccessLogBy.Count 的mapped statement來進行sql查詢,從而得到com.fashionfree.stat.accesslog.selectMemberAccessLogBy查詢的記錄個數,
再進行所需的paged sql查詢(com.fashionfree.stat.accesslog.selectMemberAccessLogBy),具體過程參見utils類中的相關代碼


(8)sql語句中含有大於號>、小於號<

    1. 將大於號、小於號寫爲: &gt; &lt; 如: 

Xml代碼  收藏代碼
  1. <delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" parameterClass="long">   
  2. delete from   
  3. MemberAccessLog   
  4. where   
  5. accessTimestamp &lt;= #value#   
  6. </delete>   

 

    2. 將特殊字符放在xml的CDATA區內: 

Xml代碼  收藏代碼
  1. <delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" parameterClass="long">   
  2. <![CDATA[  
  3. delete from  
  4. MemberAccessLog  
  5. where  
  6. accessTimestamp <= #value#  
  7. ]]>   
  8. </delete>   

   推薦使用第一種方式,寫爲&lt; 和 &gt; (XML不對CDATA裏的內容進行解析,因此如果CDATA中含有dynamic標籤,將不起作用) 

(9)include和sql標籤
   將常用的sql語句整理在一起,便於共用: 

Xml代碼  收藏代碼
  1. <sql id="selectBasicSql">   
  2. select   
  3. samplingTimestamp,onlineNum,year,   
  4. month,week,day,hour   
  5. from   
  6. OnlineMemberNum   
  7. </sql>   
  8. <sql id="whereSqlBefore">   
  9. where samplingTimestamp &lt;= #samplingTimestamp#   
  10. </sql>   
  11. <select id="com.fashionfree.accesslog.selectOnlineMemberNumsBeforeSamplingTimestamp" parameterClass="hashmap" resultClass="OnlineMemberNum">   
  12. <include refid="selectBasicSql" />   
  13. <include refid="whereSqlBefore" />   
  14. </select>   

    注意:sql標籤只能用於被引用,不能當作mapped statement。如上例中有名爲selectBasicSql的sql元素,試圖使用其作爲sql語句執行是錯誤的:
    sqlMapClient.queryForList(“selectBasicSql”); ×

(10)隨機選取記錄

Xml代碼  收藏代碼
  1. <sql id=”randomSql”>   
  2. ORDER BY rand() LIMIT #number#   
  3. </sql>   

    從數據庫中隨機選取number條記錄(只適用於MySQL)

 

(11)將SQL GROUP BY分組中的字段拼接

Xml代碼  收藏代碼
  1. <sql id=”selectGroupBy>   
  2. SELECT   
  3. a.answererCategoryId, a.answererId, a.answererName,   
  4. a.questionCategoryId, a.score, a.answeredNum,   
  5. a.correctNum, a.answerSeconds, a.createdTimestamp,   
  6. a.lastQuestionApprovedTimestamp, a.lastModified, GROUP_CONCAT(q.categoryName) as categoryName   
  7. FROM   
  8. AnswererCategory a, QuestionCategory q   
  9. WHERE a.questionCategoryId = q.questionCategoryId   
  10. GROUP BY a.answererId   
  11. ORDER BY a.answererCategoryId   
  12. </sql>  

    注:SQL中使用了MySQL的GROUP_CONCAT函數

(12) 按照IN裏面的順序進行排序

    ①MySQL: 

Xml代碼  收藏代碼
  1. <sql id=”groupByInArea”>   
  2. select   
  3. moduleId, moduleName,   
  4. status, lastModifierId, lastModifiedName,   
  5. lastModified   
  6. from   
  7. StatModule   
  8. where   
  9. moduleId in (3, 5, 1)   
  10. order by   
  11. instr(',3,5,1,' , ','+ltrim(moduleId)+',')   
  12. </sql>   

    

②SQLSERVER:

Xml代碼  收藏代碼
  1. <sql id=”groupByInArea”>   
  2. select   
  3. moduleId, moduleName,   
  4. status, lastModifierId, lastModifiedName,   
  5. lastModified   
  6. from   
  7. StatModule   
  8. where   
  9. moduleId in (3, 5, 1)   
  10. order by   
  11. charindex(','+ltrim(moduleId)+',' , ',3,5,1,')   
  12. </sql>  

    說明:查詢結果將按照moduleId在in列表中的順序(3, 5, 1)來返回
    MySQL : instr(str, substr)
    SQLSERVER: charindex(substr, str) 
    返回字符串str 中子字符串的第一個出現位置 
    ltrim(str) 
    返回字符串str, 其引導(左面的)空格字符被刪除

(13) resultMap 
   
resultMap負責將SQL查詢結果集的列值映射成Java Bean的屬性值。

Xml代碼  收藏代碼
  1. <resultMap class="java.util.HashMap" id="getActionIdAndActionNumber">   
  2. <result column="actionId" property="actionId" jdbcType="BIGINT" javaType="long"/>   
  3. <result column="count" property="count" jdbcType="INT" javaType="int"/>   
  4. </resultMap>   

   使用resultMap稱爲顯式結果映射,與之對應的是resultClass(內聯結果映射),使用resultClass的最大好處便是簡單、方便,不需顯示指定結果,由iBATIS根據反射來確定自行決定。而resultMap則可以通過指定jdbcType和javaType,提供更嚴格的配置認證。


(14) typeAlias

Xml代碼  收藏代碼
  1. <typeAlias alias="MemberOnlineDuration" type="com.fashionfree.stat.accesslog.model.MemberOnlineDuration" />   
  2. <typeAlias>允許你定義別名,避免重複輸入過長的名字。  

 

(15) remap

Xml代碼  收藏代碼
  1. <select id="testForRemap" parameterClass="hashMap" resultClass="hashMap" remapResults="true">   
  2. select   
  3. userId   
  4. <isEqual property="tag" compareValue="1">   
  5. , userName   
  6. </isEqual>   
  7. <isEqual property="tag" compareValue="2">   
  8. , userPassword   
  9. </isEqual>   
  10. from   
  11. UserInfo   
  12. </select>   

  此例中,根據參數tag值的不同,會獲得不同的結果集,如果沒有remapResults="true"屬性,iBatis會將第一次查詢時的結果集緩存,下次再執行時(必須還是該進程中)不會再執行結果集映射,而是會使用緩存的結果集。
因此,如果上面的例子中remapResult爲默認的false屬性,而有一段程序這樣書寫: 

Java代碼  收藏代碼
  1. HashMap<String, Integer> hashMap = new HashMap<String, Integer>();   
  2. hashMap.put("tag"1);   
  3. sqlClient.queryForList("testForRemap", hashMap);   
  4. hashMap.put("tag"2);   
  5. sqlClient.queryForList("testForRemap", hashMap);   

 則程序會在執行最後一句的query查詢時報錯,原因就是iBATIS使用了第一次查詢時的結果集,而前後兩次的結果集是不同的:(userId, userName)和(userId, userPassword),所以導致出錯。如果使用了remapResults="true"這一屬性,iBATIS會在每次執行查詢時都執行結果集映射,從而避免錯誤的發生(此時會有較大的開銷)。

(16) dynamic標籤的prepend

  dynamic標籤的prepend屬性作爲前綴添加到結果內容前面,當標籤的結果內容爲空時,prepend屬性將不起作用。
當dynamic標籤中存在prepend屬性時,將會把其嵌套子標籤的第一個prepend屬性忽略。例如:

Xml代碼  收藏代碼
  1. <sql id="whereSql">   
  2. <dynamic prepend="where ">   
  3. <isNotNull property="userId" prepend="BOGUS">   
  4. userId = #userId#   
  5. </isNotNull>   
  6. <isNotEmpty property="userName" prepend="and ">   
  7. userName = #userName#   
  8. </isNotEmpty>   
  9. </dynamic>   
  10. </sql>  

  

此例中,dynamic標籤中含有兩個子標籤<isNotNull>和<isNotEmpty>。根據前面敘述的原則,如果<isNotNull>標籤中沒有prepend="BOGUS" 這一假的屬性來讓dynamic去掉的話,<isNotEmpty>標籤中的and就會被忽略,會造成sql語法錯誤。
   注意:當dynamic標籤沒有prepend屬性時,不會自動忽略其子標籤的第一個prepend屬性。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章