慕圣5603621
2019-10-29 11:04
package cn.waqwb.seckilldemo.Service.impl; import cn.waqwb.seckilldemo.Service.ItemService; import cn.waqwb.seckilldemo.Service.model.ItemModel; import cn.waqwb.seckilldemo.error.BusinessException; import cn.waqwb.seckilldemo.error.EmBusinessError; import cn.waqwb.seckilldemo.mapper.ItemDOMapper; import cn.waqwb.seckilldemo.mapper.ItemStockDOMapper; import cn.waqwb.seckilldemo.po.ItemDO; import cn.waqwb.seckilldemo.po.ItemStockDO; import cn.waqwb.seckilldemo.validator.ValidationResult; import cn.waqwb.seckilldemo.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; /** * @author JokerQ * @date 2019/10/25 - 10:17 */ @Service public class ItemServiceImpl implements ItemService { @Autowired private ValidatorImpl validator; @Autowired private ItemDOMapper itemDOMapper; @Autowired private ItemStockDOMapper itemStockDOMapper; private ItemStockDO convertFromItemModel(ItemModel itemModel){ if (itemModel == null){ return null; } ItemStockDO itemStockDO = new ItemStockDO(); itemStockDO.setItenId(itemModel.getId()); itemStockDO.setStock(itemModel.getStock()); return itemStockDO; } private ItemDO itemDOConvertFromItemModel(ItemModel itemModel){ if (itemModel == null){ return null; } ItemDO itemDO = new ItemDO(); BeanUtils.copyProperties(itemModel,itemDO); itemDO.setPrice(itemModel.getPrice().doubleValue()); return itemDO; } @Override @Transactional public ItemModel createItem(ItemModel itemModel) throws BusinessException { ValidationResult result = validator.validate(itemModel); if (result.isHasErrors()) { throw new BusinessException(result.getErrMsg(), EmBusinessError.PARAMETER_VALIDATION_ERROR); } ItemDO itemDO = this.itemDOConvertFromItemModel(itemModel); itemDOMapper.insertSelective(itemDO); itemModel.setId(itemDO.getId()); ItemStockDO itemStockDO = this.convertFromItemModel(itemModel); itemStockDOMapper.insertSelective(itemStockDO); return this.getItemById(itemModel.getId()); } @Override public List<ItemModel> listItem() { return null; } @Override public ItemModel getItemById(Integer id) { ItemDO itemDO = itemDOMapper.selectByPrimaryKey(id); //TODO if (itemDO == null){ return null; } ItemStockDO itemStockDO = itemStockDOMapper.selectByItemId(itemDO.getId()); ItemModel itemModel = convertModelFromDataObject(itemDO,itemStockDO); return itemModel; } private ItemModel convertModelFromDataObject(ItemDO itemDO,ItemStockDO itemStockDO){ ItemModel itemModel = new ItemModel(); BeanUtils.copyProperties(itemDO,itemModel); itemModel.setPrice(new BigDecimal((itemDO.getPrice()))); itemModel.setStock(itemStockDO.getStock()); return itemModel; } }
package cn.waqwb.seckilldemo.controller; import cn.waqwb.seckilldemo.Service.ItemService; import cn.waqwb.seckilldemo.Service.model.ItemModel; import cn.waqwb.seckilldemo.controller.viewobject.ItemView; import cn.waqwb.seckilldemo.error.BusinessException; import cn.waqwb.seckilldemo.response.CommonReturnType; 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; /** * @author JokerQ * @date 2019/10/26 - 16:32 */ @Controller("/item") @RequestMapping("/item") @CrossOrigin(allowCredentials = "true",origins = {"*"}) public class ItemController extends BaseController{ @Autowired private ItemService itemService; @RequestMapping(value = "/create",method = {RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED}) @ResponseBody public CommonReturnType createItem( @RequestParam(name = "title")String title, @RequestParam(name = "description")String description, @RequestParam(name = "price") BigDecimal price, @RequestParam(name = "imgUrl")String imgUrl, @RequestParam(name = "stock")Integer stock ) throws BusinessException { ItemModel itemModel = new ItemModel(); itemModel.setTitle(title); itemModel.setDescription(description); itemModel.setPrice(price); itemModel.setStock(stock); itemModel.setImgUrl(imgUrl); ItemModel itemModelForReturn = itemService.createItem(itemModel); ItemView itemView = convertVOFromModel(itemModelForReturn); return CommonReturnType.create(itemView); } private ItemView convertVOFromModel(ItemModel itemModel){ if (itemModel == null){ return null; } ItemView itemView = new ItemView(); BeanUtils.copyProperties(itemModel,itemView); return itemView; } }
看了下数据库,竟然每次创建都插入了数据,但是前端却报错失败,为什么呢
没有定义 type这个属性
SpringBoot构建电商基础秒杀项目
49020 学习 · 954 问题
相似问题