电商秒杀项目-商品模块
!!尽可能地让controller层简单 让service复杂
在做任何一个功能之前,首先不要去动数据库,优先考虑领域模型是要做成什么样的,先设计领域模型,再设计表
先建立model,分析它的模型行为。
在这个项目中把销量放在item表里面,当用户发生交易行为后,通过异步的方式给对应的item的销量值+1,而不会影响下单主链路,因此暂时把销量表放在这。
创建model-设计表结构并创建表和实体-写service和控制器-写页面功能-页面美化
商品创建
1.封装service请求用来创建商品
service中
1.校验入参
2.转化itemmodel->dataobject
3.写入数据库
4.返回创建完成的对象
2.返回service创建完成的对象
商品列表
修改mapping.xml中的语句查询商品list,然后在controller中调用这个方法,返回list,通过stream().map把model转化为vo再返回
商品详情
调用service中的方法返回model,再转成vo返回给前端。service中根据id查询iteminfo,再创建一个根据itemid查询stock的方法,在service中调用,查询转成model后一起返回。
controller
package com.miaosha3.controller;
import com.miaosha3.controller.viewobject.ItemVO;
import com.miaosha3.error.BusinessException;
import com.miaosha3.response.CommonReturnType;
import com.miaosha3.service.ItemService;
import com.miaosha3.service.model.ItemModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
@Controller("/items")
@RequestMapping("/items")
@CrossOrigin(origins = {"*"},allowCredentials = "true",allowedHeaders = "*") //跨域
public class ItemController extends BaseController {
@Autowired
private ItemService itemService;
//创建商品
@RequestMapping(value = "/create",method = {RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType create(@RequestParam(name = "title")String title,
@RequestParam(name = "price")BigDecimal price,
@RequestParam(name = "stock")Integer stock,
@RequestParam(name = "description")String description,
@RequestParam(name = "imgUrl")String imgUrl) throws BusinessException {
//验证
//封装service请求用来创建商品
ItemModel itemModel = new ItemModel();
itemModel.setTitle(title);
itemModel.setPrice(price);
itemModel.setStock(stock);
itemModel.setDescription(description);
itemModel.setImgUrl(imgUrl);
ItemModel itemModelReturn = itemService.createItem(itemModel);
ItemVO itemVO = convertVOFromModel(itemModelReturn);
return CommonReturnType.create(itemVO);
}
private ItemVO convertVOFromModel(ItemModel itemModel){
//在许多企业级应用里面 viewobject的定义和model的定义是完全不一样的,
//而且许多都会用到聚合操作
if (itemModel==null){
return null;
}
ItemVO itemVO = new ItemVO();
BeanUtils.copyProperties(itemModel,itemVO);
return itemVO;
}
//商品详情页浏览
@RequestMapping(value = "/get",method = {RequestMethod.GET},consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType getItems(@RequestParam(name = "id")Integer id){
ItemModel itemModel = itemService.getItemById(id);
ItemVO itemVO = convertVOFromModel(itemModel);
return CommonReturnType.create(itemVO);
}
//商品列表浏览
@RequestMapping(value = "/listitem",method = {RequestMethod.GET},consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType listItem(){
List<ItemModel> itemModelList = itemService.listItem();
List<ItemVO> itemVOList = itemModelList.stream().map(itemModel -> {
ItemVO itemVO = convertVOFromModel(itemModel);
return itemVO;
}).collect(Collectors.toList());
return CommonReturnType.create(itemVOList);
}
}
service
package com.miaosha3.service;
import com.miaosha3.error.BusinessException;
import com.miaosha3.service.model.ItemModel;
import java.util.List;
public interface ItemService {
//创建商品
ItemModel createItem(ItemModel itemModel) throws BusinessException;
//商品列表浏览
List<ItemModel> listItem();
//商品详情浏览
ItemModel getItemById(Integer id);
}
Ipml
package com.miaosha3.service.Impl;
import com.miaosha3.dao.ItemDOMapper;
import com.miaosha3.dao.ItemStockDOMapper;
import com.miaosha3.dataobject.ItemDO;
import com.miaosha3.dataobject.ItemStockDO;
import com.miaosha3.error.BusinessException;
import com.miaosha3.error.EmBusinessError;
import com.miaosha3.service.ItemService;
import com.miaosha3.service.model.ItemModel;
import com.miaosha3.validator.ValidationResult;
import com.miaosha3.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class ItemImpl implements ItemService {
@Autowired
private ValidatorImpl validator;
@Autowired
private ItemDOMapper itemDOMapper;
@Autowired
private ItemStockDOMapper itemStockDOMapper;
@Override
@Transactional
public ItemModel createItem(ItemModel itemModel) throws BusinessException{
//校验入参
/* ValidationResult validationResult = validator.validate(itemModel);
if (validationResult.isHasError()){
throw new BusinessException(EmBusinessError.PAEAMETER_VALIDATION_ERROR,validationResult.getErrMsg());
}*/
//转化itemmodel->dataobject
ItemDO itemDO = convertItemDOFromItemModel(itemModel);
//写入数据库
itemDOMapper.insertSelective(itemDO);
itemModel.setId(itemDO.getId());
ItemStockDO itemStockDO = convertItemStockDOFromItemModel(itemModel);
itemStockDOMapper.insertSelective(itemStockDO);
//返回创建完成的对象
return getItemById(itemModel.getId());
}
private ItemDO convertItemDOFromItemModel(ItemModel itemModel){
if (itemModel==null){
return null;
}
ItemDO itemDO = new ItemDO();
BeanUtils.copyProperties(itemModel,itemDO);
itemDO.setPrice(itemModel.getPrice().doubleValue());
return itemDO;
}
private ItemStockDO convertItemStockDOFromItemModel(ItemModel itemModel){
if (itemModel==null){
return null;
}
ItemStockDO itemStockDO = new ItemStockDO();
itemStockDO.setItemId(itemModel.getId());
itemStockDO.setStock(itemModel.getStock());
return itemStockDO;
}
@Override
public List<ItemModel> listItem() {
List<ItemDO> itemDOList = itemDOMapper.listItem();
//将里面的每一个itemDO map成一个itemmodel
List<ItemModel> itemModelList = itemDOList.stream().map(itemDO -> {
ItemStockDO itemStockDO = itemStockDOMapper.selectByItemId(itemDO.getId());
ItemModel itemModel = convertItemModelFromItemDO(itemDO,itemStockDO);
return itemModel;
}).collect(Collectors.toList());
return itemModelList;
}
@Override
public ItemModel getItemById(Integer id) {
ItemDO itemDO = itemDOMapper.selectByPrimaryKey(id);
if (itemDO==null){
return null;
}
ItemStockDO itemStockDO = itemStockDOMapper.selectByItemId(itemDO.getId());
ItemModel itemModel = convertItemModelFromItemDO(itemDO,itemStockDO);
return itemModel;
}
private ItemModel convertItemModelFromItemDO(ItemDO itemDO,ItemStockDO itemStockDO){
if (itemDO==null){
return null;
}
ItemModel itemModel = new ItemModel();
BeanUtils.copyProperties(itemDO,itemModel);
itemModel.setPrice(BigDecimal.valueOf(itemDO.getPrice()));
itemModel.setStock(itemStockDO.getStock());
return itemModel;
}
}
版权声明:本文为qq_40916110原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。