在使用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. springboot 获取hibernate 的 SessionFactory

    注入bean package cn.xiaojf; import cn.xiaojf.today.data.rdb.repository.RdbCommonRepositoryImpl; import ...

  2. MyBatis_Generator插件的安装以及简单使用

    MyBatis_Generator_1.3.1.zip 1       下载安装包 安装包名称:MyBatis_Generator_1.3.1.zip 2       在Eclipse上进行安装 l  ...

  3. 利刃 MVVMLight

    已经很久没有写系列文章了,上一次是2012年写的HTLM5系列,想想我们应该是较早一批使用HTML5做项目的人. 相比我当时动不动100+的粉丝增长和两天3000+的阅读量,MVVM Light只能算 ...

  4. Java之File类

    一.初见File类 java.io.File类代表系统中的文件(文件或目录) 常用构造方法 File(String pathname) File(String parent, String child ...

  5. JParticles 2.0 发布,打造炫酷的粒子特效

    JParticles 2.0 发布,打造炫酷的粒子特效.不好意思哈,在这么繁花似锦的世界里,标题不得不取得吸引眼球一点哈,不然...还是不啰嗦了,我们进入正题吧 简单介绍一下 JParticles 2 ...

  6. SpringMVC的form:form表单的使用

    为什么要使用SpringMVC的form:form表单,有两个原因:一是可以更加快捷的完成表单的开发,比如会替你做好数据类型装换等本来需要你自己动手的工作.其次就是能够更加方便的实现表单回显. 首先要 ...

  7. Nmap在实战中的高级用法

    Nmap提供了四项基本功能(主机发现.端口扫描.服务与版本侦测.OS侦测)及丰富的脚本库.Nmap既能应用于简单的网络信息扫描,也能用在高级.复杂.特定的环境中:例如扫描互联网上大量的主机:绕开防火墙 ...

  8. (转)关于BigDecimal 转化字符串toPlainString()和toString()的区别

    对于  BigDecimal b ;     (b=(0.4321)^ 20)String s = b.toPlainString() ;System.out.println(s) ; 输出为:0.0 ...

  9. SQL Server 使用ROW_NUMBER实现的高效分页排序

    declare @pageNum int declare @pageSize int select * from (select ROW_NUMBER() over(order by a_Creati ...

  10. python web -- flask

    Flask是一个简洁的 Python_web 框架. 零. virtualenv 虚拟环境配置. $ easy_install pip $ pip install virtualenv $ virtu ...