1.商品新增final
1.1.实体类
SPU和SpuDetail实体类已经添加过,添加Sku和Stock对象:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-l3AgaY5X-1588785692734)(assets/ly-item结构图.png)]
Sku实体类:
@Data
@Table(name = "tb_sku")
public class Sku {
@Id
@KeySql(useGeneratedKeys = true)
private Long id;
private Long spuId;
private String title;
private String images;
private Long price;
private String ownSpec;// 商品特殊规格的键值对
private String indexes;// 商品特殊规格的下标
private Boolean enable;// 是否有效,逻辑删除用
private Date createTime;// 创建时间
private Date lastUpdateTime;// 最后修改时间
@Transient
private Integer stock;// 库存
}
Stock实体类:
@Data
@Table(name = "tb_stock")
public class Stock {
@Id
private Long skuId;
private Integer seckillStock;// 秒杀可用库存
private Integer seckillTotal;// 已秒杀数量
private Integer stock;// 正常库存
}
1.2.mapper
public interface SpuMapper extends Mapper<Spu> {
}
public interface StockMapper extends Mapper<Stock> {
}
1.3.controller
/**
* 商品的新增
* @param spu
* @return
*/
@PostMapping("goods")
public ResponseEntity<Void> saveGoods(@RequestBody Spu spu){
goodsService.saveGoods(spu);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
1.4.service
@Service
public class GoodsService {
@Autowired
private SpuMapper spuMapper;
@Autowired
private SpuDetailMapper spuDetailMapper;
@Autowired
private CategoryService categoryService;
@Autowired
private BrandService brandService;
@Autowired
private SkuMapper skuMapper;
@Autowired
StockMapper stockMapper;
public PageResult<Spu> querySpuByPage(Integer page, Integer rows, Boolean saleable, String key) {
//分页
PageHelper.startPage(page, rows);
//过滤
Example example = new Example(Spu.class);
Example.Criteria criteria = example.createCriteria();
//所搜字段过滤
if (StringUtils.isNotBlank(key)) {
criteria.andLike("title", "%" + key + "%");
}
//上下架过滤
if (saleable != null) {
criteria.andEqualTo("saleable", saleable);
}
//默认排序
example.setOrderByClause("last_Update_Time DESC");
//查询
List<Spu> spus = spuMapper.selectByExample(example);
//判断
if (CollectionUtils.isEmpty(spus)) {
throw new LyException(ExceptionEnum.GOODS_NOT_FOND);
}
//解析分类和品牌的名称
loadCategoryAndBrandName(spus);
//解析分页结果
PageInfo<Spu> info = new PageInfo<>(spus);
return new PageResult<>(info.getTotal(), spus);
}
private void loadCategoryAndBrandName(List<Spu> spus) {
for (Spu spu : spus) {
//处理分类名称
List<String> names = categoryService.queryByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()))
.stream().map(Category::getName).collect(Collectors.toList());
spu.setCname(StringUtils.join(names, "/"));
//处理品牌名称
spu.setBname(brandService.queryById(spu.getBrandId()).getName());
}
}
@Transactional
public void saveGoods(Spu spu) {
//新增spu
spu.setId(null);
spu.setCreateTime(new Date());
spu.setLastUpdateTime(spu.getLastUpdateTime());
spu.setSaleable(true);
spu.setValid(false);
int count = spuMapper.insert(spu);
if (count != 1) {
throw new LyException(ExceptionEnum.GOODS_SAVE_ERROR);
}
//新增detail
SpuDetail detail = spu.getSpuDetail();
detail.setSpuId(spu.getId()); //不明白
spuDetailMapper.insert(detail);
//定义库存集合
List<Stock> stockList = new ArrayList<>();
//新增sku
List<Sku> skus = spu.getSkus();
for (Sku sku : skus) {
sku.setCreateTime(new Date());
sku.setLastUpdateTime(sku.getCreateTime());
sku.setSpuId(spu.getId());
count = skuMapper.insert(sku);
if (count != 1) {
throw new LyException(ExceptionEnum.GOODS_SAVE_ERROR);
}
//新增库存
Stock stock = new Stock();
stock.setSkuId(sku.getId());
stock.setStock(sku.getStock());
stockList.add(stock);
}
//批量新增库存
stockMapper.insertList(stockList);
}
}
1.5.mapper继承新接口
注意:service中用到批量新增,用到stockMapper.insertList(stockList)
这需要引入import tk.mybatis.mapper.additional.insert.InsertListMapper
不要使用import tk.mybatis.mapper.common.special.InsertListMapper
原因:special需要:
批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等,另外该接口限制实体包含
id
属性并且必须为自增列
package com.leyou.common.mapper;
import tk.mybatis.mapper.additional.idlist.IdListMapper;
import tk.mybatis.mapper.additional.insert.InsertListMapper;
import tk.mybatis.mapper.annotation.RegisterMapper;
import tk.mybatis.mapper.common.Mapper;
/**
* @description
* @Author: mty
* @Date:2020/5/6 19:53
*/
@RegisterMapper
public interface BaseMapper<T> extends Mapper<T>, IdListMapper<T,Long>, InsertListMapper<T> {
}
public interface StockMapper extends BaseMapper<Stock> {
}