程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 轉:ibatis常用16條SQL語句,ibatis16條sql語句

轉:ibatis常用16條SQL語句,ibatis16條sql語句

編輯:JAVA綜合教程

轉:ibatis常用16條SQL語句,ibatis16條sql語句


1.輸入參數為單個值

<delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore"    
 parameterClass="long">    
 delete from    
 MemberAccessLog    
 where    
 accessTimestamp = #value#    
 </delete>   

 <delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore"   
 parameterClass="long">   
 delete from   
 MemberAccessLog   
 where   
 accessTimestamp = #value#   
 </delete>

2.輸入參數為一個對象

<insert id="com.fashionfree.stat.accesslog.MemberAccessLog.insert"    
 parameterClass="com.fashionfree.stat.accesslog.model.MemberAccessLog>    
 insert into MemberAccessLog    
 (    
 accessLogId, memberId, clientIP,    
 httpMethod, actionId, requestURL,    
 accessTimestamp, extend1, extend2,    
 extend3    
 )    
 values    
 (    
 #accessLogId#, #memberId#,    
 #clientIP#, #httpMethod#,    
 #actionId#, #requestURL#,    
 #accessTimestamp#, #extend1#,    
 #extend2#, #extend3#    
 )    
 </insert>   

 <insert id="com.fashionfree.stat.accesslog.MemberAccessLog.insert"   
 parameterClass="com.fashionfree.stat.accesslog.model.MemberAccessLog>   
 insert into MemberAccessLog   
 (   
 accessLogId, memberId, clientIP,   
 httpMethod, actionId, requestURL,   
 accessTimestamp, extend1, extend2,   
 extend3   
 )   
 values   
 (   
 #accessLogId#, #memberId#,   
 #clientIP#, #httpMethod#,   
 #actionId#, #requestURL#,   
 #accessTimestamp#, #extend1#,   
 #extend2#, #extend3#   
 )   
 </insert

3.輸入參數為一個java.util.HashMap

<select id="com.fashionfree.stat.accesslog.selectActionIdAndActionNumber"    
 parameterClass="hashMap"    
 resultMap="getActionIdAndActionNumber">    
 select    
 actionId, count(*) as count    
 from    
 MemberAccessLog    
 where    
 memberId = #memberId#    
 and accessTimestamp &gt; #start#    
 and accessTimestamp &lt;= #end#    
 group by actionId    
 </select>
<select id="com.fashionfree.stat.accesslog.selectActionIdAndActionNumber"   
 parameterClass="hashMap"   
 resultMap="getActionIdAndActionNumber">   
 select   
 actionId, count(*) as count   
 from   
 MemberAccessLog   
 where   
 memberId = #memberId#   
 and accessTimestamp &gt; #start#   
 and accessTimestamp &lt;= #end#   
 group by actionId   
 </select>

4.輸入參數中含有數組

<insert id="updateStatusBatch" parameterClass="hashMap">    
 update    
 Question    
 set    
 status = #status#    
 <dynamic prepend="where questionId in">    
 <isNotNull property="actionIds">    
 <iterate property="actionIds" open="(" close=")" conjunction=",">    
 #actionIds[]#    
 </iterate>    
 </isNotNull>    
 </dynamic>    
 </insert>   

 <insert id="updateStatusBatch" parameterClass="hashMap">   
 update   
 Question   
 set   
 status = #status#   
 <dynamic prepend="where questionId in">   
 <isNotNull property="actionIds">   
 <iterate property="actionIds" open="(" close=")" conjunction=",">   
 #actionIds[]#   
 </iterate>   
 </isNotNull>   
 </dynamic>   
 </insert>   

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

5.傳遞參數只含有一個數組

<select id="com.fashionfree.stat.accesslog.model.StatMemberAction.selectActionIdsOfModule"    
 resultClass="hashMap">    
 select    
 moduleId, actionId    
 from    
 StatMemberAction    
 <dynamic prepend="where moduleId in">    
 <iterate open="(" close=")" conjunction=",">    
 #[]#    
 </iterate>    
 </dynamic>    
 order by    
 moduleId    
 </select>

 <select id="com.fashionfree.stat.accesslog.model.StatMemberAction.selectActionIdsOfModule"   
 resultClass="hashMap">   
 select   
 moduleId, actionId   
 from   
 StatMemberAction   
 <dynamic prepend="where moduleId in">   
 <iterate open="(" close=")" conjunction=",">   
 #[]#   
 </iterate>   
 </dynamic>   
 order by   
 moduleId   
 </select>   

說明:注意select的標簽中沒有parameterClass一項

另:這裡也可以把數組放進一個hashMap中,但增加額外開銷,不建議使用

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

<select id="com.fashionfree.stat.accesslog.selectSumDistinctCountOfAccessMemberNum"    
 parameterClass="hashMap" resultClass="int">    
 select    
 count(distinct memberId)    
 from    
 MemberAccessLog    
 where    
 accessTimestamp &gt;= #start#    
 and accessTimestamp &lt; #end#    
 and actionId in $actionIdString$    
 </select>  

 <select id="com.fashionfree.stat.accesslog.selectSumDistinctCountOfAccessMemberNum"   
 parameterClass="hashMap" resultClass="int">   
 select   
 count(distinct memberId)   
 from   
 MemberAccessLog   
 where   
 accessTimestamp &gt;= #start#   
 and accessTimestamp &lt; #end#   
 and actionId in $actionIdString$   
 </select>  

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

7.分頁查詢 (pagedQuery)

<select id="com.fashionfree.stat.accesslog.selectMemberAccessLogBy"    
 parameterClass="hashMap" resultMap="MemberAccessLogMap">    
 <include refid="selectAllSql"/>    
 <include refid="whereSql"/>    
 <include refid="pageSql"/>    
 </select>    
 <select id="com.fashionfree.stat.accesslog.selectMemberAccessLogBy.Count"    
 parameterClass="hashMap" resultClass="int">    
 <include refid="countSql"/>    
 <include refid="whereSql"/>    
 </select>    
 <sql id="selectAllSql">    
 select    
 accessLogId, memberId, clientIP,    
 httpMethod, actionId, requestURL,    
 accessTimestamp, extend1, extend2,    
 extend3    
 from    
 MemberAccessLog    
 </sql>    
 <sql id="whereSql">    
 accessTimestamp &lt;= #accessTimestamp#    
 </sql>    
 <sql id="countSql">    
 select    
 count(*)    
 from    
 MemberAccessLog    
 </sql>    
 <sql id="pageSql">    
 <dynamic>    
 <isNotNull property="startIndex">    
 <isNotNull property="pageSize">    
 limit #startIndex# , #pageSize#    
 </isNotNull>    
 </isNotNull>    
 </dynamic>    
 </sql>

 <select id="com.fashionfree.stat.accesslog.selectMemberAccessLogBy"   
 parameterClass="hashMap" resultMap="MemberAccessLogMap">   
 <include refid="selectAllSql"/>   
 <include refid="whereSql"/>   
 <include refid="pageSql"/>   
 </select>   
 <select id="com.fashionfree.stat.accesslog.selectMemberAccessLogBy.Count"   
 parameterClass="hashMap" resultClass="int">   
 <include refid="countSql"/>   
 <include refid="whereSql"/>   
 </select>   
 <sql id="selectAllSql">   
 select   
 accessLogId, memberId, clientIP,   
 httpMethod, actionId, requestURL,   
 accessTimestamp, extend1, extend2,   
 extend3   
 from   
 MemberAccessLog   
 </sql>   
 <sql id="whereSql">   
 accessTimestamp &lt;= #accessTimestamp#   
 </sql>   
 <sql id="countSql">   
 select   
 count(*)   
 from   
 MemberAccessLog   
 </sql>   
 <sql id="pageSql">   
 <dynamic>   
 <isNotNull property="startIndex">   
 <isNotNull property="pageSize">   
 limit #startIndex# , #pageSize#   
 </isNotNull>   
 </isNotNull>   
 </dynamic>   
 </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; 如:

<delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" parameterClass="long">    
 delete from    
 MemberAccessLog    
 where    
 accessTimestamp &lt;= #value#    
 </delete>   
 Xml代碼  
 <delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" parameterClass="long">   
 delete from   
 MemberAccessLog   
 where   
 accessTimestamp &lt;= #value#   
 </delete>   

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

 <delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" parameterClass="long">    
 <![CDATA[   
 delete from   
 MemberAccessLog   
 where   
 accessTimestamp <= #value#   
 ]]>    
 </delete>   

 <delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" parameterClass="long">   
 <![CDATA[  
 delete from  
 MemberAccessLog  
 where  
 accessTimestamp <= #value#  
 ]]>   
 </delete>   

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

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

<sql id="selectBasicSql">    
 select    
 samplingTimestamp,onlineNum,year,    
 month,week,day,hour    
 from    
 OnlineMemberNum    
 </sql>    
 <sql id="whereSqlBefore">    
 where samplingTimestamp &lt;= #samplingTimestamp#    
 </sql>    
 <select id="com.fashionfree.accesslog.selectOnlineMemberNumsBeforeSamplingTimestamp" parameterClass="hashmap" resultClass="OnlineMemberNum">    
 <include refid="selectBasicSql" />    
 <include refid="whereSqlBefore" />    
 </select>   

 <sql id="selectBasicSql">   
 select   
 samplingTimestamp,onlineNum,year,   
 month,week,day,hour   
 from   
 OnlineMemberNum   
 </sql>   
 <sql id="whereSqlBefore">   
 where samplingTimestamp &lt;= #samplingTimestamp#   
 </sql>   
 <select id="com.fashionfree.accesslog.selectOnlineMemberNumsBeforeSamplingTimestamp" parameterClass="hashmap" resultClass="OnlineMemberNum">   
 <include refid="selectBasicSql" />   
 <include refid="whereSqlBefore" />   
 </select>   

注意:sql標簽只能用於被引用,不能當作mapped statement。如上例中有名為selectBasicSql的sql元素,試圖使用其作為sql語句執行是錯誤的:

sqlMapClient.queryForList(“selectBasicSql”); ×

10.隨機選取記錄

<sql id=”randomSql”>   
 ORDER BY rand() LIMIT #number#   
 </sql>
從數據庫中隨機選取number條記錄(只適用於MySQL)

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

<sql id=”selectGroupBy>    
 SELECT    
 a.answererCategoryId, a.answererId, a.answererName,    
 a.questionCategoryId, a.score, a.answeredNum,    
 a.correctNum, a.answerSeconds, a.createdTimestamp,    
 a.lastQuestionApprovedTimestamp, a.lastModified, GROUP_CONCAT(q.categoryName) as categoryName    
 FROM    
 AnswererCategory a, QuestionCategory q    
 WHERE a.questionCategoryId = q.questionCategoryId    
 GROUP BY a.answererId    
 ORDER BY a.answererCategoryId    
 </sql>

 <sql id=”selectGroupBy>   
 SELECT   
 a.answererCategoryId, a.answererId, a.answererName,   
 a.questionCategoryId, a.score, a.answeredNum,   
 a.correctNum, a.answerSeconds, a.createdTimestamp,   
 a.lastQuestionApprovedTimestamp, a.lastModified, GROUP_CONCAT(q.categoryName) as categoryName   
 FROM   
 AnswererCategory a, QuestionCategory q   
 WHERE a.questionCategoryId = q.questionCategoryId   
 GROUP BY a.answererId   
 ORDER BY a.answererCategoryId   
 </sql>  

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

12.按照IN裡面的順序進行排序

①MySQL:

 <sql id=”groupByInArea”>    
 select    
 moduleId, moduleName,    
 status, lastModifierId, lastModifiedName,    
 lastModified    
 from    
 StatModule    
 where    
 moduleId in (3, 5, 1)    
 order by    
 instr(',3,5,1,' , ','+ltrim(moduleId)+',')    
 </sql>   

 <sql id=”groupByInArea”>   
 select   
 moduleId, moduleName,   
 status, lastModifierId, lastModifiedName,   
 lastModified   
 from   
 StatModule   
 where   
 moduleId in (3, 5, 1)   
 order by   
 instr(',3,5,1,' , ','+ltrim(moduleId)+',')   
 </sql>   

②SQLSERVER:

 <sql id=”groupByInArea”>    
 select    
 moduleId, moduleName,    
 status, lastModifierId, lastModifiedName,    
 lastModified    
 from    
 StatModule    
 where    
 moduleId in (3, 5, 1)    
 order by    
 charindex(','+ltrim(moduleId)+',' , ',3,5,1,')    
 </sql>  

 <sql id=”groupByInArea”>   
 select   
 moduleId, moduleName,   
 status, lastModifierId, lastModifiedName,   
 lastModified   
 from   
 StatModule   
 where   
 moduleId in (3, 5, 1)   
 order by   
 charindex(','+ltrim(moduleId)+',' , ',3,5,1,')   
 </sql>

說明:查詢結果將按照moduleId在in列表中的順序(3, 5, 1)來返回

MySQL : instr(str, substr)

SQLSERVER: charindex(substr, str) 返回字符串str 中子字符串的第一個出現位置 ltrim(str) 返回字符串str, 其引導(左面的)空格字符被刪除

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

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

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

14.typeAlias

<typeAlias alias="MemberOnlineDuration" type="com.fashionfree.stat.accesslog.model.MemberOnlineDuration" />    
 <typeAlias>

允許你定義別名,避免重復輸入過長的名字

15.remap

<select id="testForRemap" parameterClass="hashMap" resultClass="hashMap" remapResults="true">    
 select    
 userId    
 <isEqual property="tag" compareValue="1">    
 , userName    
 </isEqual>    
 <isEqual property="tag" compareValue="2">    
 , userPassword    
 </isEqual>    
 from    
 UserInfo    
 </select>   

 <select id="testForRemap" parameterClass="hashMap" resultClass="hashMap" remapResults="true">   
 select   
 userId   
 <isEqual property="tag" compareValue="1">   
 , userName   
 </isEqual>   
 <isEqual property="tag" compareValue="2">   
 , userPassword   
 </isEqual>   
 from   
 UserInfo   
 </select>   

此例中,根據參數tag值的不同,會獲得不同的結果集,如果沒有remapResults="true"屬性,iBatis會將第一次查詢時的結果集緩存,下次再執行時(必須還是該進程中)不會再執行結果集映射,而是會使用緩存的結果集。

因此,如果上面的例子中remapResult為默認的false屬性,而有一段程序這樣書寫:

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

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

16.dynamic標簽的prepend dynamic標簽的prepend屬性作為前綴添加到結果內容前面,當標簽的結果內容為空時,prepend屬性將不起作用

當dynamic標簽中存在prepend屬性時,將會把其嵌套子標簽的第一個prepend屬性忽略。例如:

 <sql id="whereSql">    
 <dynamic prepend="where ">    
 <isNotNull property="userId" prepend="BOGUS">    
 userId = #userId#    
 </isNotNull>    
 <isNotEmpty property="userName" prepend="and ">    
 userName = #userName#    
 </isNotEmpty>    
 </dynamic>    
 </sql>  

 <sql id="whereSql">   
 <dynamic prepend="where ">   
 <isNotNull property="userId" prepend="BOGUS">   
 userId = #userId#   
 </isNotNull>   
 <isNotEmpty property="userName" prepend="and ">   
 userName = #userName#   
 </isNotEmpty>   
 </dynamic>   
 </sql>  

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

注意:當dynamic標簽沒有prepend屬性時,不會自動忽略其子標簽的第一個prepend屬性。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved