84parameterClass="hashMap"resultClass="int">
85
86
87
88
89select
90accessLogId,memberId,clientIP,
91httpMethod,actionId,requestURL,
92accessTimestamp,extend1,extend2,
93extend3
94from
95MemberAccessLog
96
97
98accessTimestamp<=#accessTimestamp#
99
100
101select
102count(*)
103from
104MemberAccessLog
105
106
107
108
109
110limit#startIndex#,#pageSize#
111
112
113
114
说明:
本例中,代码应为:
HashMaphashMap=newHashMap();
hashMap.put(“accessTimestamp”,someValue);
pagedQuery(“com.fashionfree.stat.accesslog.selectMemberAccessLogBy”,hashMap);
pagedQuery方法首先去查找名为com.fashionfree.stat.accesslog.selectMemberAccessLogBy.Count的mappedstatement来进行sql查询,从而得到com.fashionfree.stat.accesslog.selectMemberAccessLogBy查询的记录个数,
再进行所需的pagedsql查询(com.fashionfree.stat.accesslog.selectMemberAccessLogBy),具体过程参见utils类中的相关代码
(8)sql语句中含有大于号>、小于号<
1.将大于号、小于号写为:
><如:
Xml代码
115
116deletefrom
117MemberAccessLog
118where
119accessTimestamp<=#value#
120
2.将特殊字符放在xml的CDATA区内:
Xml代码
121
122
[CDATA[
123deletefrom
124MemberAccessLog
125where
126accessTimestamp<=#value#
127]]>
128
推荐使用第一种方式,写为<和>(XML不对CDATA里的内容进行解析,因此如果CDATA中含有dynamic标签,将不起作用)
(9)include和sql标签
将常用的sql语句整理在一起,便于共用:
Xml代码
129
130select
131samplingTimestamp,onlineNum,year,
132month,week,day,hour
133from
134OnlineMemberNum
135
136
137wheresamplingTimestamp<=#samplingTimestamp#
138
139
140
141
142
注意:
sql标签只能用于被引用,不能当作mappedstatement。
如上例中有名为selectBasicSql的sql元素,试图使用其作为sql语句执行是错误的:
sqlMapClient.queryForList(“selectBasicSql”);×
(10)随机选取记录
Xml代码
143
144ORDERBYrand()LIMIT#number#
145
从数据库中随机选取number条记录(只适用于MySQL)
(11)将SQLGROUPBY分组中的字段拼接
Xml代码
146
147SELECT
148a.answererCategoryId,a.answererId,a.answererName,
149a.questionCategoryId,a.score,a.answeredNum,
150a.correctNum,a.answerSeconds,a.createdTimestamp,
151a.lastQuestionApprovedTimestamp,a.lastModified,GROUP_CONCAT(q.categoryName)ascategoryName
152FROM
153AnswererCategorya,QuestionCategoryq
154WHEREa.questionCategoryId=q.questionCategoryId
155GROUPBYa.answererId
156ORDERBYa.answererCategoryId
157
注:
SQL中使用了MySQL的GROUP_CONCAT函数
(12)按照IN里面的顺序进行排序
①MySQL:
Xml代码
158
159select
160moduleId,moduleName,
161status,lastModifierId,lastModifiedName,
162lastModified
163from
164StatModule
165where
166moduleIdin(3,5,1)
167orderby
168instr(',3,5,1,',','+ltrim(moduleId)+',')
169
②SQLSERVER:
Xml代码
170
171select
172moduleId,moduleName,
173status,lastModifierId,lastModifiedName,
174lastModified
175from
176StatModule
177where
178moduleIdin(3,5,1)
179orderby
180charindex(','+ltrim(moduleId)+',',',3,5,1,')
181
说明:
查询结果将按照moduleId在in列表中的顺序(3,5,1)来返回
MySQL:
instr(str,substr)
SQLSERVER:
charindex(substr,str)
返回字符串str中子字符串的第一个出现位置
ltrim(str)
返回字符串str,其引导(左面的)空格字符被删除
(13)resultMap
resultMap负责将SQL查询结果集的列值映射成JavaBean的属性值。
Xml代码
182
183
184
185
使用resultMap称为显式结果映射,与之对应的是resultClass(内联结果映射),使用resultClass的最大好处便是简单、方便,不需显示指定结果,由iBATIS根据反射来确定自行决定。
而resultMap则可以通过指定jdbcType和javaType,提供更严格的配置认证。
(14)typeAlias
Xml代码
186
187允许你定义别名,避免重复输入过长的名字。
(15)remap
Xml代码
188
189select
190userId
191
192,userName
193
194
195,userPassword
196
197from
198UserInfo
199
此例中,根据参数tag值的不同,会获得不同的结果集,如果没有remapResults="true"属性,iBatis会将第一次查询时的结果集缓存,下次再执行时(必须还是该进程中)不会再执行结果集映射,而是会使用缓存的结果集。
因此,如果上面的例子中remapResult为默认的false属性,而有一段程序这样书写:
Java代码
200HashMaphashMap=newHashMap();
201hashMap.put("tag",1);
202sqlClient.queryForList("testForRemap",hashMap);
203hashMap.put("tag",2);
204sqlClient.queryForList("testForRemap",hashMap);
则程序会在执行最后一句的query查询时报错,原因就是iBATIS使用了第一次查询时的结果集,而前后两次的结果集是不同的:
(userId,userName)和(userId,userPassword),所以导致出错。
如果使用了remapResults="true"这一属性,iBATIS会在每次执行查询时都执行结果集映射,从而避免错误的发生(此时会有较大的开销)。
(16)dynamic标签的prepend
dynamic标签的prepend属性作为前缀添加到结果内容前面,当标签的结果内容为空时,prepend属性将不起作用。
当dynamic标签中存在prepend属性时,将会把其嵌套子标签的第一个prepend属性忽略。
例如:
Xml代码
205
206
207
208userId=#userId#
209
210
211userName=#userName#
212
213
214
此例中,dynamic标签中含有两个子标签和。
根据前面叙述的原则,如果标签中没有prepend="BOGUS"这一假的属性来让dynamic去掉的话,标签中的and就会被忽略,会造成sql语法错误。
注意:
当dynamic标签没有prepend属性时,不会自动忽略其子标签的第一个prepend属性。