MyBatis 本是apache的一個開源項目iBatis, 2010年這個項目由apache software foundation 遷移到了google code,并且改名為MyBatis 。下文給大家介紹Mybatis高級映射、動態(tài)SQL及獲得自增主鍵的內容,具體詳情請參考本文。
一、動態(tài)SQL
相信大家在用mybatis操作數(shù)據(jù)庫時時都會碰到一個問題,假如現(xiàn)在我們有一個關于作者的list authorList,需要根據(jù)authorList里已有的作者信息在數(shù)據(jù)庫中查詢相應作者的博客信息。那么最容易想到的做法就是遍歷authorList,獲取相應的信息查詢數(shù)據(jù)庫。
1
2
3
4
5
|
for ( int i=0;I < authorList. size ();i++) { …… //查詢數(shù)據(jù)庫代碼 // select * from blog where author=#{author,jdbcType= VARCHAR } } |
想一想,如果假設authorList的長度為N,那么我們就需要查詢N次數(shù)據(jù)庫,如果用這種方法,程序的開銷不僅僅是查詢,還有從數(shù)據(jù)庫連接池中取出連接實例、建立數(shù)據(jù)庫連接、將數(shù)據(jù)庫實例返還給數(shù)據(jù)庫連接池,假設這三個動作加起來總共用時0.001秒。那么采取遍歷的辦法查詢,將會多耗時0.001N秒,如果需要查詢1000次,那么將多1秒鐘的時間,對于程序猿來說,這是不可忍受的,因為這只是一個循環(huán)查詢,還不算其它的業(yè)務代碼。
那么,有沒有更好的辦法呢,答案是肯定,其中之一是動態(tài)SQL:
先上代碼:
1
2
3
4
5
6
|
< 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>接受.
但是動態(tài)SQL中的foreach語句用的最多的實在insert語句中,并且通常在in子句中使用。
二、高級映射
在使用mybatis的時候,一般是使用resultType = com.blog.author 實體類來接受查詢結果
或者是使用resultType = java.util.map將數(shù)據(jù)庫列名作為key,記錄值作為value返回。
但是這次需要使用resultMap,它可以允許自由組合返回值的形式,用以處理更復雜的查詢。
還是先上代碼:
SQL:
1
2
3
4
5
6
7
|
< 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配置:
1
2
3
4
5
6
7
8
|
<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實體類
1
2
3
4
5
6
|
Public class Bolg { private Integer authorID; private String authorName; private List<Post> postsList; //setter getter } |
Post實體類
1
2
3
4
5
|
Public class Post { private Integer blogID; private String blogName; //setter getter } |
這樣就可以用一個實體接受一個復雜查詢了。
下面再介紹下各個屬性的作用:
其它和普通mybatis查詢的屬性和配置就不細說了,
resultMap用來代替resultType,表示查詢結果返回的格式
resultMap中的id主要有兩個作用:
類似索引,提高查詢性能
區(qū)分不同結果
所以id最好不要省略,如果沒有主鍵,用能唯一區(qū)分記錄的字段代替
result即實體類中定義的變量名,column是數(shù)據(jù)庫的列名
collection 就是列表、map等集合
postsList就是在Blog實體類中定義的list變量名
ofType就是對象列表中對象的實體類。
三、獲得自增ID:
如果有如下情況,在插入數(shù)據(jù)庫記錄后,想得到插入記錄的主鍵,用以后面的業(yè)務代碼
那么mybatis針對這種情況也提供了相應的支持(不支持批量插入):
MySQL是原聲自增ID;假設自增主鍵的字段名就為ID
1
2
3
4
|
<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" 表示返回主鍵的名字。
那么在業(yè)務代碼中就可以用下列語句接收:
假設實體類為User
1
|
User userNew = userMapper.insert(user); |
userNew.getID //即為插入后的自增ID
其實,mysql的自增主鍵可以用select LAST_INSERT_ID();來得到,
所以,還有一種寫法:
1
2
3
4
5
6
7
|
<insert id= "insert" parameterType= "User" > <selectKey resultType= "int" order= "AFTER" keyProperty= "id" > SELECT LAST_INSERT_ID() AS id </selectKey> insert into name,age values ( #{name}, #{age} ) </insert> |
和mysql的獲取主鍵方式剛好相反,mysql是insert執(zhí)行后由表分配自增長的值,而oracle是獲取到自增長的值后再進行插入記錄操作,在執(zhí)行insert sql前必須指定一個主鍵值給要插入的記錄所以要要在"BEFORE"的時候拿到自增的序列,然后用selectKey的方式注入到入?yún)⒂成渲屑纯伞<僭O自增長還是id
1
2
3
4
5
6
7
8
|
<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,age VALUES (#{id} #{name}, #{age} ) </insert> |
這里的id就是selectKey獲得的自增id。
接收方式和mysql一樣,在獲取自增主鍵時,最好使用實體接收。
以上所述是小編給大家介紹的Mybatis高級映射、動態(tài)SQL及獲得自增主鍵,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!