详细分析mybatis中resultType和resultMap的区别与联系

在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解

比如,我们平时使用的单表查询,很多时候使用的就是resultType

下来,看一段代码吧

 1 package org.cxxy.base.cxsc.entity; 2  3 public class TbClass { 4     private Integer id; 5  6     private String classname; 7  8     private String deptname; 9 10     public Integer getId() {11         return id;12     }13 14     public void setId(Integer id) {15         this.id = id;16     }17 18     public String getClassname() {19         return classname;20     }21 22     public void setClassname(String classname) {23         this.classname = classname == null ? null : classname.trim();24     }25 26     public String getDeptname() {27         return deptname;28     }29 30     public void setDeptname(String deptname) {31         this.deptname = deptname == null ? null : deptname.trim();32     }33 }

上面的PO类我使用的是我的一个小Demo

下来开始贴我的XML Mapper

<resultMap id="BaseResultMap" type="org.cxxy.base.cxsc.entity.TbClass"><id column="id" jdbcType="INTEGER" property="id" /><result column="classname" jdbcType="VARCHAR" property="classname" /><result column="deptname" jdbcType="VARCHAR" property="deptname" /></resultMap>

这个resultMap是对应的我的po类的属性

下来,贴出我的xml的单表查询statement

<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select  id, classname, deptnamefrom tb_classwhere id = #{id,jdbcType=INTEGER}</select>
 

parameterType代表的是输入参数(比如:select * from tb_class where id = "xxxx"),resultMap表示映射的结果集,从命名中也可以看到Map,当然是结果集了,

上述代码所代表的单表查询(一对一),当然,在一般开发的时候,像这种映射,我们一般会使用下述的写法

<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="类的全限定名">select  id, classname, deptnamefrom tb_classwhere id = #{id,jdbcType=INTEGER}</select>

即是说所得到的结果一样,一般在我们理解方面,尽量还是选择后者

但是如果根据客户的需求的变化,比如说写出了类的扩展类

org.cxxy.base.cxsc.entity.TbClassDatail

如果,在扩展类中引入了外类(其他的表的属性(和本类没有共同的属性)),我们可以使用resultMap,但是也并非完全

resultMap

定义po类在Orders类中加入User属性。在Orders类中加入List<Orderdetail> orderdetails属性

 订单查询清单

<select id="findOrdersDetailList" resultMap="userorderdetailmap">SELECT    orders.*,    user.username,    user.address,    orderdetail.id orderdetail_id,    orderdetail.items_id,    orderdetail.items_num    FROM orders,user,orderdetail    WHERE orders.user_id = user.id    AND orders.id = orderdetail.orders_id</select> 

 <!– 订单信息resultmap –>

<!-- 订单信息resultmap --><resultMap type="cn.itcast.mybatis.po.Orders" id="userorderdetailmap"><id property="id"column="id"/><result property="user_id" column="user_id"/><result property="number" column="number"/><association property="user" javaType="cn.itcast.mybatis.po.User"><id property="id" column="user_id"/><result property="username" column="username"/><result property="address" column="address"/></association><collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail"><id property="id" column="orderdetail_id"/><result property="items_id" column="items_id"/><result property="items_num" column="items_num"/></collection></resultMap>

上面的代码,我是贴的某培训机构的订单查询代码,

  上面的实体类的关系是:Order—–>User 一对一(一个用户一个订单) Order——->OrderDetail 一对多(一个订单有多条订单明细)

像这种的一对多、多对多查询的映射,我们尽量使用resultMap

注:collection 标签是一对多的映射,常用于一对多中扩展类下的List<po对象>的属性   association标签适用扩展类包含的一对一的po类对象属性

MyBatis中关于resultType和resultMap的区别

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

总结一下

resultType:

作用:

将查询结果按照sql列名pojo属性名一致性映射到pojo中(适用于单表仅查询)。

场合:

常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用resultType将每一条记录映射到pojo中,在前端页面遍历list(list中是pojo)即可。

以上就是详细分析mybatis中resultType和resultMap的区别与联系的详细内容,更多请关注其它相关文章!

每一个成功者都有一个开始。勇于开始,才能找到成功的路。

详细分析mybatis中resultType和resultMap的区别与联系

相关文章:

你感兴趣的文章:

标签云: