標籤:img 部落格 images 批量 方法 log each 寫法 bar
原文:http://www.cnblogs.com/edwinchen/p/4105278.html?utm_source=tuicool&utm_medium=referral一、動態SQL
相信大家在用mybatis操作資料庫時時都會碰到一個問題,假如現在我們有一個關於作者的list authorList,需要根據authorList裡已有的作者資訊在資料庫中查詢相應作者的部落格資訊。那麼最容易想到的做法就是遍曆authorList,擷取相應的資訊查詢資料庫。
for(int i=0;I < authorList.size();i++) { …… //查詢資料庫代碼 //select * from blog where author=#{author,jdbcType=VARCHAR}}
想一想,如果假設authorList的長度為N,那麼我們就需要查詢N次資料庫,如果用這種方法,程式的開銷不僅僅是查詢,還有從資料庫連接池中取出串連執行個體、建立資料庫連接、將資料庫執行個體返還給資料庫連接池,假設這三個動作加起來總共用時0.001秒。那麼採取遍曆的辦法查詢,將會多耗時0.001N秒,如果需要查詢1000次,那麼將多1秒鐘的時間,對於程式猿來說,這是不可忍受的,因為這隻是一個迴圈查詢,還不算其它的業務代碼。
那麼,有沒有更好的辦法呢,答案是肯定,其中之一是動態SQL:
先上代碼:
<select id="dynamicForeachTest" resultType="com.blog.Blog" parameterType="java.util.List"> select * from blog where author in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach></select>
tem表示集合中每一個元素進行迭代時的別名,
index指定一個名字,用於表示在迭代過程中,每次迭代到的位置,
open表示該語句以什麼開始,
separator表示在每次進行迭代之間以什麼符號作為分隔字元,
close表示以什麼結束這樣傳回值就可以用List<Bolg>接受.
但是動態SQL中的foreach語句用的最多的實在insert語句中,並且通常在in子句中使用。
二、進階映射
在使用mybatis的時候,一般是使用resultType = com.blog.author 實體類來接受查詢結果
或者是使用resultType = java.util.map將資料庫列名作為key,記錄值作為value返回。
但是這次需要使用resultMap,它可以允許自由組合傳回值的形式,用以處理更複雜的查詢。
還是先上代碼:
SQL:
<select id="getBlogs" resultMap=" blogs " parameterType="map"> Select a.authorID, a.uthorName, b.blogID, b.blogName from author a left join blog b on a. authorID=b. authorID where a. authorID = #{authorID,jdbcType=INTEGER}</select>
mybatis配置:
<resultMap id="blogs" type="com.bloh.Blog"> <id property="authorID" column=" authorID"> <result property="authorName" column=" authorName"> <collection property="postsList" ofType="com.bolg.Post"> <id property="blogID" column=" blogID"/> <result property="blogName" column="blogName"/> </collection> </resultMap>
Blog實體類
Public class Bolg { private Integer authorID; private String authorName; private List<Post> postsList; //setter getter}
Post實體類
Public class Post { private Integer blogID; private String blogName; //setter getter}
這樣就可以用一個實體接受一個複雜查詢了。
下面再介紹下各個屬性的作用:
其它和普通mybatis查詢的屬性和配置就不細說了,
resultMap用來代替resultType,表示查詢結果返回的格式
resultMap中的id主要有兩個作用:
- 類似索引,提高查詢效能
- 區分不同結果
所以id最好不要省略,如果沒有主鍵,用能唯一區分記錄的欄位代替
result即實體類中定義的變數名,column是資料庫的列名
collection 就是列表、map等集合
postsList就是在Blog實體類中定義的list變數名
ofType就是對象列表中對象的實體類。
三、獲得自增ID:
如果有如下情況,在插入資料庫記錄後,想得到插入記錄的主鍵,用以後面的業務代碼
那麼mybatis針對這種情況也提供了相應的支援(不支援批量插入):
MySQL是原聲自增ID;假設自增主鍵的欄位名就為ID
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="User">insert into <include refid="TABLE_NAME" /> ( NAME, AGE )values ( #{name}, #{age} )</insert>
比普通的插入就多了兩個屬性 useGeneratedKeys="true" 表示開啟返回自增ID
keyProperty="id" 表示返回主鍵的名字。
那麼在業務代碼中就可以用下列語句接收:
假設實體類為User
User userNew = userMapper.insert(user);userNew.getID //即為插入後的自增ID
其實,mysql的自增主鍵可以用select LAST_INSERT_ID();來得到,
所以,還有一種寫法:
<insert id="insert" parameterType="User"><selectKey resultType="int" order="AFTER" keyProperty="id">SELECT LAST_INSERT_ID() AS id</selectKey>insert into name,agevalues ( #{name}, #{age} )</insert>
和mysql的擷取主鍵方式剛好相反,mysql是insert執行後由表分配自增長的值,而oracle是擷取到自增長的值後再進行插入記錄操作,在執行insert sql前必須指定一個主索引值給要插入的記錄所以要要在"BEFORE"的時候拿到自增的序列,然後用selectKey的方式注入到入參映射中即可。假設自增長還是id
<insert id=" insert " useGeneratedKeys="true" keyProperty="id" parameterType="xxxx" ><selectKey resultType="int" order="BEFORE" keyProperty="id">SELECT SEQ_TABLE.NEXTVAL FROM dual</selectKey>INSERT INTO id,name,ageVALUES(#{id} #{name}, #{age} )</insert>
這裡的id就是selectKey獲得的自增id。
接收方式和mysql一樣,在擷取自增主鍵時,最好使用實體接收。
(轉)Mybatis進階映射、動態SQL及獲得自增主鍵