首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

iBATIS惯用Sql语句

2012-09-04 
iBATIS常用Sql语句版权所有:中科方德软件有限公司 ( NATIONAL FUNDAMENTAL SOFTWARE OF CHINA ) All Right

iBATIS常用Sql语句
版权所有:中科方德软件有限公司 ( NATIONAL FUNDAMENTAL SOFTWARE OF CHINA ) All Rights Reserved 1/12
iBATIS常用Sql语句
版本1.0
版权所有:中科方德软件有限公司 ( NATIONAL FUNDAMENTAL SOFTWARE OF CHINA ) All Rights Reserved 2/12
文档修订记录
版本编号或者更改记录编号
*变化
状态
简要说明(变更内容和变更范围)
日期
变更人
审核日期
审核人
1.0
新建
2009-4-17
谷涛
1.1
增加
2009-4-28
谷涛
*变化状态:A——增加,M——修改,D——删除
版权所有:中科方德软件有限公司 ( NATIONAL FUNDAMENTAL SOFTWARE OF CHINA ) All Rights Reserved 3/12
(1) 输入参数为单个值
<delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore"
parameterparameteropen="(" close=")" conjunction=",">
#actionIds[]#
</iterate>
</isNotNull>
</dynamic>
</insert>
说明:actionIds为传入的数组的名字;
使用dynamic标签避免数组为空时导致sql语句语法出错;
使用isNotNull标签避免数组为null时ibatis解析出错
版权所有:中科方德软件有限公司 ( NATIONAL FUNDAMENTAL SOFTWARE OF CHINA ) All Rights Reserved 5/12
(5)传递参数只含有一个数组
<select id="com.fashionfree.stat.accesslog.model.StatMemberAction.selectActionIdsOfModule"
resultclose=")" conjunction=",">
#[]#
</iterate>
</dynamic>
order by
moduleId
</select>
说明;注意select的标签中没有parameterClass一项
另:这里也可以把数组放进一个hashMap中,但增加额外开销,不建议使用
(6)让ibatis把参数直接解析成字符串
<select id="com.fashionfree.stat.accesslog.selectSumDistinctCountOfAccessMemberNum"
parameterresultresultMap="MemberAccessLogMap">
<include refid="selectAllSql"/>
<include refid="whereSql"/>
<include refid="pageSql"/>
</select>
<select id="com.fashionfree.stat.accesslog.selectMemberAccessLogBy.Count"
parameterresultparameterparameterparameterresult/>
<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)
版权所有:中科方德软件有限公司 ( NATIONAL FUNDAMENTAL SOFTWARE OF CHINA ) All Rights Reserved 9/12
(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中使用了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>
版权所有:中科方德软件有限公司 ( NATIONAL FUNDAMENTAL SOFTWARE OF CHINA ) All Rights Reserved 10/12
②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>
说明:查询结果将按照moduleId在in列表中的顺序(3, 5, 1)来返回
MySQL : instr(str, substr)
SQLSERVER: charindex(substr, str)
返回字符串str 中子字符串的第一个出现位置
ltrim(str)
返回字符串str, 其引导(左面的)空格字符被删除
(13) resultMap
resultMap负责将SQL查询结果集的列值映射成Java Bean的属性值。
<resultMap 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,提供更严格的配置认证。
版权所有:中科方德软件有限公司 ( NATIONAL FUNDAMENTAL SOFTWARE OF CHINA ) All Rights Reserved 11/12
(14) typeAlias
<typeAlias alias="MemberOnlineDuration" type="com.fashionfree.stat.accesslog.model.MemberOnlineDuration" />
<typeAlias>允许你定义别名,避免重复输入过长的名字。
(15) remap
<select id="testForRemap" parameterresultremapResults="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);
###############################################
则程序会在执行最后一句的query查询时报错,原因就是iBATIS使用了第一次查询时的结果集,而前后两次的结果集是不同的:(userId, userName)和(userId, userPassword),所以导致出错。如果使用了remapResults="true"这一属性,iBATIS会在每次执行查询时都执行结果集
版权所有:中科方德软件有限公司 ( NATIONAL FUNDAMENTAL SOFTWARE OF CHINA ) All Rights Reserved 12/12
映射,从而避免错误的发生(此时会有较大的开销)。
(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>
此例中,dynamic标签中含有两个子标签<isNotNull>和<isNotEmpty>。根据前面叙述的原则,如果<isNotNull>标签中没有prepend="BOGUS" 这一假的属性来让dynamic去掉的话,<isNotEmpty>标签中的and就会被忽略,会造成sql语法错误。
注意:当dynamic标签没有prepend属性时,不会自动忽略其子标签的第一个prepend属性。

热点排行
Bad Request.