当前位置: 移动技术网 > IT编程>开发语言>Java > 深入理解Mybatis中的resultType和resultMap

深入理解Mybatis中的resultType和resultMap

2019年07月22日  | 移动技术网IT编程  | 我要评论

 一、概述

mybatis中在查询进行select映射的时候,返回类型可以用resulttype,也可以用resultmap,resulttype是直接表示返回类型的,而resultmap则是对外部resultmap的引用,但是resulttype跟resultmap不能同时存在。

在mybatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的map里面的,其中键是属性名,值则是其对应的值。

①当提供的返回类型属性是resulttype时,mybatis会将map里面的键值对取出赋给resulttype所指定的对象对应的属性。所以其实mybatis的每一个查询映射的返回类型都是resultmap,只是
当提供的返回类型属性是resulttype的时候,mybatis对自动的给把对应的值赋给resulttype所指定对象的属性。

②当提供的返回类型是resultmap时,因为map不能很好表示领域模型,就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用。

二、resulttype

blog.java
public class blog {
private int id;
private string title;
private string content;
private string owner;
private list<comment> comments;
}

其所对应的数据库表中存储有id、title、content、owner属性。

<typealias alias="blog" type="com.tiantian.mybatis.model.blog"/>
<select id="selectblog" parametertype="int" resulttype="blog">
select * from t_blog where id = #{id}
</select>

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

三、resultmap

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

①简单查询的写法

<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,表示返回结果映射到哪一个resultmap上,外部resultmap的type属性表示该resultmap的结果是一个什么样的类型,这里是blog类型,那么mybatis就会把它当作一个blog对象取出。resultmap节点的子节点id是用于标识该对象的id的,而result子节点则是用于标识一些简单属性的,其中的column属性表示从数据库中查询的属性,property则表示查询出来的属性对应的值赋给实体对象的哪个属性。简单查询的resultmap的写法就是这样的。

②复杂查询

有一个comment类,其中有一个blog的引用,表示是对哪个blog的comment,那么在查询comment的时候把其对应的blog也要查出来赋给其blog属性。

public class comment {
private int id;
private string content;
private date commentdate = new date();
private blog blog;
}
<!--来自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对象,可以看到在对应的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中通过一次请求就拿出来的。

<!-- 来自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>
<select id="selectcommentsbyblog" parametertype="int" resultmap="commentresult">
select * from t_comment where blog = #{blogid}
</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进行区别。

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();
}
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) {
for (comment comment : comments)
system.out.println(comment);
}
session.close();
}

以上所述是小编给大家介绍的mybatis中的resulttype和resultmap,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网