一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - iBatis習(xí)慣用的16條SQL語句

iBatis習(xí)慣用的16條SQL語句

2020-06-28 11:19huiy_寧?kù)o而致遠(yuǎn) JAVA教程

iBatis 是apache 的一個(gè)開源項(xiàng)目,一個(gè)O/R Mapping 解決方案,iBatis 最大的特點(diǎn)就是小巧,上手很快.這篇文章主要介紹了iBatis習(xí)慣用的16條SQL語句的相關(guān)資料,需要的朋友可以參考下

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 &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.輸入?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 &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注入的風(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 &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>

說明:本例中,代碼應(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)寫為: &gt; &lt; 如:

?
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 &lt;= #value#
</delete>
Xml代碼
<delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" parameterClass="long">
delete from
MemberAccessLog
where
accessTimestamp &lt;= #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>

推薦使用第一種方式,寫為&lt; 和 &gt; (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 &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標(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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: yellow高清视频日本动漫 | 5g影院天天| 国产一区日韩二区欧美三区 | 99精品国产成人一区二区在线 | 啊好大好粗| 亚洲国产中文字幕在线视频综合 | 国产欧美日韩不卡一区二区三区 | jzzjzz视频免费播放 | 天美传媒在线视频 | meyd–456佐山爱在线播放 | 亚洲欧美日韩特级毛片 | w7w7w7w7w免费| 丰满的闺蜜2中文字幕 | 久久视频在线视频观看天天看视频 | 国产chinese男男gaygay | 蜜色影院 | 亚洲天堂影院在线观看 | 丝袜足控免费网站xx动漫漫画 | 亚洲咪咪| 91在线精品老司机免费播放 | 欧美日韩精品免费一区二区三区 | 丝袜护士强制脚足取精 | 日本三级欧美三级人妇英文 | 91免费精品国自产拍在线不卡 | 成人资源在线观看 | 我的好妈妈7中字在线观看韩国 | 公交车上插入 | 荡娃艳妇系列小说 | 国产成人亚洲精品一区二区在线看 | 婷婷综合七月激情啪啪 | 啊哈~嗯哼~用力cao我小说 | 亚洲天堂h | 99久久国产综合精麻豆 | 国产免费美女视频 | 日本高清有码视频 | 4hc44四虎永久地址链接 | 超h 超重口 高h 污肉1v1 | 欧美xxxxxbb| 日韩精品欧美激情国产一区 | 天天色踪合合 | 日韩免费毛片视频杨思敏 |