Mybatis 之 resultMap简介

2024-09-03 01:18
文章标签 简介 mybatis resultmap

本文主要是介绍Mybatis 之 resultMap简介,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文转自:http://haohaoxuexi.iteye.com/blog/1337009

MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。在MyBatis进行查询映射的时候,其实每次查询的结果都是放在一个对应的Map里面的,其中键是数据库字段名,值则是其对应的值。当提供的返回类型属性是resultType的时候,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性.

有这样一个Blog.java文件

Java代码

 

[java]  view plain copy
  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代码

 

[xml]  view plain copy
  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代码
[xml]  view plain copy
  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>  

resultMap的id用于标识该元素,一般被select映射中resultMap属性来引用;resultMap的type属性表示该resultMap的结果是一个什么样的类型(这里是Blog类型,那么MyBatis就会把它当作一个Blog对象取出)resultMap节点的子节点id是用于标识该对象对应数据库中表的id(表的主键),而result子节点则是用于表示数据库字段和对象属性的对应关系,其中的Column属性是表的对应字段名,Property实体对象的哪个属性。简单查询的resultMap的写法就是这样的。接下来看一个复杂一点的查询。有一个Comment类,其中有一个Blog的引用,表示是对哪个Blog的Comment,那么我们在查询Comment的时候把其对应的Blog也要查出来赋给其blog属性。

Java代码

 

[java]  view plain copy
  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代码
[xml]  view plain copy
  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>  

上面的association子节点中,property属性表示是resultMap所返回类型关联属性的名称(类Comment 的blog属性);select属性表示通过哪个select映射来填充对应的属性(此处,会通过Id为selectBlog的查询, 填充属性对象);Column 是对应的关联外键(此处为Comment表关联blog表的外键:blog);javaType表示当前关联对象在JAVA中是什么类型。在上述的写法中,关联对象是通过子查询来进行关联的,当然也可以直接通过关联查询来进行关联.我们可以看到在对应的resultMap的返回类型是一个Comment对象,其中只有一个association节点,而没有像前面说的简单查询所对应的id,result子节点,但是其仍会把对应的id等属性赋给Comment对象,这就是前面所说的MyBatis拥有自动封装功能,只要你提供了返回类型,MyBatis会根据自己的判断来利用查询结果封装对应的对象,同理上一个查询例子中,即使不在resultMap中明确的指出id对应哪个字段,title对应哪个字段,MyBatis也会根据自身的判断来帮你封。MyBatis的自身判断是把查询的field或其对应的别名与返回对象的属性进行比较,如果相匹配且类型也相匹配,MyBatis则会对其进行赋值。其访问情况是这样的,先是请求id为selectComment的select映射,然后得到一个id为CommentResult的ResultMap对象。

 

上述介绍的是一对一或一对多的中对一的一方的关联的查询。在实际应用中还有一个用的比较多的应用是通过“一”查出对应的“多”,同时在拿出“多”的时候也同样要把“一”关联上,即在上述例子中,在拿出Blog对象的时候,就把其对应的Comment全部拿出来,在拿出Comment的时候也还是需要把其对应的Blog拿出来,这是在JAVA中通过一次请求就拿出来的。写法如下:

Xml代码

[xml]  view plain copy
  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代码
[java]  view plain copy
  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();  
  26. }  

 

这篇关于Mybatis 之 resultMap简介的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1131609

相关文章

Qt QCustomPlot库简介(最新推荐)

《QtQCustomPlot库简介(最新推荐)》QCustomPlot是一款基于Qt的高性能C++绘图库,专为二维数据可视化设计,它具有轻量级、实时处理百万级数据和多图层支持等特点,适用于科学计算、... 目录核心特性概览核心组件解析1.绘图核心 (QCustomPlot类)2.数据容器 (QCPDataC

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI

MyBatis编写嵌套子查询的动态SQL实践详解

《MyBatis编写嵌套子查询的动态SQL实践详解》在Java生态中,MyBatis作为一款优秀的ORM框架,广泛应用于数据库操作,本文将深入探讨如何在MyBatis中编写嵌套子查询的动态SQL,并结... 目录一、Myhttp://www.chinasem.cnBATis动态SQL的核心优势1. 灵活性与可

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

Mybatis Plus JSqlParser解析sql语句及JSqlParser安装步骤

《MybatisPlusJSqlParser解析sql语句及JSqlParser安装步骤》JSqlParser是一个用于解析SQL语句的Java库,它可以将SQL语句解析为一个Java对象树,允许... 目录【一】jsqlParser 是什么【二】JSqlParser 的安装步骤【三】使用场景【1】sql语

mybatis的mapper对应的xml写法及配置详解

《mybatis的mapper对应的xml写法及配置详解》这篇文章给大家介绍mybatis的mapper对应的xml写法及配置详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录前置mapper 对应 XML 基础配置mapper 对应 xml 复杂配置Mapper 中的相