商品新增final

本文详细介绍了在Java中如何实现商品新增操作,涉及到实体类SPU、Sku、SpuDetail和Stock的创建,mapper接口及controller、service层的实现,特别是mapper接口的继承选择,强调了InsertListMapper与SpecialInsertListMapper的区别与适用场景。
摘要由CSDN通过智能技术生成

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> {
}

1.6.测试

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值