resultMap的用法以及关联结果集映射
resultType
resultType可以把查询结果封装到pojo类型中,但必须pojo类的属性名和查询到的数据库表的字段名一致。
如果sql查询到的字段与pojo的属性名不一致,则需要使用resultMap将字段名和属性名对应起来,进行手动配置封装,将结果映射到pojo中
resultMap
resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。
先在Mapper文件中,配置基本的sql语句
<!-- 查询所有的订单数据 --><!-- resultMap:填入配置的resultMap标签的id值 --><select id="queryOrderAll" resultMap="orderResultMap">SELECT id, user_id,number,createtime, note FROM `order`</select>
配置resultMap标签,映射不同的字段和属性名
<!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo --><!-- id:设置ResultMap的id --><resultMap type="order" id="orderResultMap"><!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id --><!-- property:主键在pojo中的属性名 --><!-- column:主键在数据库中的列名 --><id property="id" column="id" /><!-- 定义普通属性 --><result property="userId" column="user_id" /><result property="number" column="number" /><result property="createtime" column="createtime" /><result property="note" column="note" /></resultMap>
结果就可以封装到pojo类型中
使用resultMap进行关联查询
一对一查询
一对一数据模型:订单用户
一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询。如果从用户信息出发查询用户下的订单信息则为一对多查询,因为一个用户可以下多个订单。
改造pojo类
在订单类中添加User属性,User属性是一个引用类型,用于存储关联查询的用户信息,因为关联关系是一对一,所以只需要添加单个属性即可
配置Mapper.xml配置文件
OrderMapper.xml
先使用id和result属性,映射order类的结果集,然后在使用association映射关联对象User的结果集
<resultMap type="order" id="orderUserResultMap"><id property="id" column="id" /><result property="userId" column="user_id" /><result property="number" column="number" /><result property="createtime" column="createtime" /><result property="note" column="note" /><!-- association :配置一对一属性 --><!-- property:order里面的User属性名 --><!-- javaType:属性类型 --><association property="user" javaType="user"><!-- id:声明主键,表示user_id是关联查询对象的唯一标识--><id property="id" column="user_id" /><result property="username" column="username" /><result property="address" column="address" /></association></resultMap><!-- 一对一关联,查询订单,订单内部包含用户属性 -->
<select id="queryOrderUserResultMap" resultMap="orderUserResultMap">SELECTo.id,o.user_id,o.number,o.createtime,o.note,u.username,u.addressFROM`order` oLEFT JOIN `user` u ON o.user_id = u.id
</select>
- 测试
@Test
public void testQueryOrderUserResultMap() {// mybatis和spring整合,整合之后,交给spring管理SqlSession sqlSession = this.sqlSessionFactory.openSession();// 创建Mapper接口的动态代理对象,整合之后,交给spring管理UserMapper userMapper = sqlSession.getMapper(UserMapper.class);// 使用userMapper执行根据条件查询用户,结果封装到Order类中List<Order> list = userMapper.queryOrderUserResultMap();for (Order o : list) {System.out.println(o);}// mybatis和spring整合,整合之后,交给spring管理sqlSession.close();
}
- 结果
一对多查询
查询所有用户信息及相关订单。
修改pojo类,在pojo类添加订单集合属性
修改UserMapper.xml配置文件
先使用id和result配置映射User类的结果,然后使用一对多关系的collection标签配置Order结果
<resultMap type="user" id="userOrderResultMap"><id property="id" column="id" /><result property="username" column="username" /><result property="birthday" column="birthday" /><result property="sex" column="sex" /><result property="address" column="address" /><!-- 配置一对多的关系property:填写pojo类中集合类类属性的名称javaType:填写集合类型的名称 --><collection property="orders" javaType="list" ofType="order"><!-- 配置主键,是关联Order的唯一标识 --><id property="id" column="oid" /><result property="number" column="number" /><result property="createtime" column="createtime" /><result property="note" column="note" /></collection>
</resultMap><!-- 一对多关联,查询订单同时查询该用户下的订单 -->
<select id="queryUserOrder" resultMap="userOrderResultMap">SELECTu.id,u.username,u.birthday,u.sex,u.address,o.id oid,o.number,o.createtime,o.noteFROM`user` uLEFT JOIN `order` o ON u.id = o.user_id
</select>
- 测试结果
resultMap的用法以及关联结果集映射相关推荐
- mysql中resultmap_@Results用法和resultMap的用法
MyBatis中使用@Results注解来映射查询结果集到实体类属性. (1)@Results的基本用法.当数据库字段名与实体类对应的属性名不一致时,可以使用@Results映射来将其对应起来.col ...
- mysql resultmap_MySQL数据库:mybatis的resultType和resultMap基础用法
在MySQL数据库中,resultType和resultMap都是用于返回多行查询.二者不能一起用.区别在于后者能解决复杂查询时定的映射问题(比如有两个表相关联,此时就要用resultMap了),希望 ...
- resultMap的用法
1.配置多个一对一(association)的时候,默认执行第一个一对一. 2.resultMap中不可以有多个association,多对多关系可以修改表结构,配置成2个一对多的表来实现, 添加一个 ...
- resultType及resultMap的用法
学习目标: 了解resultType的使用条件和基本使用 了解resultMap的使用条件基本使用 学习内容: 1. resultType的使用条件 resultType可以把查询结果封装到pojo ...
- java后台管理系统项目学习day10--mybatis<一对多,一对一>
目录 1.resultMap的用法及关联结果集映射 1-1.概念 1-2.新建一个worker表 1-3.重新创建一个module,名字为springboot_mybatis_demo3 1-4.创建 ...
- 【SpringBoot、java文件copy、Mybatis、SpringApplication】
Spring Boot 整合 MyBatis 快速搭建 Web 开发环境 SpringBoot项目创建与整合mybatis Java文件copy拷贝的4种方式 小白学SpringBoot mybati ...
- Mybatis resultMap 嵌套集合
resultMap 是 Mybatis 最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap 包含的元素: <!--column不做限制,可 ...
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap[转]
上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...
- Mybatis中强大的resultMap
本文来说下mybatis中的resultMap,在平时的开发中resultType使用的比较多.resultType在解决一对一的关系时候比较方便,但是在设计到多对多的时候,使用resultMap比较 ...
最新文章
- 如何发现数据中的异常值?对异常值是怎么处理的?
- Alpha 测试和Beta的区别
- Rhino脚本引擎技术介绍
- html在线拖拽环绕,jQuery实现html元素拖拽
- Blazor 中如何下载文件到浏览器
- Spring Boot微服务,Docker和Kubernetes研讨会–第一部分
- iframe关于滚动条的去除和保留
- InputStream中通过mark和reset方法重复利用缓存
- Custom Looks using Qt Style Sheets
- 无法生成会话打印机,点打印提示没有选择的打印机
- 使用hexo+GitHub搭建步骤
- MTK:NICODE与ASCII码使用
- debian mysql目录_debian Squeeze配置apache php mysql环境,debian中apache目录结
- python封装功能讲解_python学习28——面向对象实例讲解与封装
- 远程迅雷linux,Ubuntu 14.04安装迅雷Xware过程笔记
- CH340以及CH系列USB转串口驱动下载
- html - <thead>标签
- 笔记本散热不好怎么办
- php车牌识别,跨平台车牌识别应用 Light-LPR
- 一起来看看,除夕夜有哪些走心的文案?
热门文章
- 移动电子政务网站群发展与创新
- Git生成多个ssh-keygen
- Java Web —— Session 和 cookie 保存登录信息
- Linux下有哪些重启命令
- 唯品会Java开发工程师校招面经
- Error: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception:
- MT【340】彭塞列闭合定理
- python如何给字符串排序_Python语言字符串排序方法
- 8个宝藏级英语APP 0基础必备
- 在CF卡上安装Gentoo