转:ibatis常用16条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 > #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.输入参数中含有数组
<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 >= #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注入的风险,不推荐使用
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 <= #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> 说明:本例中,代码应为: 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. 将大于号、小于号写为: > < 如:
<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区内: <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不对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 <= #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标签只能用于被引用,不能当作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属性。
转:ibatis常用16条SQL语句的更多相关文章
- ibatis常用16条SQL语句
(1) 输入参数为单个值 <delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" p ...
- 常用的几条sql语句
### 常用的几条sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,v ...
- 关联映射、关联查询【重点掌握一条SQL语句的那种方法】
1 什么叫关联映射 通过数据库对象之间的关联关系(一对一.一对多.多对多),反映到实体对象上之间的引用. 举例 用户实体类(User):user_id user_name user_token 笔记本 ...
- MyBatis7:MyBatis插件及示例----打印每条SQL语句及其执行时间
Plugins 摘一段来自MyBatis官方文档的文字. MyBatis允许你在某一点拦截已映射语句执行的调用.默认情况下,MyBatis允许使用插件来拦截方法调用 Executor(update.q ...
- SQL反模式学习笔记18 减少SQL查询数据,避免使用一条SQL语句解决复杂问题
目标:减少SQL查询数据,避免使用一条SQL语句解决复杂问题 反模式:视图使用一步操作,单个SQL语句解决复杂问题 使用一个查询来获得所有结果的最常见后果就是产生了一个笛卡尔积.导致查询性能降低. 如 ...
- 一条SQL语句在MySQL中如何执行的
本篇文章会分析一个 sql 语句在 MySQL 中的执行流程,包括 sql 的查询在 MySQL 内部会怎么流转,sql 语句的更新是怎么完成的. 在分析之前我会先带着你看看 MySQL 的基础架构, ...
- Oracle种常用性能监控SQL语句
--Oracle常用性能监控SQL语句 --1 SELECT * FROM SYS.V_$SQLAREA WHERE DISK_READS > 100; --2 监控事例的等待 SELECT E ...
- 一条SQL语句在MySQL中是如何执行的
概览 本篇文章会分析下一个sql语句在mysql中的执行流程,包括sql的查询在mysql内部会怎么流转,sql语句的更新是怎么完成的. 一.mysql架构分析 mysql主要分为Server层和存储 ...
- 一条SQL语句是如何执行的?--Mysql45讲笔记记录 打卡day1
写在前面的话:回想以前上班的时候,空闲时间还是挺多的,但是都荒废了.如今找工作着实费劲了.但是这段时间在极客时间买了mysql45讲,就好像发现了新大陆一样,这是我认真做笔记的第一天,说实话第一讲我已 ...
随机推荐
- C#中使用Oracle存储过程返回结果集
问题: 在MSSQLServer中定义的存储过程可以直接返回一个数据集,如: create procedure sp_getAllEmployees as SELECT * FROM [NORTHWN ...
- Animo.js :一款管理 CSS 动画的强大的小工具
Animo.js 是一个功能强大的小工具,用于管理 CSS 动画.它的特色功能包括像堆栈动画,创建跨浏览器的模糊,设置动画完成的回调等等.Animo 还包括惊人的 animate.css,为您提供了近 ...
- 解决Fedora解压文件产生乱码的问题
最近有发现在使用Linux的时候,之前也遇到过在ubuntu下,最后ubuntu貌似在原生下优化了这个问题,现在换到了fedora上的时候问题又出现了,解压出来的文件中文乱码. 为了在linux下可以 ...
- Java进阶之reflection(反射机制)——反射概念与基础
反射机制是Java动态性之一,而说到动态性首先得了解动态语言.那么何为动态语言? 一.动态语言 动态语言,是指程序在运行时可以改变其结构:新的函数可以引进,已有的函数可以被删除等结构上的变化.比如常见 ...
- 【原创】Lucene.Net+盘古分词器(详细介绍)
本章阅读概要 1.Lucenne.Net简介 2.介绍盘古分词器 3.Lucene.Net实例分析 4.结束语(Demo下载) Lucene.Net简介 Lucene.net是Lucene的.net移 ...
- 安装部署Windows服务脚本
@echo off Installutil.exe 程序目录 F:\test\TestWindows.exe 服务程序目录 @sc start "服务名称" @sc config ...
- .NET Core 调用WCF 服务
.NET Core 和ASP.NET Core 已经可以调用WCF 服务. 环境要求: VS2015 Update 2 +VS2015 Tooling + .NET Core SDK 下载地址: ht ...
- 这几天做完简易酒店管理系统,对Sql Server执行计划的浅显了解。
我是一名大三的小学生,今天开始我的第一篇博客,最近随便做了一个简易的酒店管理系统,对sql执行计划有了初步的了解. 查看上面语句的预估执行计划,在工具栏中有这个按钮 聚集索引扫描被称为Index Sc ...
- mysql 批量插入数据
MySQL使用INSERT插入多条记录,应该如何操作呢?下面就为您详细介绍MySQL使用INSERT插入多条记录的实现方法,供您参考. 看到这个标题也许大家会问,这有什么好说的,调用多次INSERT语 ...
- html5学习笔记(2)
效果图 以下为源码 <!DOCTYPE html> <html> <head lang="en"> <meta charset=" ...