(1)spu保存
这个页面选择了手机之后,会调用查询分类品牌的接口,显示品牌,在分类品牌中添加了两个品牌后也会在这里显示出来
销售属性名称需要查询base_sale_attr表:
实体类:BaseSaleAttr
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;
/**
* <p>
* BaseSaleAttr
* </p>
*
*/
@Data
@ApiModel(description = "销售属性")
@TableName("base_sale_attr")
public class BaseSaleAttr extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "销售属性名称")
@TableField("name")
private String name;
}
加载销售属性
创建mapper:BaseSaleAttrMapper
@Mapper
public interface BaseSaleAttrMapper extends BaseMapper<BaseSaleAttr> {
}
继续在MangeService添加接口
/**
* 查询所有的销售属性数据
* @return
*/
List<BaseSaleAttr> getBaseSaleAttrList();
实现类
@Autowired
private BaseSaleAttrMapper baseSaleAttrMapper;
@Override
public List<BaseSaleAttr> getBaseSaleAttrList() {
return baseSaleAttrMapper.selectList(null);
}
继续在控制器SpuManageController添加接口:
@RestController
@RequestMapping("admin/product")
public class SpuManageController {
// 引入服务层
@Autowired
private ManageService manageService;
// 销售属性http://api.gmall.com/admin/product/baseSaleAttrList
@GetMapping("baseSaleAttrList")
public Result baseSaleAttrList(){
// 查询所有的销售属性集合
List<BaseSaleAttr> baseSaleAttrList = manageService.getBaseSaleAttrList();
return Result.ok(baseSaleAttrList);
}
}
(2)保存后台代码
实体类:SpuInfo
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>
* SpuInfo
* </p>
*
*/
@Data
@ApiModel(description = "SpuInfo")
@TableName("spu_info")
public class SpuInfo extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "商品名称")
@TableField("spu_name")
private String spuName;
@ApiModelProperty(value = "商品描述(后台简述)")
@TableField("description")
private String description;
@ApiModelProperty(value = "三级分类id")
@TableField("category3_id")
private Long category3Id;
@ApiModelProperty(value = "品牌id")
@TableField("tm_id")
private Long tmId;
// 销售属性集合
@TableField(exist = false)
private List<SpuSaleAttr> spuSaleAttrList;
// 商品的图片集合
@TableField(exist = false)
private List<SpuImage> spuImageList;
// 商品的海报图片集合
@TableField(exist = false)
private List<SpuPoster> spuPosterList;
}
SpuImage :spu图片实体类
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;
/**
* <p>
* SpuImage
* </p>
*
*/
@Data
@ApiModel(description = "Spu图片")
@TableName("spu_image")
public class SpuImage extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "商品id")
@TableField("spu_id")
private Long spuId;
@ApiModelProperty(value = "图片名称")
@TableField("img_name")
private String imgName;
@ApiModelProperty(value = "图片路径")
@TableField("img_url")
private String imgUrl;
}
SpuPoster :spu海报实体类
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;
/**
* <p>
* SpuPoster
* </p>
*
*/
@Data
@ApiModel(description = "SpuPoster")
@TableName("spu_poster")
public class SpuPoster extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "商品id")
@TableField("spu_id")
private Long spuId;
@ApiModelProperty(value = "文件名称")
@TableField("img_name")
private String imgName;
@ApiModelProperty(value = "文件路径")
@TableField("img_url")
private String imgUrl;
}
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;
}
SpuSaleAttrValue :销售属性值实体类
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;
/**
* <p>
* SpuSaleAttrValue
* </p>
*
*/
@Data
@ApiModel(description = "销售属性值")
@TableName("spu_sale_attr_value")
public class SpuSaleAttrValue 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_value_name")
private String saleAttrValueName;
@ApiModelProperty(value = "销售属性名称(冗余)")
@TableField("sale_attr_name")
private String saleAttrName;
// 是否是默认选中状态
// @TableField("sale_attr_name")
// String isChecked;
@TableField(exist = false)
String isChecked;
}
创建mapper
建立对应的mapper 文件
SpuImageMapper、SpuSaleAttrMapper 、SpuSaleAttrValueMapper 、SpuPosterMapper
@Mapper
public interface SpuImageMapper extends BaseMapper<SpuImage> {
}
@Mapper
public interface SpuSaleAttrMapper extends BaseMapper<SpuSaleAttr> {
}
@Mapper
public interface SpuSaleAttrValueMapper extends BaseMapper<SpuSaleAttrValue> {
}
@Mapper
public interface SpuPosterMapper extends BaseMapper<SpuPoster> {
}
继续添加在接口添加代码
/**
* 保存商品数据
* @param spuInfo
*/
void saveSpuInfo(SpuInfo spuInfo);
实现类:
@Override
@Transactional(rollbackFor = Exception.class)
public void saveSpuInfo(SpuInfo spuInfo) {
/*
spuInfo;
spuImage;
spuSaleAttr;
spuSaleAttrValue;
spuPoster
*/
//保存spu信息
spuInfoMapper.insert(spuInfo);
//保存图片
// 获取到spuImage 集合数据
List<SpuImage> spuImageList = spuInfo.getSpuImageList();
// 判断不为空
if (!CollectionUtils.isEmpty(spuImageList)){
// 循环遍历
for (SpuImage spuImage : spuImageList) {
// 需要将spuId 赋值
spuImage.setSpuId(spuInfo.getId());
// 保存spuImge
spuImageMapper.insert(spuImage);
}
}
//保存销售属性
// 获取销售属性集合
List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
// 判断
if (!CollectionUtils.isEmpty(spuSaleAttrList)){
// 循环遍历
for (SpuSaleAttr spuSaleAttr : spuSaleAttrList) {
// 需要将spuId 赋值
spuSaleAttr.setSpuId(spuInfo.getId());
spuSaleAttrMapper.insert(spuSaleAttr);
//保存销售属性值
// 再此获取销售属性值集合
List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
// 判断
if (!CollectionUtils.isEmpty(spuSaleAttrValueList)){
// 循环遍历
for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttrValueList) {
// 需要将spuId, sale_attr_name 赋值
spuSaleAttrValue.setSpuId(spuInfo.getId());
spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
spuSaleAttrValueMapper.insert(spuSaleAttrValue);
}
}
}
}
//保存海报
// 获取到posterList 集合数据
List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
// 判断不为空
if (!CollectionUtils.isEmpty(spuPosterList)){
for (SpuPoster spuPoster : spuPosterList) {
// 需要将spuId 赋值
spuPoster.setSpuId(spuInfo.getId());
// 保存spuPoster
spuPosterMapper.insert(spuPoster);
}
}
}
继续在控制器添加代码
/**
* 保存spu
* @param spuInfo
* @return
*/
@PostMapping("saveSpuInfo")
public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
// 调用服务层的保存方法
manageService.saveSpuInfo(spuInfo);
return Result.ok();
}
(3)spu表关系: