update Question set status = #status##actionIds[]#update Question set status = #status##actionIds[]#说明:actionIds为传入的数组的名字; 使用dynamic标签避免数组为空时导致sql语句语法出错; 使用isNotNull标签避免数组为null时ibatis解析出错
说明:使用这种方法存在sql注入的风险,不推荐使用7.分页查询 (pagedQuery)select accessLogId, memberId, clientIP,httpMethod, actionId, requestURL,accessTimestamp, extend1, extend2,extend3 from MemberAccessLogaccessTimestamp <= #accessTimestamp#select count(*) from MemberAccessLoglimit #startIndex# , #pageSize#select accessLogId, memberId, clientIP,httpMethod, actionId, requestURL,accessTimestamp, extend1, extend2,extend3 from MemberAccessLogaccessTimestamp <= #accessTimestamp#select count(*) from MemberAccessLoglimit #startIndex# , #pageSize#
说明:本例中,代码应为:
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 from MemberAccessLog where accessTimestamp <= #value#Xml代码delete from MemberAccessLog where accessTimestamp <= #value#将特殊字符放在xml的CDATA区内:推荐使用第一种方式,写为< 和 > (XML不对CDATA里的内容进行解析,因此如果CDATA中含有dynamic标签,将不起作用) 9.include和sql标签 将常用的sql语句整理在一起,便于共用:select samplingTimestamp,onlineNum,year,month,week,day,hour from OnlineMemberNumwhere samplingTimestamp <= #samplingTimestamp#select samplingTimestamp,onlineNum,year,month,week,day,hour from OnlineMemberNumwhere samplingTimestamp <= #samplingTimestamp#注意:sql标签只能用于被引用,不能当作mapped statement。如上例中有名为selectBasicSql的sql元素,试图使用其作为sql语句执行是错误的:sqlMapClient.queryForList(“selectBasicSql”); ×
10.随机选取记录
ORDER BY rand() LIMIT #number#从数据库中随机选取number条记录(只适用于MySQL)
11.将SQL GROUP BY分组中的字段拼接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.answererCategoryIdSELECT 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中使用了MySQL的GROUP_CONCAT函数
12.按照IN里面的顺序进行排序
①MySQL:select moduleId, moduleName,status, lastModifierId, lastModifiedName,lastModified from StatModule where moduleId in (3, 5, 1) order by instr(',3,5,1,' , ','+ltrim(moduleId)+',')select moduleId, moduleName,status, lastModifierId, lastModifiedName,lastModified from StatModule where moduleId in (3, 5, 1) order by instr(',3,5,1,' , ','+ltrim(moduleId)+',')②SQLSERVER:select moduleId, moduleName,status, lastModifierId, lastModifiedName,lastModified from StatModule where moduleId in (3, 5, 1) order by charindex(','+ltrim(moduleId)+',' , ',3,5,1,')select moduleId, moduleName,status, lastModifierId, lastModifiedName,lastModified from StatModule where moduleId in (3, 5, 1) order by charindex(','+ltrim(moduleId)+',' , ',3,5,1,')说明:查询结果将按照moduleId在in列表中的顺序(3, 5, 1)来返回MySQL : instr(str, substr)SQLSERVER: charindex(substr, str) 返回字符串str 中子字符串的第一个出现位置 ltrim(str) 返回字符串str, 其引导(左面的)空格字符被删除13.resultMap resultMap负责将SQL查询结果集的列值映射成Java Bean的属性值Xml代码使用resultMap称为显式结果映射,与之对应的是resultClass(内联结果映射),使用resultClass的最大好处便是简单、方便,不需显示指定结果,由iBATIS根据反射来确定自行决定。而resultMap则可以通过指定jdbcType和javaType,提供更严格的配置认证。