MyBatis之ResultMap簡介

MyBatis中在查詢進行select映射的時候,返回類型可以用resultType,也可以用resultMap,resultType是直接表示返回類型的,而resultMap則是對外部ResultMap的引用,但是resultType跟resultMap不能同時存在。在MyBatis進行查詢映射的時候,其實查詢出來的每一個屬性都是放在一個對應的Map裏面的,其中鍵是屬性名,值則是其對應的值。當提供的返回類型屬性是resultType的時候,MyBatis會將Map裏面的鍵值對取出賦給resultType所指定的對象對應的屬性。所以其實MyBatis的每一個查詢映射的返回類型都是ResultMap,只是當我們提供的返回類型屬性是resultType的時候,MyBatis對自動的給我們把對應的值賦給resultType所指定對象的屬性,而當我們提供的返回類型是resultMap的時候,因爲Map不能很好表示領域模型,我們就需要自己再進一步的把它轉化爲對應的對象,這常常在複雜查詢中很有作用。

 

有這樣一個Blog.java文件

 

Java代碼  收藏代碼
  1. import java.util.List;  
  2.   
  3. public class Blog {  
  4.   
  5.     private int id;  
  6.   
  7.     private String title;  
  8.   
  9.     private String content;  
  10.   
  11.     private String owner;  
  12.       
  13.     private List<Comment> comments;  
  14.   
  15.     public int getId() {  
  16.         return id;  
  17.     }  
  18.   
  19.     public void setId(int id) {  
  20.         this.id = id;  
  21.     }  
  22.   
  23.     public String getTitle() {  
  24.         return title;  
  25.     }  
  26.   
  27.     public void setTitle(String title) {  
  28.         this.title = title;  
  29.     }  
  30.   
  31.     public String getContent() {  
  32.         return content;  
  33.     }  
  34.   
  35.     public void setContent(String content) {  
  36.         this.content = content;  
  37.     }  
  38.   
  39.     public String getOwner() {  
  40.         return owner;  
  41.     }  
  42.   
  43.     public void setOwner(String owner) {  
  44.         this.owner = owner;  
  45.     }  
  46.       
  47.     public List<Comment> getComments() {  
  48.         return comments;  
  49.     }  
  50.       
  51.     public void setComments(List<Comment> comments) {  
  52.         this.comments = comments;  
  53.     }  
  54.   
  55.     @Override  
  56.     public String toString() {  
  57.         return " ----------------博客-----------------\n id: " + id + "\n title: " + title + "\n content: " + content  
  58.                 + "\n owner: " + owner;  
  59.     }  
  60.   
  61. }  

 

其所對應的數據庫表中存儲有id,title,Content,Owner屬性,那麼當我們進行下面這樣一個查詢映射的時候

 

Xml代碼  收藏代碼
  1. <typeAlias alias="Blog" type="com.tiantian.mybatis.model.Blog"/><!--來自MyBatis的配置文件mybatis_config.xml-->  
  2. <select id="selectBlog" parameterType="int" resultType="Blog">  
  3.         select * from t_blog where id = #{id}  
  4. </select><!--來自SQL映射文件BlogMapper.xml-->  

 

 MyBatis會自動創建一個ResultMap對象,然後基於查找出來的屬性名進行鍵值對封裝,然後再看到返回類型是Blog對象,再從ResultMap中取出與Blog對象對應的鍵值對進行賦值。

當返回類型直接是一個ResultMap的時候也是非常有用的,這主要用在進行復雜聯合查詢上,因爲進行簡單查詢是沒有什麼必要的。我們先看看一個返回類型爲ResultMap的簡單查詢,再看看複雜查詢的用法。

簡單查詢的寫法

 

Xml代碼  收藏代碼
  1. <resultMap type="Blog" id="BlogResult">  
  2.         <id column="id" property="id"/>  
  3.         <result column="title" property="title"/>  
  4.         <result column="content" property="content"/>  
  5.         <result column="owner" property="owner"/>  
  6.     </resultMap>  
  7.     <select id="selectBlog" parameterType="int" resultMap="BlogResult">  
  8.         select * from t_blog where id = #{id}  
  9.     </select>  

 

 select映射中resultMap的值是一個外部resultMap的id,表示返回結果映射到哪一個resultMap上,外部resultMap的type屬性表示該resultMap的結果是一個什麼樣的類型,這裏是Blog類型,那麼MyBatis就會把它當作一個Blog對象取出。resultMap節點的子節點id是用於標識該對象的id的,而result子節點則是用於標識一些簡單屬性的,其中的Column屬性表示從數據庫中查詢的屬性,Property則表示查詢出來的屬性對應的值賦給實體對象的哪個屬性。簡單查詢的resultMap的寫法就是這樣的。接下來看一個複雜一點的查詢。

有一個Comment類,其中有一個Blog的引用,表示是對哪個Blog的Comment,那麼我們在查詢Comment的時候把其對應的Blog也要查出來賦給其blog屬性。

 

Java代碼  收藏代碼
  1. import java.util.Date;  
  2.   
  3. public class Comment {  
  4.   
  5.     private int id;  
  6.       
  7.     private String content;  
  8.       
  9.     private Date commentDate = new Date();  
  10.       
  11.     private Blog blog;  
  12.   
  13.     public int getId() {  
  14.         return id;  
  15.     }  
  16.   
  17.     public void setId(int id) {  
  18.         this.id = id;  
  19.     }  
  20.   
  21.     public String getContent() {  
  22.         return content;  
  23.     }  
  24.   
  25.     public void setContent(String content) {  
  26.         this.content = content;  
  27.     }  
  28.   
  29.     public Date getCommentDate() {  
  30.         return commentDate;  
  31.     }  
  32.   
  33.     public void setCommentDate(Date commentDate) {  
  34.         this.commentDate = commentDate;  
  35.     }  
  36.   
  37.     public Blog getBlog() {  
  38.         return blog;  
  39.     }  
  40.   
  41.     public void setBlog(Blog blog) {  
  42.         this.blog = blog;  
  43.     }  
  44.       
  45.     public String toString() {  
  46.         return blog + "\n ----------------評論-----------------\n id: " + id + "\n content: " + content + "\n commentDate: " + commentDate;  
  47.     }  
  48.       
  49. }  
 

 

其寫法是這樣的

 

Xml代碼  收藏代碼
  1. <!--來自CommentMapper.xml文件    -->  
  2.     <resultMap type="Comment" id="CommentResult">  
  3.         <association property="blog" select="selectBlog" column="blog" javaType="Blog"/>  
  4.     </resultMap>  
  5.       
  6.     <select id="selectComment" parameterType="int" resultMap="CommentResult">  
  7.         select * from t_Comment where id = #{id}  
  8.     </select>  
  9.       
  10.     <select id="selectBlog" parameterType="int" resultType="Blog">  
  11.         select * from t_Blog where id = #{id}  
  12.     </select>  

其訪問情況是這樣的,先是請求id爲selectComment的select映射,然後得到一個id爲CommentResult的ResultMap對象,我們可以看到在對應的resultMap的返回類型是一個Comment對象,其中只有一個association節點,而沒有像前面說的簡單查詢所對應的id,result子節點,但是其仍會把對應的id等屬性賦給Comment對象,這就是前面所說的MyBatis擁有自動封裝功能,只要你提供了返回類型,MyBatis會根據自己的判斷來利用查詢結果封裝對應的對象,所以前面的簡單查詢中,如果你不在resultMap中明確的指出id對應哪個字段,title對應哪個字段,MyBatis也會根據自身的判斷來幫你封裝,MyBatis的自身判斷是把查詢的field或其對應的別名與返回對象的屬性進行比較,如果相匹配且類型也相匹配,MyBatis則會對其進行賦值。在上面對應的resultMap中關聯了一個blog屬性,其對應的JAVA類型爲Blog,在上述的寫法中,關聯對象是通過子查詢來進行關聯的,當然也可以直接通過關聯查詢來進行關聯。上面的association子節點中,Property屬性表示是resultMap返回類型的哪個關聯屬性,對於上面的例子就是Comment管理的blog屬性;select表示進行哪個select映射來映射對應的關聯屬性,即會去請求id爲select所對應的值的select映射 來查詢出其所關聯的屬性對象;Column表示當前關聯對象在id爲CommentResult的resultMap中所對應的鍵值對,該鍵值對將作爲對關聯對象子查詢的參數,即將把在selectComment中查詢出來的blog屬性的值作爲參數傳給進行關聯對象blog的子查詢selectBlog的參數;javaType表示當前關聯對象在JAVA中是什麼類型。

 

上述介紹的是一對一或一對多的情況下,對一的一方的關聯的查詢。在實際應用中還有一個用的比較多的應用是通過一的一方查出對應的多的一方,在拿出多的一方的時候也同樣要把一的一方關聯上,即在上述例子中,在拿出Blog對象的時候,就把其對應的Comment全部拿出來,在拿出Comment的時候也還是需要把其對應的Blog拿出來,這是在JAVA中通過一次請求就拿出來的。寫法如下:

 

Xml代碼  收藏代碼
  1. <!-- 來自BlogMapper.xml文件 -->  
  2.     <resultMap type="Blog" id="BlogResult">  
  3.         <id column="id" property="id"/>  
  4.         <collection property="comments" select="selectCommentsByBlog" column="id" ofType="Comment"></collection>  
  5.     </resultMap>  
  6.   
  7.     <resultMap type="Comment" id="CommentResult">  
  8.         <association property="blog" javaType="Blog" column="blog" select="selectBlog"/>  
  9.     </resultMap>  
  10.   
  11.     <select id="selectBlog" parameterType="int" resultMap="BlogResult">  
  12.         select * from t_blog where id = #{id}  
  13.     </select>  
  14.   
  15. <!--  通過Blog來查找Comment   -->  
  16.     <select id="selectCommentsByBlog" parameterType="int" resultMap="CommentResult">  
  17.         select * from t_Comment where blog = #{blogId}  
  18.     </select>  

上述請求的入口是id爲selectBlog的select映射,返回結果爲id爲BlogResult的resultMap,id爲BlogResult的類型爲Blog,其中指定了id的屬性和字段,指定id將對MyBatis內部的構造作用非常大。其中關聯了一個comments對象,因爲一個Blog可以有很多Comment,該comments爲一個集合,所以用集合collection進行映射,其中的select還是表示進行哪個子查詢來查詢對應的comments,column表示把上述查出來的哪個字段值當作參數傳給子查詢,ofType也是表示返回類型,這裏的返回類型是集合內部的類型,之所以用ofType而不是用type是MyBatis內部爲了和關聯association進行區別。 

 

 

 

測試代碼:

 

Java代碼  收藏代碼
  1. @Test  
  2. public void selectCommentsByBlogTest() {  
  3.     SqlSession session = Util.getSqlSessionFactory().openSession();  
  4.     CommentMapper commentMapper = session.getMapper(CommentMapper.class);  
  5.     List<Comment> comments = commentMapper.selectCommentsByBlog(6);  
  6.     for (Comment comment : comments)  
  7.         System.out.println(comment);  
  8.     session.close();  
  9. }  
  10.   
  11. /** 
  12.  * 查詢單條記錄 
  13.  */  
  14. @Test  
  15. public void testSelectOne() {  
  16.     SqlSession session = Util.getSqlSessionFactory().openSession();  
  17.     BlogMapper blogMapper = session.getMapper(BlogMapper.class);  
  18.     Blog blog = blogMapper.selectBlog(6);  
  19.     List<Comment> comments = blog.getComments();  
  20.     if (comments != null) {  
  21.         System.out.println("--------------Comments Size------------" + comments.size());  
  22.         for (Comment comment : comments)  
  23.             System.out.println(comment);  
  24.     }  
  25.     session.close();  

轉自:http://haohaoxuexi.iteye.com/blog/1337009

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章