博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot秒杀课程学习整理1-4
阅读量:6587 次
发布时间:2019-06-24

本文共 7343 字,大约阅读时间需要 24 分钟。

1)商品模型设计

      (应该是先设计商品的model,然后才是数据库表)

      模型字段(id,title,price(double),stock(库存),description,sales,imgUrl)

      创建表   item(id,title,price,description,sales,imgUrl)

                    item_stock(id,stock,item_id)

2)  使用mybatis-generator生成dataObject及dao文件

     【1】 这里需要修改pom文件里的插件配置,将overwrite改成false不允许覆盖(否则之前修改的文件都        会被覆盖掉)

     【2】修改mybatis_generator.xml文件,将原来已经生成过的注释掉新增生成的表的配置

     【3】执行mybatis-generator命令

3)  生成的Mapping文件插入数据不会返回id

    可以在该方法加上一下几个属性

useGeneratedKeys="true" keyColumn="SUBJECT_ID" keyProperty="subjectId"

4)存中文进入数据库是会乱码

      新建数据库时要设置字符集,然后连接数据库的链接添加useUnicode=true&characterEncoding=utf8

5)获取商品列表实现

      在itemMapping的xml文件里添加sql查询语句,这里暂时不支持分页,后续补上

itemService
package com.miaoshaproject.service;import com.miaoshaproject.error.BusinessException;import com.miaoshaproject.service.model.ItemModel;import java.util.List;public interface ItemService {    //创建商品    ItemModel createItem(ItemModel itemModel) throws BusinessException;    //商品列表浏览    List
listItem(); //商品详情预览 ItemModel getItemById(Integer id);}
View Code

   itemServiceImpl

package com.miaoshaproject.service.impl;import com.miaoshaproject.dao.ItemDOMapper;import com.miaoshaproject.dao.StockDOMapper;import com.miaoshaproject.dataobject.ItemDO;import com.miaoshaproject.dataobject.StockDO;import com.miaoshaproject.error.BusinessException;import com.miaoshaproject.error.EmBusinessError;import com.miaoshaproject.service.ItemService;import com.miaoshaproject.service.model.ItemModel;import com.miaoshaproject.validator.ValidationResult;import com.miaoshaproject.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;@Servicepublic class ItemServiceImpl implements ItemService {    @Autowired    private ValidatorImpl validator;    @Autowired    private ItemDOMapper itemDOMapper;    @Autowired    private StockDOMapper stockDOMapper;    @Override    @Transactional //?不懂    public ItemModel createItem(ItemModel itemModel) throws BusinessException {        ValidationResult result=validator.validate(itemModel);        if(result.isHasError()){            throw new BusinessException(EmBusinessError.PARAMTER_VALIDATION_ERROR,result.getErrMsg());        }        ItemDO itemDO=this.convertItemDOFromItemModel(itemModel);        itemDOMapper.insertSelective(itemDO);        itemModel.setId(itemDO.getId());        StockDO stockDO=this.convertItemStockFromItemModel(itemModel);        stockDOMapper.insertSelective(stockDO);        return this.getItemById(itemModel.getId());    }    @Override    public List
listItem() { List
itemDOList =itemDOMapper.listItem(); List
itemModelList= itemDOList.stream().map(itemDO ->{ StockDO stockDO = stockDOMapper.selectByItemId(itemDO.getId()); ItemModel itemModel=this.converItemModelFromItemDO(itemDO,stockDO); return itemModel; }).collect(Collectors.toList()); return itemModelList; } @Override public ItemModel getItemById(Integer id) { ItemDO itemDO=itemDOMapper.selectByPrimaryKey(id); if(itemDO == null){ return null; } StockDO stockDO=stockDOMapper.selectByItemId(itemDO.getId()); ItemModel itemModel=this.converItemModelFromItemDO(itemDO,stockDO); return itemModel; } public ItemDO convertItemDOFromItemModel(ItemModel itemModel){ if(itemModel==null){ return null; }; ItemDO itemDO=new ItemDO(); BeanUtils.copyProperties(itemModel,itemDO); itemDO.setPrice(itemModel.getPrice().doubleValue()); return itemDO; } public StockDO convertItemStockFromItemModel(ItemModel itemModel){ StockDO stockDO=new StockDO(); stockDO.setItemId(itemModel.getId()); stockDO.setStock(itemModel.getStock()); return stockDO; } public ItemModel converItemModelFromItemDO(ItemDO itemDO,StockDO stockDO){ ItemModel itemModel=new ItemModel(); BeanUtils.copyProperties(itemDO,itemModel); itemModel.setPrice(new BigDecimal(itemDO.getPrice())); itemModel.setStock(stockDO.getStock()); return itemModel; }}
View Code

  itemController

package com.miaoshaproject.controller;import com.miaoshaproject.controller.viewobject.ItemVO;import com.miaoshaproject.error.BusinessException;import com.miaoshaproject.response.CommonReturnType;import com.miaoshaproject.service.impl.ItemServiceImpl;import com.miaoshaproject.service.model.ItemModel;import org.springframework.beans.BeanUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.math.BigDecimal;import java.util.List;import java.util.stream.Collectors;@RestController@RequestMapping("/item")@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")public class ItemController extends BaseController{    @Autowired    private ItemServiceImpl itemService;    //创建商品的controller    @RequestMapping(value="add",method = {RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})    public CommonReturnType createItem(@RequestParam(name="title")String title,                                       @RequestParam(name="description")String description,                                       @RequestParam(name="price") BigDecimal price,                                       @RequestParam(name="stock")Integer stock,                                       @RequestParam(name="imgUrl")String imgUrl                                       ) throws BusinessException {        //疯转service请求用来创建商品        ItemModel itemModel=new ItemModel();        itemModel.setTitle(title);        itemModel.setDescription(description);        itemModel.setPrice(price);        itemModel.setStock(stock);        itemModel.setImgUrl(imgUrl);        ItemModel itemModelForReturn = itemService.createItem(itemModel);        ItemVO itemVO=this.convertItemVOFromItemModel(itemModelForReturn);        return CommonReturnType.create(itemVO);    }    //商品详情    @RequestMapping(value="detail",method = {RequestMethod.GET})    @ResponseBody    public CommonReturnType getItem(@RequestParam(name="id")Integer id){        ItemModel itemModel=itemService.getItemById(id);        ItemVO itemVO=convertItemVOFromItemModel(itemModel);        return CommonReturnType.create(itemVO);    }    //商品列表    @RequestMapping(value="list",method = {RequestMethod.GET})    @ResponseBody    public CommonReturnType getList(){        List
itemModelList = itemService.listItem(); List
itemVOList = itemModelList.stream().map(itemModel->{ ItemVO itemVO=this.convertItemVOFromItemModel(itemModel); return itemVO; }).collect(Collectors.toList()); return CommonReturnType.create(itemVOList); } public ItemVO convertItemVOFromItemModel(ItemModel itemModel){ ItemVO itemVO=new ItemVO(); if(itemModel == null){ return null; } BeanUtils.copyProperties(itemModel,itemVO); return itemVO; }}
View Code

 

 

转载于:https://www.cnblogs.com/llcMite/p/10739100.html

你可能感兴趣的文章
”一个封锁操作被对 WSACancelBlockingCall 的调用中断“。解决办法
查看>>
【原创】sysbench 使用总结
查看>>
android:theme决定AlertDialog的背景颜色
查看>>
递归练习(C语言)
查看>>
线性表的链式表示和实现
查看>>
由"缓存"到"Memcached分布式缓存"
查看>>
(一四〇)访问控制:protected
查看>>
几个单词
查看>>
关于vue项目本地运行以后,输入本机ip不能访问的问题
查看>>
idea找不到或无法加载主类
查看>>
我人生中的第一场Java面试
查看>>
redux速成法典
查看>>
java灵活传参之builder模式
查看>>
上集: Android开源库大全分类汇总(Android技术资料汇总)
查看>>
iOS面试知识点(一)
查看>>
交易所相继上线BCH合约,意味着什么?
查看>>
阿里云爬虫风险管理产品商业化,为云端流量保驾护航
查看>>
Centos 6更新yum安装源
查看>>
mysql数据库字符集问题
查看>>
我的友情链接
查看>>