(1)数据库表结构

根据以上的需求,以此将SKU关联的数据库表结构设计为如下:

base_attr_value:前面学的平台属性值表

我们进行关联,可以从分类导向平台,通过平台过滤商品

(2)平台属性添加

按照提供的属性进行添加更改 

修改AB:改为价格:

修改尺寸:

 修改长度:为机身存储:

机身内存就是机身存储:重复了,可以删除这个机身存储 

添加cpu型号:

商品spu管理

添加销售属性信息

(3)保存skuInfo前期准备

点击添加sku需要两个数据

或获取图片的接口、和获取销售属性的接口

先删除没用的测试数据:rrr

图片加载功能 

spu图片查询接口

功能分析:图片列表是根据spuId得来,涉及到的数据库表spu_image

添加接口实现类

/**
 * 根据spuId 查询spuImageList
 * @param spuId
 * @return
 */
List<SpuImage> getSpuImageList(Long spuId);

实现类

@Override
public List<SpuImage> getSpuImageList(Long spuId) {
    QueryWrapper<SpuImage> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("spu_id", spuId);
    return spuImageMapper.selectList(queryWrapper);
}

创建新的控制器

@Api(tags = "商品SKU接口")
@RestController
@RequestMapping("admin/product")
public class SkuManageController {

    @Autowired
    private ManageService manageService;

    /**
     * 根据spuId 查询spuImageList
     * @param spuId
     * @return
     */
    @GetMapping("spuImageList/{spuId}")
    public Result<List<SpuImage>> getSpuImageList(@PathVariable("spuId") Long spuId) {
        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }
}

 

销售属性查询接口:

 

 

 

 

SpuSaleAttr :销售属性实体类

package com.atguigu.gmall.model.product;

import com.atguigu.gmall.model.base.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

/**
 * <p>
 * SpuSaleAttr
 * </p>
 *
 */
@Data
@ApiModel(description = "销售属性")
@TableName("spu_sale_attr")
public class SpuSaleAttr extends BaseEntity {
	
	private static final long serialVersionUID = 1L;
	
	@ApiModelProperty(value = "商品id")
	@TableField("spu_id")
	private Long spuId;

	@ApiModelProperty(value = "销售属性id")
	@TableField("base_sale_attr_id")
	private Long baseSaleAttrId;

	@ApiModelProperty(value = "销售属性名称(冗余)")
	@TableField("sale_attr_name")
	private String saleAttrName;

	// 销售属性值对象集合
	@TableField(exist = false)
	List<SpuSaleAttrValue> spuSaleAttrValueList;

}

 

销售属性

/**
 * 根据spuId 查询销售属性集合
 * @param spuId
 * @return
 */
List<SpuSaleAttr> getSpuSaleAttrList(Long spuId);

实现类:

@Override
public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
    return spuSaleAttrMapper.selectSpuSaleAttrList(spuId);
}

SpuSaleAttrMapper :

@Mapper
public interface SpuSaleAttrMapper extends BaseMapper<SpuSaleAttr> {
    // 根据spuId 查询销售属性集合
    List<SpuSaleAttr> selectSpuSaleAttrList(Long spuId);

}

 创建SpuSaleAttrMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.atguigu.gmall.product.mapper.SpuSaleAttrMapper">


    <resultMap id="spuSaleAttrMap" type="com.atguigu.gmall.model.product.SpuSaleAttr" autoMapping="true">

    <id property="id" column="id"></id>
    <!--一对多-->
    <collection property="spuSaleAttrValueList" ofType="com.atguigu.gmall.model.product.SpuSaleAttrValue" autoMapping="true">
        <id property="id" column="sale_attr_value_id"></id>
    </collection>

</resultMap>

<sql id="spuSaleAttr"> 
sa.id ,sa.spu_id, sa.sale_attr_name,sa.base_sale_attr_id,
    sv.id sale_attr_value_id,
    sv.sale_attr_value_name
</sql>
<select id="selectSpuSaleAttrList" resultMap="spuSaleAttrMap">
    select
      <include refid="spuSaleAttr"></include>
    from spu_sale_attr sa inner join spu_sale_attr_value  sv
    on  sa.spu_id=sv.spu_id and sa.base_sale_attr_id=sv.base_sale_attr_id
    where  sa.spu_id=#{spu_id}
</select>
</mapper>

创建新的控制器SkuManagerController:


@Autowired
private ManagerService

/**
 * 根据spuId 查询销售属性和销售属性值集合
 * @param spuId
 * @return
 */
@GetMapping("spuSaleAttrList/{spuId}")
public Result<List<SpuSaleAttr>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {
    List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);
    return Result.ok(spuSaleAttrList);
}

 

(3)sku点击保存按钮

创建对应数据库表实体类,以及接口mapper

SkuInfoMapper:

@Mapper
public interface SkuInfoMapper extends BaseMapper<SkuInfo> {
}

SkuImageMapper: 

@Mapper
public interface SkuImageMapper extends BaseMapper<SkuImage> {
}

SkuAttrValueMapper :

@Mapper
public interface SkuAttrValueMapper extends BaseMapper<SkuAttrValue> {
}

SkuSaleAttrValueMapper :

@Mapper
public interface SkuSaleAttrValueMapper extends BaseMapper<SkuSaleAttrValue> {

}

编写接口与实现

/**
 * 保存数据
 * @param skuInfo
 */
void saveSkuInfo(SkuInfo skuInfo);

实现类:

@Override
@Transactional(rollbackFor = Exception.class)
public void saveSkuInfo(SkuInfo skuInfo) {
    /*
        skuInfo 库存单元表 --- spuInfo!
        skuImage 库存单元图片表 --- spuImage!
        skuSaleAttrValue sku销售属性值表{sku与销售属性值的中间表} --- skuInfo ,spuSaleAttrValue
        skuAttrValue sku与平台属性值的中间表 --- skuInfo ,baseAttrValue
     */
    skuInfoMapper.insert(skuInfo);
    List<SkuImage> skuImageList = skuInfo.getSkuImageList();
    if (skuImageList != null && skuImageList.size() > 0) {
        // 循环遍历
        for (SkuImage skuImage : skuImageList) {
            skuImage.setSkuId(skuInfo.getId());
            skuImageMapper.insert(skuImage);
        }
    }

    List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
    // 调用判断集合方法
    if (!CollectionUtils.isEmpty(skuSaleAttrValueList)) {
        for (SkuSaleAttrValue skuSaleAttrValue : skuSaleAttrValueList) {
            skuSaleAttrValue.setSkuId(skuInfo.getId());
            skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
            skuSaleAttrValueMapper.insert(skuSaleAttrValue);
        }
    }

    List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
    if (!CollectionUtils.isEmpty(skuAttrValueList)) {
        for (SkuAttrValue skuAttrValue : skuAttrValueList) {
            skuAttrValue.setSkuId(skuInfo.getId());
            skuAttrValueMapper.insert(skuAttrValue);
        }
    }
}

续集在控制器添加代码:SkuManageController

/**
 * 保存sku
 * @param skuInfo
 * @return
 */
@PostMapping("saveSkuInfo")
public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
    // 调用服务层
    manageService.saveSkuInfo(skuInfo);
    return Result.ok();
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部