1.规格参数是啥
就是我们之前封装的属性分组里的属性,我们现在一组一组给他获取出来
2.sql
根据三级分类与spuId查出属性分组与对应属性的相关信息
SELECT pav.`spu_id`,ag.`attr_group_id`,ag.`attr_group_name`,ag.`catelog_id`,a.`attr_id`,a.`attr_name`,pav.`attr_value` FROM pms_attr_group ag
LEFT JOIN pms_attr_attrgroup_relation aar ON ag.`attr_group_id` = aar.`attr_group_id`
LEFT JOIN pms_attr a ON a.`attr_id` = aar.`attr_id`
LEFT JOIN pms_product_attr_value pav ON pav.`attr_id` =aar.`attr_id`
WHERE pav.`spu_id` = 2 AND ag.`catelog_id` = 225
3.前面的模型需要变动
使用内部类,映射到xml中会报错,如果要使用xml一步到位直接映射返回结果的话,需要把之前定义的内部类拿出来
模型变成如下
ItemVo
package com.atguigu.gulimall.product.vo;
import com.atguigu.gulimall.product.entity.SkuImagesEntity;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.entity.SpuInfoDescEntity;
import lombok.Data;
import java.util.List;
/**
* @author rengang
* @version 1.0
* @date 2021/4/19 9:20
*/
@Data
public class ItemVo {
//1.sku基本信息
SkuInfoEntity skuInfoEntity;
//2.sku的图片信息
List<SkuImagesEntity> skuImagesEntitys;
//3.sku的销售属性
SkuItemSaleAttrVo skuItemSaleAttrVo;
//4.spu的介绍
SpuInfoDescEntity spuInfoDescEntity;
//5.spu的规格参数信息(基本属性)
List<SpuItemAttrGroupVo> spuItemAttrGroupVos;
}
SkuItemSaleAttrVo
package com.atguigu.gulimall.product.vo;
import lombok.Data;
import lombok.ToString;
import java.util.List;
@Data
@ToString
public class SkuItemSaleAttrVo{
private Long attrId;
private String attrName;
private List<String> attrValues;
}
SpuItemAttrGroupVo
package com.atguigu.gulimall.product.vo;
import lombok.Data;
import lombok.ToString;
import java.util.List;
@Data
@ToString
public class SpuItemAttrGroupVo{
private String groupName;
private List<Attr> attrs;
}
4.dao层代码
AttrGroupDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.gulimall.product.dao.AttrGroupDao">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.atguigu.gulimall.product.entity.AttrGroupEntity" id="attrGroupMap">
<result property="attrGroupId" column="attr_group_id"/>
<result property="attrGroupName" column="attr_group_name"/>
<result property="sort" column="sort"/>
<result property="descript" column="descript"/>
<result property="icon" column="icon"/>
<result property="catelogId" column="catelog_id"/>
</resultMap>
<resultMap id="SpuItemAttrGroupVo" type="com.atguigu.gulimall.product.vo.SpuItemAttrGroupVo">
<result property="groupName" column="attr_group_name"/>
<collection property="attrs" ofType="com.atguigu.gulimall.product.vo.Attr">
<result property="attrName" column="attr_name"></result>
<result property="attrValue" column="attr_value"></result>
</collection>
</resultMap>
<select id="getAttrGroupWithAttrsByCatIdAndSpuId"
resultMap="SpuItemAttrGroupVo">
SELECT pav.`spu_id`,ag.`attr_group_id`,ag.`attr_group_name`,ag.`catelog_id`,a.`attr_id`,a.`attr_name`,pav.`attr_value`
FROM pms_attr_group ag
LEFT JOIN pms_attr_attrgroup_relation aar ON ag.`attr_group_id` = aar.`attr_group_id`
LEFT JOIN pms_attr a ON a.`attr_id` = aar.`attr_id`
LEFT JOIN pms_product_attr_value pav ON pav.`attr_id` =aar.`attr_id`
WHERE pav.`spu_id` = #{spuId} AND ag.`catelog_id` = #{catalogId}
</select>
</mapper>
AttrGroupDao.java
package com.atguigu.gulimall.product.dao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroupVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 属性分组
*
* @author leifengyang
* @email leifengyang@gmail.com
* @date 2019-10-01 21:08:49
*/
@Mapper
public interface AttrGroupDao extends BaseMapper<AttrGroupEntity> {
List<SpuItemAttrGroupVo> getAttrGroupWithAttrsByCatIdAndSpuId(Long spuId, Long catalogId);
}
5.单元测试
@Test
public void testGroupAttr(){
List<SpuItemAttrGroupVo> attrGroupWithAttrsByCatIdAndSpuId = attrGroupDao.getAttrGroupWithAttrsByCatIdAndSpuId(2l, 225l);
System.out.println("attrGroupWithAttrsByCatIdAndSpuId:"+attrGroupWithAttrsByCatIdAndSpuId);
}
打印结果如下,成功封装数据后并返回了
attrGroupWithAttrsByCatIdAndSpuId:[SpuItemAttrGroupVo(groupName=主体, attrs=[Attr(attrId=null, attrName=上市年份, attrValue=2019), Attr(attrId=null, attrName=入网型号, attrValue=ANG-AN00)]), SpuItemAttrGroupVo(groupName=基本信息, attrs=[Attr(attrId=null, attrName=机身长度(mm), attrValue=160.13mm)])]