iBatis 簡(jiǎn)介:
iBatis 是apache 的一個(gè)開源項(xiàng)目,一個(gè)O/R Mapping 解決方案,iBatis 最大的特點(diǎn)就是小巧,上手很快。如果不需要太多復(fù)雜的功能,iBatis 是能夠滿足你的要求又足夠靈活的最簡(jiǎn)單的解決方案,現(xiàn)在的iBatis 已經(jīng)改名為Mybatis 了。
官網(wǎng)為:http://www.mybatis.org/
1.輸入?yún)?shù)為單個(gè)值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<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.輸入?yún)?shù)為一個(gè)對(duì)象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<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.輸入?yún)?shù)為一個(gè)java.util.HashMap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<select id= "com.fashionfree.stat.accesslog.selectActionIdAndActionNumber" parameterClass= "hashMap" resultMap= "getActionIdAndActionNumber" > select actionId, count(*) as count from MemberAccessLog where memberId = #memberId# and accessTimestamp > #start# and accessTimestamp <= #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 > #start# and accessTimestamp <= #end# group by actionId </select> |
4.輸入?yún)?shù)中含有數(shù)組
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<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為傳入的數(shù)組的名字; 使用dynamic標(biāo)簽避免數(shù)組為空時(shí)導(dǎo)致sql語句語法出錯(cuò); 使用isNotNull標(biāo)簽避免數(shù)組為null時(shí)ibatis解析出錯(cuò)
5.傳遞參數(shù)只含有一個(gè)數(shù)組
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<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的標(biāo)簽中沒有parameterClass一項(xiàng)
另:這里也可以把數(shù)組放進(jìn)一個(gè)hashMap中,但增加額外開銷,不建議使用
6.讓ibatis把參數(shù)直接解析成字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<select id= "com.fashionfree.stat.accesslog.selectSumDistinctCountOfAccessMemberNum" parameterClass= "hashMap" resultClass= "int" > select count(distinct memberId) from MemberAccessLog where accessTimestamp >= #start# and accessTimestamp < #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 >= #start# and accessTimestamp < #end# and actionId in $actionIdString$ </select> |
說明:使用這種方法存在sql注入的風(fēng)險(xiǎn),不推薦使用
7.分頁(yè)查詢 (pagedQuery)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<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 <= #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 <= #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> |
說明:本例中,代碼應(yīng)為:
1
2
3
|
HashMap hashMap = new HashMap(); hashMap.put(“accessTimestamp”, someValue); pagedQuery(“com.fashionfree.stat.accesslog.selectMemberAccessLogBy”, hashMap); |
pagedQuery方法首先去查找名為com.fashionfree.stat.accesslog.selectMemberAccessLogBy.Count 的mapped statement來進(jìn)行sql查詢,從而得到com.fashionfree.stat.accesslog.selectMemberAccessLogBy查詢的記錄個(gè)數(shù), 再進(jìn)行所需的paged sql查詢(com.fashionfree.stat.accesslog.selectMemberAccessLogBy),具體過程參見utils類中的相關(guān)代碼
8.sql語句中含有大于號(hào)>、小于號(hào)< 1. 將大于號(hào)、小于號(hào)寫為: > < 如:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<delete id= "com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" parameterClass= "long" > delete from MemberAccessLog where accessTimestamp <= #value# </delete> Xml代碼 <delete id= "com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" parameterClass= "long" > delete from MemberAccessLog where accessTimestamp <= #value# </delete> |
將特殊字符放在xml的CDATA區(qū)內(nèi):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<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> |
推薦使用第一種方式,寫為< 和 > (XML不對(duì)CDATA里的內(nèi)容進(jìn)行解析,因此如果CDATA中含有dynamic標(biāo)簽,將不起作用)
9.include和sql標(biāo)簽 將常用的sql語句整理在一起,便于共用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<sql id= "selectBasicSql" > select samplingTimestamp,onlineNum,year, month,week,day,hour from OnlineMemberNum </sql> <sql id= "whereSqlBefore" > where samplingTimestamp <= #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 <= #samplingTimestamp# </sql> <select id= "com.fashionfree.accesslog.selectOnlineMemberNumsBeforeSamplingTimestamp" parameterClass= "hashmap" resultClass= "OnlineMemberNum" > <include refid= "selectBasicSql" /> <include refid= "whereSqlBefore" /> </select> |
注意:sql標(biāo)簽只能用于被引用,不能當(dāng)作mapped statement。如上例中有名為selectBasicSql的sql元素,試圖使用其作為sql語句執(zhí)行是錯(cuò)誤的:
1
|
sqlMapClient.queryForList(“selectBasicSql”); × |
10.隨機(jī)選取記錄
1
2
3
|
<sql id=”randomSql”> ORDER BY rand() LIMIT #number# </sql> |
從數(shù)據(jù)庫(kù)中隨機(jī)選取number條記錄(只適用于MySQL)
11.將SQL GROUP BY分組中的字段拼接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<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函數(shù)
12.按照IN里面的順序進(jìn)行排序
①M(fèi)ySQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<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> |
說明:查詢結(jié)果將按照moduleId在in列表中的順序(3, 5, 1)來返回
1
|
MySQL : instr(str, substr) |
SQLSERVER: charindex(substr, str) 返回字符串str 中子字符串的第一個(gè)出現(xiàn)位置 ltrim(str) 返回字符串str, 其引導(dǎo)(左面的)空格字符被刪除
13.resultMap resultMap負(fù)責(zé)將SQL查詢結(jié)果集的列值映射成Java Bean的屬性值
1
2
3
4
5
6
7
8
9
|
<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稱為顯式結(jié)果映射,與之對(duì)應(yīng)的是resultClass(內(nèi)聯(lián)結(jié)果映射),使用resultClass的最大好處便是簡(jiǎn)單、方便,不需顯示指定結(jié)果,由iBATIS根據(jù)反射來確定自行決定。而resultMap則可以通過指定jdbcType和javaType,提供更嚴(yán)格的配置認(rèn)證。
14.typeAlias
1
2
|
<typeAlias alias= "MemberOnlineDuration" type= "com.fashionfree.stat.accesslog.model.MemberOnlineDuration" /> <typeAlias> |
允許你定義別名,避免重復(fù)輸入過長(zhǎng)的名字
15.remap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<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> |
此例中,根據(jù)參數(shù)tag值的不同,會(huì)獲得不同的結(jié)果集,如果沒有remapResults="true"屬性,iBatis會(huì)將第一次查詢時(shí)的結(jié)果集緩存,下次再執(zhí)行時(shí)(必須還是該進(jìn)程中)不會(huì)再執(zhí)行結(jié)果集映射,而是會(huì)使用緩存的結(jié)果集。
因此,如果上面的例子中remapResult為默認(rèn)的false屬性,而有一段程序這樣書寫:
1
2
3
4
5
|
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代碼
1
2
3
4
5
|
HashMap<String, Integer> hashMap = new HashMap<String, Integer>(); hashMap.put( "tag" , 1 ); sqlClient.queryForList( "testForRemap" , hashMap); hashMap.put( "tag" , 2 ); sqlClient.queryForList( "testForRemap" , hashMap); |
則程序會(huì)在執(zhí)行最后一句的query查詢時(shí)報(bào)錯(cuò),原因就是iBATIS使用了第一次查詢時(shí)的結(jié)果集,而前后兩次的結(jié)果集是不同的:(userId, userName)和(userId, userPassword),所以導(dǎo)致出錯(cuò)。如果使用了remapResults="true"這一屬性,iBATIS會(huì)在每次執(zhí)行查詢時(shí)都執(zhí)行結(jié)果集映射,從而避免錯(cuò)誤的發(fā)生(此時(shí)會(huì)有較大的開銷)。
16.dynamic標(biāo)簽的prepend dynamic標(biāo)簽的prepend屬性作為前綴添加到結(jié)果內(nèi)容前面,當(dāng)標(biāo)簽的結(jié)果內(nèi)容為空時(shí),prepend屬性將不起作用。
當(dāng)dynamic標(biāo)簽中存在prepend屬性時(shí),將會(huì)把其嵌套子標(biāo)簽的第一個(gè)prepend屬性忽略。例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<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標(biāo)簽中含有兩個(gè)子標(biāo)簽<isNotNull>和<isNotEmpty>。根據(jù)前面敘述的原則,如果<isNotNull>標(biāo)簽中沒有prepend="BOGUS" 這一假的屬性來讓dynamic去掉的話,<isNotEmpty>標(biāo)簽中的and就會(huì)被忽略,會(huì)造成sql語法錯(cuò)誤。
注意:當(dāng)dynamic標(biāo)簽沒有prepend屬性時(shí),不會(huì)自動(dòng)忽略其子標(biāo)簽的第一個(gè)prepend屬性。
以上所述是小編給大家介紹的iBatis習(xí)慣用的16條SQL語句,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/huiy/archive/2016/10/26/6000676.html