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>

最新文章

  1. 【Alpha】Daily Scrum Meeting第五次
  2. 【python+mysql】在python中调用mysql出问题 ImportError: No module named MySQLdb.constants
  3. jQuery.last() 函数
  4. Jexus针对Asp.net core应用程序的六大不可替代的优势
  5. 使用触发器实现记录oracle用户登录失败信息到alert.log日志文件
  6. apache activemq 学习笔记
  7. phonegap开发经验谈之一命令行建立项目和准备工作
  8. Android混淆问题
  9. 初学swift笔记字典、数组(四)
  10. HDU 4464 Browsing History(最大ASCII的和)
  11. php 求水仙花数优化
  12. 第三次冲刺spring会议(第五次会议)
  13. nginx初级安装配置
  14. C语言-while循环
  15. Vmware报错:此主机支持IntelVTx 但IntelVTx处于禁用状态
  16. C++STL(vector,map,set,list)成员函数整理
  17. 一个Bootstrap的例子--关于validate
  18. openstack 网络更改版
  19. 纪念L.A. Zadeh教授
  20. iOS.Thread.OSAtomic

热门文章

  1. IOS 单元测试
  2. accessor mothod mutator mothod 更改器方法 访问器方法 类的方法可以访问类的任何一个对象的私有域!
  3. Delphi的RTTI(许多参考链接)
  4. rac_udev建立磁盘方式安装grid时不识别磁盘
  5. POJ2559 Largest Rectangle in a Histogram —— 单调栈
  6. Window 64位下的客户机配置PLSQL链接远程Oracle
  7. 让Outlook一直保持开启
  8. complex brain network
  9. servlet过滤器Filter(理论篇)
  10. CMD help