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

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

下来,看一段代码吧

 package org.cxxy.base.cxsc.entity;

 public class TbClass {
private Integer id; private String classname; private String deptname; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getClassname() {
return classname;
} public void setClassname(String classname) {
this.classname = classname == null ? null : classname.trim();
} public String getDeptname() {
return deptname;
} public void setDeptname(String deptname) {
this.deptname = deptname == null ? null : deptname.trim();
}
}

上面的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, deptname
from tb_class
where 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, deptname
from tb_class
where 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类对象属性

总结一下

resultType:

作用:

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

场合:

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

好了,今天就分享到这里,以上仅为我自己的观点,博主现在大学生一枚,理解可能不是很充分,希望大牛能够多提提意见,谢谢。

版权声明:本文为博主原创文章,未经博主允许不得转载。

原帖地址:http://www.cnblogs.com/ChoviWu/p/7190311.html

mybatis中resultType和resultMap的联系的更多相关文章

  1. MyBatis中resultType和resultMap的区别

    resultType和resultMap功能类似  ,都是返回对象信息  ,但是resultMap要更强大一些 ,可自定义.因为resultMap要配置一下,表和类的一一对应关系,所以说就算你的字段名 ...

  2. [转]MyBatis中resultType与resultMap区别

    MyBatis中关于resultType和resultMap的具体区别如下: MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap.resu ...

  3. Mybatis中resultType和resultMap

    一.概述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部Res ...

  4. MyBatis有关resultType和resultMap差异

    MyBatis有关resultType和resultMap差异   MyBatis中在查询进行select映射的时候,返回类型能够用resultType,也能够用resultMap.resultTyp ...

  5. Mybatis中parameterType、resultMap、statementType等等配置详解(标签中基本配置详解)

    一.(转自:https://blog.csdn.net/majinggogogo/article/details/72123185) 映射文件是以<mapper>作为根节点,在根节点中支持 ...

  6. mybatis 的 resulttype 和resultMap

    resultType适合返回值比较简单的,比如一个数据类型,或者一个对象.比如对象的情况,是将表的列名和对象的属性一一对应的. 但是resultType无法处理返回值比较复杂的,特别是连接查询,需要用 ...

  7. Mybatis中resultType理解

  8. MyBatis 中 resultMap 详解

    resultMap 是 Mybatis 最强大的元素之一,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中.如在实际应用中,有一个表为(用户角色表),通过查询用户表信息展示页面, ...

  9. mybatis中查询结果为空时不同返回类型对应返回值

    今天在别人的代码基础上实现新需求,看到对于mybatis查询结果的判断不是很正确,如果查询结果为空就会异常,不知道大家有没有这样的疑惑:mybatis中resultType有多种返回类型,对于每种不同 ...

随机推荐

  1. rPithon vs. rPython(转)

    Similar to rPython, the rPithon package (http://rpithon.r-forge.r-project.org) allows users to execu ...

  2. win2012中添加架构FTP服务器

    打开IIS管理器(win+R输入inetmgr后回车或通过 添加FTP站点

  3. HTMLTestRunner测试报告美化

    前言 ​最近小伙伴们在学玩python,,看着那HTMLTestRunner生成的测试报告,左右看不顺眼,终觉得太丑.搜索了一圈没有找到合适的美化报告,于是忍不住自已动手进行了修改,因习惯python ...

  4. docker - 关于network的一些理解

    docker 提供给我们多种(4种)网络模式,我们可以根据自己的需求来使用.例如我们在一台主机(host)或者同一个docker engine上面运行continer的时候,我们就可以选择bridge ...

  5. 磁盘文件I/O,SSD结构,局部性原理 笔记

    磁盘文件I/O过程 进程向内核发起read scene.dat请求: 内核根据inode获取对应该进程的address space,在address space查找page_cache,如果没有找到, ...

  6. 浅谈卷积神经网络及matlab实现

    前言,好久不见,大家有没有想我啊.哈哈.今天我们来随便说说卷积神经网络. 1卷积神经网络的优点 卷积神经网络进行图像分类是深度学习关于图像处理的一个应用,卷积神经网络的优点是能够直接与图像像素进行卷积 ...

  7. cal日历工具的用法

    cal的基本语法:$ cal [month] [year] 1.显示当前月的日历 $ cal 2.显示某年的日历 $ cal 2015 3.显示某年某月日历 $ cal 12 2015 =-=-=-= ...

  8. 解决(防止)DDOS攻击的另一种思想

    本方案适合作最后的处理方案. 在服务器遭到DDOS攻击后,防火墙.高防盾或者其他的方案都已经失去了效力,这时运维人员无任何方案可以处理,并且只能任由DDOS攻击或关闭服务器时,该方案可以有限的抵挡大部 ...

  9. python实现微信接口(itchat)

    python实现微信接口(itchat) 安装 sudo pip install itchat 登录 itchat.auto_login() 这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实 ...

  10. ecshop循环计数

    循环依次递增+1 <!-- {foreach from=$comments item=comment name=comment} --> {$smarty.foreach.comment. ...