(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点击保存按钮
实体类:
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.math.BigDecimal;
import java.util.List;
/**
* <p>
* SkuInfo
* </p>
*
*/
@Data
@ApiModel(description = "SkuInfo")
@TableName("sku_info")
public class SkuInfo extends BaseEntity {
public SkuInfo(){}
public SkuInfo(Long skuId){
setId(skuId);
}
// 判断去重的话,自动调用equals 方法。
public boolean equals(SkuInfo skuInfo){
return getId().equals(skuInfo.getId());
}
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "商品id")
@TableField("spu_id")
private Long spuId;
@ApiModelProperty(value = "价格")
@TableField("price")
private BigDecimal price;
@ApiModelProperty(value = "sku名称")
@TableField("sku_name")
private String skuName;
@ApiModelProperty(value = "商品规格描述")
@TableField("sku_desc")
private String skuDesc;
@ApiModelProperty(value = "重量")
@TableField("weight")
private String weight;
@ApiModelProperty(value = "品牌(冗余)")
@TableField("tm_id")
private Long tmId;
@ApiModelProperty(value = "三级分类id(冗余)")
@TableField("category3_id")
private Long category3Id;
@ApiModelProperty(value = "默认显示图片(冗余)")
@TableField("sku_default_img")
private String skuDefaultImg;
@ApiModelProperty(value = "是否销售(1:是 0:否)")
@TableField("is_sale")
private Integer isSale;
@TableField(exist = false)
List<SkuImage> skuImageList;
@TableField(exist = false)
List<SkuAttrValue> skuAttrValueList;
@TableField(exist = false)
List<SkuSaleAttrValue> skuSaleAttrValueList;
}
需要用到四张表
创建对应数据库表实体类,以及接口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);
判断集合是否为空可以这样:
if (skuImageList != null && skuImageList.size() > 0)
需要先添加sku_info表生成skuid,下面的表插入会用到
图片值:
平台属性值:
销售属性值:
实现类:
@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();
}
sku信息表:
图片表:
sku平台属性关联表:
sku销售属性值表:
它来自:spu销售属性值表: