iBatis習(xí)慣用的16條SQL語句
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è)值
<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ì)象
<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
<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.輸入?yún)?shù)中含有數(shù)組
<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ù)組
<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ù)直接解析成字符串
<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注入的風(fēng)險(xiǎn),不推薦使用
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>
說明:本例中,代碼應(yīng)為:
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)寫為: > < 如:
<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區(qū)內(nèi):
<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不對(duì)CDATA里的內(nèi)容進(jìn)行解析,因此如果CDATA中含有dynamic標(biāo)簽,將不起作用)
9.include和sql標(biāo)簽 將常用的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標(biāo)簽只能用于被引用,不能當(dāng)作mapped statement。如上例中有名為selectBasicSql的sql元素,試圖使用其作為sql語句執(zhí)行是錯(cuò)誤的:
sqlMapClient.queryForList(“selectBasicSql”); ×
10.隨機(jī)選取記錄
<sql id=”randomSql”> ORDER BY rand() LIMIT #number# </sql>
從數(shù)據(jù)庫中隨機(jī)選取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函數(shù)
12.按照IN里面的順序進(jìn)行排序
①M(fèi)ySQL:
<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>
說明:查詢結(jié)果將按照moduleId在in列表中的順序(3, 5, 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的屬性值
<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
<typeAlias alias="MemberOnlineDuration" type="com.fashionfree.stat.accesslog.model.MemberOnlineDuration" /> <typeAlias>
允許你定義別名,避免重復(fù)輸入過長(zhǎng)的名字
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>
此例中,根據(jù)參數(shù)tag值的不同,會(huì)獲得不同的結(jié)果集,如果沒有remapResults="true"屬性,iBatis會(huì)將第一次查詢時(shí)的結(jié)果集緩存,下次再執(zhí)行時(shí)(必須還是該進(jìn)程中)不會(huì)再執(zhí)行結(jié)果集映射,而是會(huì)使用緩存的結(jié)果集。
因此,如果上面的例子中remapResult為默認(rèn)的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);
則程序會(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屬性忽略。例如:
<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ǎng)站的支持!
- java~springboot~ibatis數(shù)組in查詢的實(shí)現(xiàn)方法
- ibatis遷移到mybatis3的注意事項(xiàng)
- 通過ibatis解決sql注入問題
- ibatis學(xué)習(xí)之搭建Java項(xiàng)目
- Mybatis報(bào)錯(cuò): org.apache.ibatis.exceptions.PersistenceException解決辦法
- Mybatis與Ibatis的區(qū)別
- Ibatis.net結(jié)合oracle批量刪除實(shí)現(xiàn)代碼
- ibatis簡(jiǎn)單實(shí)現(xiàn)與配置
- ibatis結(jié)合oracle批量插入三種方法的測(cè)評(píng)
相關(guān)文章
IDEA 開發(fā)配置SparkSQL及簡(jiǎn)單使用案例代碼
這篇文章主要介紹了IDEA 開發(fā)配置SparkSQL及簡(jiǎn)單使用案例代碼,本文通過代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08淺談Java中Collection和Collections的區(qū)別
下面小編就為大家?guī)硪黄獪\談Java中Collection和Collections的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08啟用設(shè)置org.slf4j.Logger打印并輸出日志方式
這篇文章主要介紹了啟用設(shè)置org.slf4j.Logger打印并輸出日志方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11SpringBoot多級(jí)緩存實(shí)現(xiàn)方案總結(jié)
所謂多級(jí)緩存,是指在整個(gè)系統(tǒng)架構(gòu)的不同系統(tǒng)層面進(jìn)行數(shù)據(jù)緩存,以提升訪問速度,多級(jí)緩存就是為了解決項(xiàng)目服務(wù)中單一緩存使用不足的缺點(diǎn),本文我們將給大家總結(jié)了SpringBoot多級(jí)緩存實(shí)現(xiàn)方案,需要的朋友可以參考下2023-08-08Java mutable對(duì)象和immutable對(duì)象的區(qū)別說明
這篇文章主要介紹了Java mutable對(duì)象和immutable對(duì)象的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot+SpringCache實(shí)現(xiàn)兩級(jí)緩存(Redis+Caffeine)
這篇文章主要介紹了SpringBoot+SpringCache實(shí)現(xiàn)兩級(jí)緩存(Redis+Caffeine),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04