设为首页 加入收藏

TOP

Mybatis之ResultMap的使用详解(二)
2019-09-23 11:18:20 】 浏览:87
Tags:Mybatis ResultMap 使用 详解
es; } /** * 设置属性集合 * @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 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇第十六章、初识数据库 下一篇win中Oracle简易客户端和plsql的..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目