---
title: "Mybatis之ResultMap简介，关联对象 "
author: "爱上不吃鱼DE猫"
date: "2014-11-22T13:19:25Z"
tags: []
categories: ["编程语言"]
slug: "mybatis-resultmap"
draft: false
lastmod: "2014-11-22T13:19:25Z"
id: 2159148
url: "https://yaolunwei.iteye.com/blog/2159148"
---

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代码

 

 [![收藏代码](http://haohaoxuexi.iteye.com/images/icon_star.png)]()

01. import java.util.List;
02. public class Blog {
03. private int id;
04. private String title;
05. private String content;
06. private String owner;
07. private List&lt;Comment&gt; comments;
08. public int getId() {
09. return id;
10. }
11. public void setId(int id) {
12. this.id = id;
13. }
14. public String getTitle() {
15. return title;
16. }
17. public void setTitle(String title) {
18. this.title = title;
19. }
20. public String getContent() {
21. return content;
22. }
23. public void setContent(String content) {
24. this.content = content;
25. }
26. public String getOwner() {
27. return owner;
28. }
29. public void setOwner(String owner) {
30. this.owner = owner;
31. }
32. public List&lt;Comment&gt; getComments() {
33. return comments;
34. }
35. public void setComments(List&lt;Comment&gt; comments) {
36. this.comments = comments;
37. }
38. @Override
39. public String toString() {
40. return " ----------------博客-----------------\\n id: " + id + "\\n title: " + title + "\\n content: " + content
41. + "\\n owner: " + owner;
42. }
43. }

 

其所对应的数据库表中存储有id，title，Content，Owner属性，那么当我们进行下面这样一个查询映射的时候

 

Xml代码

 

 [![收藏代码](http://haohaoxuexi.iteye.com/images/icon_star.png)]()

1. &lt;typeAlias alias="Blog" type="com.tiantian.mybatis.model.Blog"/&gt;&lt;!--来自MyBatis的配置文件mybatis\_config.xml--&gt;
2. &lt;select id="selectBlog" parameterType="int" resultType="Blog"&gt;
3. select * from t\_blog where id = #{id}
4. &lt;/select&gt;&lt;!--来自SQL映射文件BlogMapper.xml--&gt;

 

 MyBatis会自动创建一个ResultMap对象，然后基于查找出来的属性名进行键值对封装，然后再看到返回类型是Blog对象，再从ResultMap中取出与Blog对象对应的键值对进行赋值。

当返回类型直接是一个ResultMap的时候也是非常有用的，这主要用在进行复杂联合查询上，因为进行简单查询是没有什么必要的。我们先看看一个返回类型为ResultMap的简单查询，再看看复杂查询的用法。

简单查询的写法

 

Xml代码

 

 [![收藏代码](http://haohaoxuexi.iteye.com/images/icon_star.png)]()

1. &lt;resultMap type="Blog" id="BlogResult"&gt;
2. &lt;id column="id" property="id"/&gt;
3. &lt;result column="title" property="title"/&gt;
4. &lt;result column="content" property="content"/&gt;
5. &lt;result column="owner" property="owner"/&gt;
6. &lt;/resultMap&gt;
7. &lt;select id="selectBlog" parameterType="int" resultMap="BlogResult"&gt;
8. select * from t\_blog where id = #{id}
9. &lt;/select&gt;

 

 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代码

 

 [![收藏代码](http://haohaoxuexi.iteye.com/images/icon_star.png)]()

01. import java.util.Date;
02. public class Comment {
03. private int id;
04. private String content;
05. private Date commentDate = new Date();
06. private Blog blog;
07. public int getId() {
08. return id;
09. }
10. public void setId(int id) {
11. this.id = id;
12. }
13. public String getContent() {
14. return content;
15. }
16. public void setContent(String content) {
17. this.content = content;
18. }
19. public Date getCommentDate() {
20. return commentDate;
21. }
22. public void setCommentDate(Date commentDate) {
23. this.commentDate = commentDate;
24. }
25. public Blog getBlog() {
26. return blog;
27. }
28. public void setBlog(Blog blog) {
29. this.blog = blog;
30. }
31. public String toString() {
32. return blog + "\\n ----------------评论-----------------\\n id: " + id + "\\n content: " + content + "\\n commentDate: " + commentDate;
33. }
34. }

 

 

其写法是这样的

 

Xml代码

 

 [![收藏代码](http://haohaoxuexi.iteye.com/images/icon_star.png)]()

01. &lt;!--来自CommentMapper.xml文件    --&gt;
02. &lt;resultMap type="Comment" id="CommentResult"&gt;
03. &lt;association property="blog" select="selectBlog" column="blog" javaType="Blog"/&gt;
04. &lt;/resultMap&gt;
05. &lt;select id="selectComment" parameterType="int" resultMap="CommentResult"&gt;
06. select * from t\_Comment where id = #{id}
07. &lt;/select&gt;
08. &lt;select id="selectBlog" parameterType="int" resultType="Blog"&gt;
09. select * from t\_Blog where id = #{id}
10. &lt;/select&gt;

其访问情况是这样的，先是请求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代码

 

 [![收藏代码](http://haohaoxuexi.iteye.com/images/icon_star.png)]()

01. &lt;!-- 来自BlogMapper.xml文件 --&gt;
02. &lt;resultMap type="Blog" id="BlogResult"&gt;
03. &lt;id column="id" property="id"/&gt;
04. &lt;collection property="comments" select="selectCommentsByBlog" column="id" ofType="Comment"&gt;&lt;/collection&gt;
05. &lt;/resultMap&gt;
06. &lt;resultMap type="Comment" id="CommentResult"&gt;
07. &lt;association property="blog" javaType="Blog" column="blog" select="selectBlog"/&gt;
08. &lt;/resultMap&gt;
09. &lt;select id="selectBlog" parameterType="int" resultMap="BlogResult"&gt;
10. select * from t\_blog where id = #{id}
11. &lt;/select&gt;
12. &lt;!--  通过Blog来查找Comment   --&gt;
13. &lt;select id="selectCommentsByBlog" parameterType="int" resultMap="CommentResult"&gt;
14. select * from t\_Comment where blog = #{blogId}
15. &lt;/select&gt;

上述请求的入口是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代码

 

 [![收藏代码](http://haohaoxuexi.iteye.com/images/icon_star.png)]()

01. @Test
02. public void selectCommentsByBlogTest() {
03. SqlSession session = Util.getSqlSessionFactory().openSession();
04. CommentMapper commentMapper = session.getMapper(CommentMapper.class);
05. List&lt;Comment&gt; comments = commentMapper.selectCommentsByBlog(6);
06. for (Comment comment : comments)
07. System.out.println(comment);
08. session.close();
09. }
10. /\**
11. * 查询单条记录
12. \*/
13. @Test
14. public void testSelectOne() {
15. SqlSession session = Util.getSqlSessionFactory().openSession();
16. BlogMapper blogMapper = session.getMapper(BlogMapper.class);
17. Blog blog = blogMapper.selectBlog(6);
18. List&lt;Comment&gt; comments = blog.getComments();
19. if (comments != null) {
20. System.out.println("--------------Comments Size------------" + comments.size());
21. for (Comment comment : comments)
22. System.out.println(comment);
23. }
24. session.close();
25. }

 

转载地址：http://blog.csdn.net/elim168/article/details/40651171