Mybatis簡介
MyBatis是一個支持普通SQL查詢,存儲過程和高級映射的優(yōu)秀持久層框架。MyBatis消除了幾乎所有的JDBC代碼和參數(shù)的手工設(shè)置以及對結(jié)果集的檢索封裝。MyBatis可以使用簡單的XML或注解用于配置和原始映射,將接口和Java的POJO(Plain Old Java Objects,普通的Java對象)映射成數(shù)據(jù)庫中的記錄。
Mybatis的功能架構(gòu)分為三層(圖片借用了百度百科):
1) API接口層:提供給外部使用的接口API,開發(fā)人員通過這些本地API來操縱數(shù)據(jù)庫。接口層一接收到調(diào)用請求就會調(diào)用數(shù)據(jù)處理層來完成具體的數(shù)據(jù)處理。
2) 數(shù)據(jù)處理層:負責具體的SQL查找、SQL解析、SQL執(zhí)行和執(zhí)行結(jié)果映射處理等。它主要的目的是根據(jù)調(diào)用的請求完成一次數(shù)據(jù)庫操作。
3) 基礎(chǔ)支撐層:負責最基礎(chǔ)的功能支撐,包括連接管理、事務(wù)管理、配置加載和緩存處理,這些都是共用的東西,將他們抽取出來作為最基礎(chǔ)的組件。為上層的數(shù)據(jù)處理層提供最基礎(chǔ)的支撐。
MyBatis中在查詢進行select映射的時候,返回類型可以用resultType,也可以用resultMap,resultType是直接表示返回類型的,而resultMap則是對外部ResultMap的引用,但是resultType跟resultMap不能同時存在。在MyBatis進行查詢映射的時候,其實查詢出來的每一個屬性都是放在一個對應(yīng)的Map里面的,其中鍵是屬性名,值則是其對應(yīng)的值。當提供的返回類型屬性是resultType的時候,MyBatis會將Map里面的鍵值對取出賦給resultType所指定的對象對應(yīng)的屬性。所以其實MyBatis的每一個查詢映射的返回類型都是ResultMap,只是當我們提供的返回類型屬性是resultType的時候,MyBatis對自動的給我們把對應(yīng)的值賦給resultType所指定對象的屬性,而當我們提供的返回類型是resultMap的時候,因為Map不能很好表示領(lǐng)域模型,我們就需要自己再進一步的把它轉(zhuǎn)化為對應(yīng)的對象,這常常在復(fù)雜查詢中很有作用。
有這樣一個Blog.java文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import java.util.List; public class Blog { private int id; private String title; private String content; private String owner; private List<Comment> comments; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getTitle() { return title; } public void setTitle(String title) { this .title = title; } public String getContent() { return content; } public void setContent(String content) { this .content = content; } public String getOwner() { return owner; } public void setOwner(String owner) { this .owner = owner; } public List<Comment> getComments() { return comments; } public void setComments(List<Comment> comments) { this .comments = comments; } @Override public String toString() { return " ----------------博客-----------------\n id: " + id + "\n title: " + title + "\n content: " + content + "\n owner: " + owner; } } |
其所對應(yīng)的數(shù)據(jù)庫表中存儲有id,title,Content,Owner屬性,那么當我們進行下面這樣一個查詢映射的時候
1
2
3
4
|
<typeAlias alias= "Blog" type= "com.tiantian.mybatis.model.Blog" /><!--來自MyBatis的配置文件mybatis_config.xml--> <select id= "selectBlog" parameterType= "int" resultType= "Blog" > select * from t_blog where id = #{id} </select><!--來自SQL映射文件BlogMapper.xml--> |
MyBatis會自動創(chuàng)建一個ResultMap對象,然后基于查找出來的屬性名進行鍵值對封裝,然后再看到返回類型是Blog對象,再從ResultMap中取出與Blog對象對應(yīng)的鍵值對進行賦值。
當返回類型直接是一個ResultMap的時候也是非常有用的,這主要用在進行復(fù)雜聯(lián)合查詢上,因為進行簡單查詢是沒有什么必要的。我們先看看一個返回類型為ResultMap的簡單查詢,再看看復(fù)雜查詢的用法。
簡單查詢的寫法
1
2
3
4
5
6
7
8
9
|
<resultMap type= "Blog" id= "BlogResult" > <id column= "id" property= "id" /> <result column= "title" property= "title" /> <result column= "content" property= "content" /> <result column= "owner" property= "owner" /> </resultMap> <select id= "selectBlog" parameterType= "int" resultMap= "BlogResult" > select * from t_blog where id = #{id} </select> |
select映射中resultMap的值是一個外部resultMap的id,表示返回結(jié)果映射到哪一個resultMap上,外部resultMap的type屬性表示該resultMap的結(jié)果是一個什么樣的類型,這里是Blog類型,那么MyBatis就會把它當作一個Blog對象取出。resultMap節(jié)點的子節(jié)點id是用于標識該對象的id的,而result子節(jié)點則是用于標識一些簡單屬性的,其中的Column屬性表示從數(shù)據(jù)庫中查詢的屬性,Property則表示查詢出來的屬性對應(yīng)的值賦給實體對象的哪個屬性。簡單查詢的resultMap的寫法就是這樣的。接下來看一個復(fù)雜一點的查詢。
有一個Comment類,其中有一個Blog的引用,表示是對哪個Blog的Comment,那么我們在查詢Comment的時候把其對應(yīng)的Blog也要查出來賦給其blog屬性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import java.util.Date; public class Comment { private int id; private String content; private Date commentDate = new Date(); private Blog blog; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getContent() { return content; } public void setContent(String content) { this .content = content; } public Date getCommentDate() { return commentDate; } public void setCommentDate(Date commentDate) { this .commentDate = commentDate; } public Blog getBlog() { return blog; } public void setBlog(Blog blog) { this .blog = blog; } public String toString() { return blog + "\n ----------------評論-----------------\n id: " + id + "\n content: " + content + "\n commentDate: " + commentDate; } } |
其寫法是這樣的
1
2
3
4
5
6
7
8
9
10
|
<!--來自CommentMapper.xml文件 --> <resultMap type= "Comment" id= "CommentResult" > <association property= "blog" select= "selectBlog" column= "blog" javaType= "Blog" /> </resultMap> <select id= "selectComment" parameterType= "int" resultMap= "CommentResult" > select * from t_Comment where id = #{id} </select> <select id= "selectBlog" parameterType= "int" resultType= "Blog" > select * from t_Blog where id = #{id} </select> |
其訪問情況是這樣的,先是請求id為selectComment的select映射,然后得到一個id為CommentResult的ResultMap對象,我們可以看到在對應(yīng)的resultMap的返回類型是一個Comment對象,其中只有一個association節(jié)點,而沒有像前面說的簡單查詢所對應(yīng)的id,result子節(jié)點,但是其仍會把對應(yīng)的id等屬性賦給Comment對象,這就是前面所說的MyBatis擁有自動封裝功能,只要你提供了返回類型,MyBatis會根據(jù)自己的判斷來利用查詢結(jié)果封裝對應(yīng)的對象,所以前面的簡單查詢中,如果你不在resultMap中明確的指出id對應(yīng)哪個字段,title對應(yīng)哪個字段,MyBatis也會根據(jù)自身的判斷來幫你封裝,MyBatis的自身判斷是把查詢的field或其對應(yīng)的別名與返回對象的屬性進行比較,如果相匹配且類型也相匹配,MyBatis則會對其進行賦值。在上面對應(yīng)的resultMap中關(guān)聯(lián)了一個blog屬性,其對應(yīng)的JAVA類型為Blog,在上述的寫法中,關(guān)聯(lián)對象是通過子查詢來進行關(guān)聯(lián)的,當然也可以直接通過關(guān)聯(lián)查詢來進行關(guān)聯(lián)。上面的association子節(jié)點中,Property屬性表示是resultMap返回類型的哪個關(guān)聯(lián)屬性,對于上面的例子就是Comment管理的blog屬性;select表示進行哪個select映射來映射對應(yīng)的關(guān)聯(lián)屬性,即會去請求id為select所對應(yīng)的值的select映射 來查詢出其所關(guān)聯(lián)的屬性對象;Column表示當前關(guān)聯(lián)對象在id為CommentResult的resultMap中所對應(yīng)的鍵值對,該鍵值對將作為對關(guān)聯(lián)對象子查詢的參數(shù),即將把在selectComment中查詢出來的blog屬性的值作為參數(shù)傳給進行關(guān)聯(lián)對象blog的子查詢selectBlog的參數(shù);javaType表示當前關(guān)聯(lián)對象在JAVA中是什么類型。
上述介紹的是一對一或一對多的情況下,對一的一方的關(guān)聯(lián)的查詢。在實際應(yīng)用中還有一個用的比較多的應(yīng)用是通過一的一方查出對應(yīng)的多的一方,在拿出多的一方的時候也同樣要把一的一方關(guān)聯(lián)上,即在上述例子中,在拿出Blog對象的時候,就把其對應(yīng)的Comment全部拿出來,在拿出Comment的時候也還是需要把其對應(yīng)的Blog拿出來,這是在JAVA中通過一次請求就拿出來的。寫法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- 來自BlogMapper.xml文件 --> <resultMap type= "Blog" id= "BlogResult" > <id column= "id" property= "id" /> <collection property= "comments" select= "selectCommentsByBlog" column= "id" ofType= "Comment" ></collection> </resultMap> <resultMap type= "Comment" id= "CommentResult" > <association property= "blog" javaType= "Blog" column= "blog" select= "selectBlog" /> </resultMap> <select id= "selectBlog" parameterType= "int" resultMap= "BlogResult" > select * from t_blog where id = #{id} </select> <!-- 通過Blog來查找Comment --> <select id= "selectCommentsByBlog" parameterType= "int" resultMap= "CommentResult" > select * from t_Comment where blog = #{blogId} </select> |
上述請求的入口是id為selectBlog的select映射,返回結(jié)果為id為BlogResult的resultMap,id為BlogResult的類型為Blog,其中指定了id的屬性和字段,指定id將對MyBatis內(nèi)部的構(gòu)造作用非常大。其中關(guān)聯(lián)了一個comments對象,因為一個Blog可以有很多Comment,該comments為一個集合,所以用集合collection進行映射,其中的select還是表示進行哪個子查詢來查詢對應(yīng)的comments,column表示把上述查出來的哪個字段值當作參數(shù)傳給子查詢,ofType也是表示返回類型,這里的返回類型是集合內(nèi)部的類型,之所以用ofType而不是用type是MyBatis內(nèi)部為了和關(guān)聯(lián)association進行區(qū)別。
測試代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@Test public void selectCommentsByBlogTest() { SqlSession session = Util.getSqlSessionFactory().openSession(); CommentMapper commentMapper = session.getMapper(CommentMapper. class ); List<Comment> comments = commentMapper.selectCommentsByBlog( 6 ); for (Comment comment : comments) System.out.println(comment); session.close(); } /** * 查詢單條記錄 */ @Test public void testSelectOne() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper. class ); Blog blog = blogMapper.selectBlog( 6 ); List<Comment> comments = blog.getComments(); if (comments != null ) { System.out.println( "--------------Comments Size------------" + comments.size()); for (Comment comment : comments) System.out.println(comment); } session.close(); } |
以上所述是小編給大家介紹的MyBatis中的resultMap簡要概述,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!