首页 > 其他分享 >MyBatis之ResultMap简介,关联对象

MyBatis之ResultMap简介,关联对象

时间:2023-02-09 13:32:34浏览次数:36  
标签:blog Blog 简介 ResultMap public content MyBatis 查询 id


MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。在MyBatis进行查询映射的时候,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。当提供的返回类型属性是resultType的时候,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性,而当我们提供的返回类型是resultMap的时候,因为Map不能很好表示领域模型,我们就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用。

有这样一个Blog.java文件



1. import
2.
3. public class
4.
5. private int
6.
7. private
8.
9. private
10.
11. private
12.
13. private
14.
15. public int
16. return
17. }
18.
19. public void setId(int
20. this.id = id;
21. }
22.
23. public
24. return
25. }
26.
27. public void
28. this.title = title;
29. }
30.
31. public
32. return
33. }
34.
35. public void
36. this.content = content;
37. }
38.
39. public
40. return
41. }
42.
43. public void
44. this.owner = owner;
45. }
46.
47. public
48. return
49. }
50.
51. public void
52. this.comments = comments;
53. }
54.
55. @Override
56. public
57. return " ----------------博客-----------------\n id: " + id + "\n title: " + title + "\n content: "
58. + "\n owner: "
59. }
60.
61. }

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;
}

}

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



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
4. </select><!--来自SQL映射文件BlogMapper.xml-->




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

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

简单查询的写法



    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
    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属性。




      1. import
      2.
      3. public class
      4.
      5. private int
      6.
      7. private
      8.
      9. private Date commentDate = new
      10.
      11. private
      12.
      13. public int
      14. return
      15. }
      16.
      17. public void setId(int
      18. this.id = id;
      19. }
      20.
      21. public
      22. return
      23. }
      24.
      25. public void
      26. this.content = content;
      27. }
      28.
      29. public
      30. return
      31. }
      32.
      33. public void
      34. this.commentDate = commentDate;
      35. }
      36.
      37. public
      38. return
      39. }
      40.
      41. public void
      42. this.blog = blog;
      43. }
      44.
      45. public
      46. return blog + "\n ----------------评论-----------------\n id: " + id + "\n content: " + content + "\n commentDate: "
      47. }
      48.
      49. }


      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. <!--来自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
      8. </select>
      9.
      10. <select id="selectBlog" parameterType="int" resultType="Blog">
      11. select * from t_Blog where 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中通过一次请求就拿出来的。写法如下:



      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
      13. </select>
      14.
      15. <!-- 通过Blog来查找Comment -->
      16. <select id="selectCommentsByBlog" parameterType="int" resultMap="CommentResult">
      17. select * from t_Comment where blog
      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进行区别。

      测试代码:



      1. @Test
      2. public void
      3. SqlSession session = Util.getSqlSessionFactory().openSession();
      4. CommentMapper commentMapper = session.getMapper(CommentMapper.class);
      5. List<Comment> comments = commentMapper.selectCommentsByBlog(6);
      6. for
      7. System.out.println(comment);
      8. session.close();
      9. }
      10.
      11. /**
      12. * 查询单条记录
      13. */
      14. @Test
      15. public void
      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------------"
      22. for
      23. System.out.println(comment);
      24. }
      25. session.close();
      26. }

      标签:blog,Blog,简介,ResultMap,public,content,MyBatis,查询,id
      From: https://blog.51cto.com/u_4172728/6046886

      相关文章

      • 基于MyBatis3.0.6的基本操作介绍
        每一个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心。SqlSessionFactory本身是由SqlSessionFactoryBuilder创建的,一般而言,在一个应......
      • Mybatis类型转换介绍
        1.2建立TypeHandler我们知道java有java的数据类型,数据库有数据库的数据类型,那么我们在往数据库中插入数据的时候是如何把java类型当做数据库类型插入数据库,在从数据库读取......
      • 浅谈mybatis返回单一对象或对象列表的问题
        目录mybatis返回单一对象或对象列表一、说明二、代码测试UserMap.xml映射文件dao文件UserMap.java测试代码和结果文件mybatis返回的对象包含集合 mybatis......
      • FCoE简介
        目录FCoE使用前提FCoEFCoE是一种融合网络技术,其目的是将FC帧封装到以太网帧中,实现以太网链路与光纤链路通信的功能。SAN一般指存储区域网络,FCSAN有光纤组网,IPSAN由以......
      • Mybatis 复杂对象resultMap的使用
        目录mybatis复杂对象resultMap下面是resultMap的定义普通属性省略说明select相关配置Model代码resultMap处理复杂映射问题Ⅰ多对一查询:学生——老师(1)创......
      • 解决resultMap映射数据错误的问题
        目录resultMap映射数据错误解决方案【报错】resultMap认知错误附图(修改过后的) resultMap映射数据错误mapper文件使用了resultMap进行一对多关系映射,不管怎么......
      • Mybatis数据库批量操作
         1:新增首先,Mysql插入一条记录返回主键对Mybatis版本要求低,而批量插入返回带主键的,需要升级到3.3.1以及以上的版本。 ​1.1:Mysql上图需要注意加入useGenerate......
      • Mybatis-Plus 之BaseMapper 方法详解
        packagecom.itheima.dao;importcom.baomidou.mybatisplus.core.conditions.Wrapper;importcom.baomidou.mybatisplus.core.metadata.IPage;importcom.baomidou.my......
      • 三种权限模型简介
        三种权限模型简介1.RABC模型该模型是基于角色的权限管理模型(RBAC,RoleBasedAccessControl)角色的概念就是对用户的一个升级,管理者不需要考虑用户和权限关系。......
      • mybatis使用resultMap获取不到值的解决方案
        目录mybatisresultMap获取不到值问题描述原因及解决方法Mybatis从数据库中获取值为nullResultMap要解决的问题:属性名和字段名不一致解决方法 mybatisre......