resultMap的使用总结
Mybatis:resultMap的使用总结
resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中。
resultMap包含的元素:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性--> <resultMap id= "唯一的标识" type= "映射的pojo对象" > <id column= "表的主键字段,或者可以为查询语句中的别名字段" jdbcType= "字段类型" property= "映射pojo对象的主键属性" /> <result column= "表的一个字段(可以为任意表的一个字段)" jdbcType= "字段类型" property= "映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)" /> <association property= "pojo的一个对象属性" javaType= "pojo关联的pojo对象" > <id column= "关联pojo对象对应表的主键字段" jdbcType= "字段类型" property= "关联pojo对象的主席属性" /> <result column= "任意表的字段" jdbcType= "字段类型" property= "关联pojo对象的属性" /> </association> <!-- 集合中的property须为oftype定义的pojo对象的属性--> <collection property= "pojo的集合属性" ofType= "集合中的pojo对象" > <id column= "集合中pojo对象对应的表的主键字段" jdbcType= "字段类型" property= "集合中pojo对象的主键属性" /> <result column= "可以为任意表的字段" jdbcType= "字段类型" property= "集合中的pojo对象的属性" /> </collection> </resultMap> 复制代码 |
如果collection标签是使用嵌套查询,格式如下:
1
2
|
<collection column= "传递给嵌套查询语句的字段参数" property= "pojo对象中集合属性" ofType= "集合属性中的pojo对象" select= "嵌套的查询语句" > </collection> |
注意:<collection>标签中的column:要传递给select查询语句的参数,如果传递多个参数,格式为column= ” {参数名1=表字段1,参数名2=表字段2} ;
以下以实例介绍resultMap的用法:
一、简单需求:一个商品的结果映射;
1、创建商品pojo对象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
public class TShopSku { /** * 主键ID */ private Long id; /** * 商品名 */ private String skuName; /** * 分类ID */ private Long categoryId; /** * 主键ID * @return ID */ public Long getId() { return id; } /** * 主键ID, * @param id */ public void setId(Long id) { this .id = id; } /** * 商品名 * @return SKU_NAME 商品名 */ public String getSkuName() { return skuName; } /** * 商品名 * @param skuName 商品名 */ public void setSkuName(String skuName) { this .skuName = skuName == null ? null : skuName.trim(); } /** * 分类ID * @return CATEGORY_ID 分类ID */ public Long getCategoryId() { return categoryId; } /** * 分类ID * @param categoryId 分类ID */ public void setCategoryId(Long categoryId) { this .categoryId = categoryId; } 复制代码 |
对应的resultMap:
1
2
3
4
5
|
<resultMap id= "BaseResultMap" type= "com.meikai.shop.entity.TShopSku" > <id column= "ID" jdbcType= "BIGINT" property= "id" /> <result column= "SKU_NAME" jdbcType= "VARCHAR" property= "skuName" /> <result column= "CATEGORY_ID" jdbcType= "BIGINT" property= "categoryId" /> </resultMap> |
二、商品pojo类添加属性集合:
一个商品会有一些属性,现在需要将查询出的商品属性添加到商品对象中,首先需要在原商品pojo类的基础上中添加属性的集合:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
复制代码 /** * 属性集合 */ private List<TShopAttribute> attributes; /** * 获得属性集合 */ public List<TShopAttribute> getAttributes() { return attributes; } /** * 设置属性集合 * @param attributes */ public void setAttributes(List<TShopAttribute> attributes) { this .attributes = attributes; } 复制代码 |
将Collection标签添加到resultMap中,这里有两种方式:
1、嵌套结果:
对应的resultMap:
1
2
3
4
5
6
7
8
9
10
|
<resultMap id= "BasePlusResultMap" type= "com.meikai.shop.entity.TShopSku" > <id column= "ID" jdbcType= "BIGINT" property= "id" /> <result column= "SKU_NAME" jdbcType= "VARCHAR" property= "skuName" /> <result column= "CATEGORY_ID" jdbcType= "BIGINT" property= "categoryId" /> <collection property= "attributes" ofType= "com.meikai.shop.entity.TShopAttribute" > <id column= "AttributeID" jdbcType= "BIGINT" property= "id" /> <result column= "attribute_NAME" jdbcType= "VARCHAR" property= "attributeName" /> </collection> </resultMap> 复制代码 |
查询语句:
1
2
3
4
5
|
<select id= "getById" resultMap= "basePlusResultMap" > select s.ID,s.SKU_NAME,s.CATEGORY_ID,a.ID,a.ATTRIBUTE_NAME from t_shop_sku s,t_shop_attribute a where s.ID =a.SKU_ID and s.ID = #{id,jdbcType =BIGINT}; </select> |
2、关联的嵌套查询(在collection中添加select属性):
商品结果集映射resultMap:
1
2
3
4
5
6
7
|
<resultMap id= "BasePlusResultMap" type= "com.meikai.shop.entity.TShopSku" > <id column= "ID" jdbcType= "BIGINT" property= "id" /> <result column= "SKU_NAME" jdbcType= "VARCHAR" property= "skuName" /> <result column= "CATEGORY_ID" jdbcType= "BIGINT" property= "categoryId" /> <collection column= "{skuId=ID}" property= "attributes" ofType= "com.meikai.shop.entity.TShopAttribute" select= "getAttribute" > </collection> </resultMap> |
collection的select会执行下面的查询属性语句:
1
2
3
4
5
|
<select id= "getAttribute" resultMap= "AttributeResultMap" > select a.ID,s.ATTRIBUTE_NAME from t_shop_attribute a where a.ID = #{skuId,jdbcType =BIGINT}; </select> |
属性结果集映射:
1
2
3
4
|
<resultMap id= "AttributeResultMap" type= "com.meikai.shop.entity.TShopAttribute" > <id column= "ID" jdbcType= "BIGINT" property= "id" /> <result column= "ATTRIBUTE_NAME" jdbcType= "VARCHAR" property= "attributeName" /> </resultMap> |
BasePlusResultMap包含了属性查询语句的Collection
所以通过下面的查询商品语句就可获得商品以及其包含的属性集合:
<select id="getById" resultMap="BasePlusResultMap">
select s.ID,s.SKU_NAME,s.CATEGORY_ID
from t_shop_sku s
where s.ID = #{id,jdbcType =BIGINT};
</select>
原文: https://www.cnblogs.com/xinruyi
resultMap的使用总结的更多相关文章
- MyBatis的resultMap
1.大家学习MyBatis时,可能会碰到实体类属性跟数据库字段不同的情况 如:数据库 ------ 实体类 stuname ----> name 即: 数据库中的stuname字段对 ...
- MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作
一.getMapper()接口 解析:getMapper()接口 IDept.class定义一个接口, 挂载一个没有实现的方法,特殊之处,借楼任何方法,必须和小配置中id属性是一致的 通过代理:生成接 ...
- resultMap 映射
1. sql的重用:定义一个sql片段,可在任何SQL语句中重用该片段. <sql id="personColumns"> name, sex, updateTime& ...
- myBatis的一对多查询,主要利用resultMap实现一次查询多个结果集
日常开发中有这中场景,一个用户有多个角色,一个角色又有多个菜单,想查出一个用户的所有菜单.除了常见的关联查询之外,更使用的应该是利用myBatis的resultMap来实现一次查询出多个结果集,缺点: ...
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...
- MyBatis学习(二)、SQL语句映射文件(1)resultMap
二.SQL语句映射文件(1)resultMap SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyB ...
- mybatis - resultMap
resultMap有比较强大的自动映射,下面是摘自mybatis中文官网的的片段: 当自动映射查询结果时,MyBatis会获取sql返回的列名并在java类中查找相同名字的属性(忽略大小写). 这意味 ...
- [刘阳Java]_MyBatis_映射文件的resultMap标签入门_第4讲
<resultMap>:用于解决实体类中属性和表字段名不相同的问题 id:表示当前<resultMap>标签的唯一标识 result:定义表字段和实体类属性的对应关系 prop ...
- mybatis resultMap映射学习笔记
这几天,百度mybatis突然看不到官网了,不知道百度怎么整的.特此贴出mybatis中文官网: http://www.mybatis.org/mybatis-3/zh/index.html 一个学习 ...
- MyBatis中的resultType和resultMap
MyBatis的查询在进行映射的时候,返回值类型可以使用resultType同时也可以使用resultMap.前者表示直接的返回值类型,一般是domain名称,当然这里可以写domain的全部路径也可 ...
随机推荐
- 这打车App麻烦了!遭黑客勒索巨额比特币
6月17日下午,易到用车发布<客服电话故障公告>称,5月25日-26日,易到平台遭到网络黑客攻击,核心服务器被入侵,攻击导致易到核心数据被加密,服务器宕机,绝大部分服务功能受到波及,且攻击 ...
- CentOS7.6静默(无图形化界面)安装Oracle 11g
一.准备工作 1.准备CentOS 7 系统环境 由于是使用静默模式(silent)安装的,无需使用图形化界面,我选择了最小安装的服务器版的CentOS 7.安装完成后,只有命令行界面. 2.下载 O ...
- D-query SPOJ - DQUERY (莫队算法裸题)
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...
- Mongodb文档查询
MongoDB 查询数据的语法格式如下: db.collection.find(query, projection) query :可选,使用查询操作符指定查询条件 projection :可选,使用 ...
- 浅析Java web程序之客户端和服务器端交互原理
原文链接: https://www.iteye.com/topic/470019 1. 协议 a. TCP/IP整体构架概述 TCP/IP协议并不完全符合OSI的七层参考模型.传统的开放式系统互连参考 ...
- shell脚本if语句后面的中括号[]与java的if后面的小括号不同(),实际上[左中括号相当于test命令
四.shell 中的条件判断命令 test 和 [ test 命令可以处理 shell 脚本中的各类工作.它产生的不是一般的输出,而是可使用的退出状态.test 命令通过接受各种不同的参数,来控制 ...
- 写一个基于TCP协议套接字,服务端实现接收客户端的连接并发
''' 写一个基于TCP协议套接字,服务端实现接收客户端的连接并发 ''' client import socket import time client = socket.socket() clie ...
- 浮点数的存储、类型转换知识点(面宝P34)
以float a=1.0f为例: (int)a实际上是以浮点数a为参数构造了一个整型数,该整数的值是1: (int&)a则是告诉编译器将a当作整数看(并没有做任何实质上的转换),即读a的内存时 ...
- Leetcode练习题21. Merge Two Sorted Lists
题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...
- head first 设计模式笔记9-迭代器模式
迭代器模式:提供一种方法顺序访问一个集合对象中的各个元素,而又不暴露其内部的表示. 迭代器接口 /** * @author oy * @date 2019年9月22日 上午9:03:08 * @ver ...