商品页面创建的时候显示未知错误,debug发现ItemServiceImpl中this.type=No Such instance field:type

来源:4-3 商品模型模型--商品创建03

慕圣5603621

2019-10-29 11:04

http://img1.mukewang.com/5db7abd600016abf17580983.jpg

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

}


写回答 关注

2回答

  • 慕圣5603621
    2019-10-30 09:56:07

    看了下数据库,竟然每次创建都插入了数据,但是前端却报错失败,为什么呢

    是瓜哥啊

    请问解决了吗

    2021-05-25 00:07:57

    共 1 条回复 >

  • 扁鹊
    2019-10-29 18:56:52

    没有定义 type这个属性

    慕圣5603...

    可是这个type没有知道在哪里的错啊

    2019-10-29 18:58:26

    共 1 条回复 >

SpringBoot构建电商基础秒杀项目

应用SpringBoot快速搭建拥有用户、商品、交易及秒杀活动的电商秒杀应用。

49020 学习 · 954 问题

查看课程

相似问题