resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中。

resultMap包含的元素:

<!--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标签是使用嵌套查询,格式如下:

 <collection column="传递给嵌套查询语句的字段参数" property="pojo对象中集合属性" ofType="集合属性中的pojo对象" select="嵌套的查询语句" >
</collection>

注意:<collection>标签中的column:要传递给select查询语句的参数,如果传递多个参数,格式为column= ” {参数名1=表字段1,参数名2=表字段2} ;

以下以实例介绍resultMap的用法:

一、简单需求:一个商品的结果映射;

1、创建商品pojo对象:

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:

<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类的基础上中添加属性的集合:

    /**
* 属性集合
*/
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:

<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>

查询语句:

<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:

<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会执行下面的查询属性语句:

<select id="getAttribute"  resultMap="AttributeResultMap">
select a.ID,s.ATTRIBUTE_NAME
from t_shop_attribute a
where a.ID = #{skuId,jdbcType =BIGINT};
</select>

属性结果集映射:

<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>

Mybatis:resultMap的使用总结(转自https://www.cnblogs.com/kenhome/p/7764398.html)的更多相关文章

  1. 解决:oracle+myBatis ResultMap 类型为 map 时返回结果中存在 timestamp 时使用 jackson 转 json 报错

    前言:最近在做一个通用查询单表的组件,所以 sql 的写法就是 select *,然后 resultType="map" ,然后使用 jackson @ResponseBody 返 ...

  2. mybatis resultmap标签type属性什么意思

    mybatis resultmap标签type属性什么意思? :就表示被转换的对象啊,被转换成object的类型啊 <resultMap id="BaseResultMap" ...

  3. mybatis写当天 当月的数据 时间段数据https://www.cnblogs.com/xzjf/p/7600533.html

    mybatis写当天 当月的数据 时间段数据----https://www.cnblogs.com/xzjf/p/7600533.html

  4. 访问路径:https://i.cnblogs.com/posts?categoryid=925678

    https://i.cnblogs.com/posts?categoryid=925678

  5. URL https://i.cnblogs.com/EditPosts.aspx?opt=1

    URL url = new URL("https://i.cnblogs.com");URL url1 = new URL(url, "EditPosts.aspx?op ...

  6. 随笔二-https://www.cnblogs.com/shang1680/p/9657994.html

    作业要求来自https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 GitHub远程仓库的地址:https://github.com/ ...

  7. 211806189杨昊辰 https://www.cnblogs.com/honey1433223/

    211806189杨昊辰 https://www.cnblogs.com/honey1433223/

  8. https://www.cnblogs.com/h2zZhou/p/5440271.html

    https://www.cnblogs.com/h2zZhou/p/5440271.html

  9. https://www.cnblogs.com/soundcode/p/4174410.html

    https://www.cnblogs.com/soundcode/p/4174410.html 1.首先要在服务器端新建一个网站axpx页 然后再网站的后台写代码获取winform传过来的文件名. ...

随机推荐

  1. VLC RTP Over TCP

    在RTSP协议请求数据时,让VLC以TCP的方式获取服务器发来的RTP数据 不为别的,下次回复直接用博客链接就能回复大家了! 操作:工具 -> 首选项 然后: 搞定! ------------- ...

  2. GO 入门(一)

    1.下载安装go环境          https://golang.org/dl/ 2.检查环境变量配置情况,安装过程中会自动配置:GOROOT    和    Path 3.建立go工作区,并配置 ...

  3. 指定查询条件,查询对应的集合List(单表)

    TestDao.java(测试类) @Test public void findCollectionByConditionNoPage(){  ApplicationContext ac = new ...

  4. 使用Primose方式解决异步编程回调的一些问题--animate动画的例子

    function animate(dis, time) { var def = $.Deferred(); $('.boll') .animate({ left: dis + 'px' }, time ...

  5. Python2.7使用virtualenv windows7

    环境: python2.7 windows7 一.安装 H:\>pip install virtualenv 二.创建项目目录 H:\>mkdir envtest H:\>cd en ...

  6. [数据挖掘课程笔记]关联规则挖掘 - Apriori算法

    两种度量: 支持度(support)  support(A→B) = count(AUB)/N (N是数据库中记录的条数) 自信度(confidence)confidence(A→B) = count ...

  7. JVM client模式和Server模式

    我们把jdk安装完成后,在命名行输入java -version 不仅可以看到jdk版本相关信息,还会看到类似与 Java HotSpot(TM) 64-Bit Server VM (build 25. ...

  8. Xmpp学习之Asmack取经-asmack入门(一)

    1.XMPPConnection:它主要是用来创建一个跟XMPP服务端的Socket连接.它是与Jabber服务端的默认连接并且已经在RFC 3920中精确定义过了.示例如下: XMPPConnect ...

  9. 配置Nginx四层负载均衡

    nginx 支持TCP转发和负载均衡的支持 实现下面的架构: 看配置: #user nobody; worker_processes 1; #error_log logs/error.log; #er ...

  10. TCP与HTTP连接管理

    一. HTTP事务时延原因(HTTP权威指南 P86) 1.客户端首先需要根据URI确定WEB服务器的IP和端口号, 那么DNS解析上花的时间会很多(大多数HTTP客户端会有一个小的DNS缓存)   ...