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查询语句,这里暂时不支持分页,后续补上

<select id="listItem" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from item order by sales DESC;
</select>
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<ItemModel> listItem();
//商品详情预览
ItemModel getItemById(Integer id);
}

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; @Service
public 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<ItemModel> listItem() {
List<ItemDO> itemDOList =itemDOMapper.listItem();
List<ItemModel> 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;
} }

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<ItemModel> itemModelList = itemService.listItem();
List<ItemVO> 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; }
}


springboot秒杀课程学习整理1-4的更多相关文章

  1. springboot秒杀课程学习整理1-6

    1)活动模型设计 配饰秒杀的模型(promoModel)id promoName startDate(建议使用joda-time) endDate itemId promoItemPrice 数据库( ...

  2. springboot秒杀课程学习整理1-1

    1)新建一个maven工程quickStart,然后在pom文件里添加依赖 <parent> <groupId>org.springframework.boot</gro ...

  3. springboot秒杀课程学习整理1-5

    1)交易模型设计 交易模型(用户下单的交易模型)OrderModel id(String 交易单号使用String), userId,itemId,amount(数量),orderAmount(总金额 ...

  4. springboot秒杀课程学习整理1-3

    1)实现手机验证码功能,用户注册功能,用户登入功能(这里讲开发流程,及本人遇到的问题,具体实现请看代码) 1.拦截请求,获取请求参数(这里的consumes是个常量,可以定义在baseControll ...

  5. springboot秒杀课程学习整理1-2

    1)从数据库到前端,做了三层转换,最后统一返回给前端的格式 DO-> model: 放在service中,目的是为了组装来自于数据库的数据,有些字段来自于不同的表的取,这一层相当于真正的业务模型 ...

  6. SpringBoot源码学习系列之异常处理自动配置

    SpringBoot源码学习系列之异常处理自动配置 1.源码学习 先给个SpringBoot中的异常例子,假如访问一个错误链接,让其返回404页面 在浏览器访问: 而在其它的客户端软件,比如postm ...

  7. 201671010450-姚玉婷-实验十四 团队项目评审&课程学习总结

    项目 内容 所属科目 软件工程http://www.cnblogs.com/nwnu-daizh 作业要求 https://www.cnblogs.com/nwnu-daizh/p/11093584. ...

  8. 金生芳-实验十四 团队项目评审&课程学习总结

    实验十四 团队项目评审&课程学习总结 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 作业学习目标 (1)掌握软件项目评审会流程(2)反思总结课 ...

  9. 201671030117 孙欢灵 实验十四 团队项目评审&课程学习总结

    项目 内容 作业所属课程 所属课程 作业要求 作业要求 课程学习目标 (1)掌握软件项目评审会流程:(2)反思总结课程学习内容 任务一:团队项目审核已完成.项目验收过程意见表已上交. 任务二:课程学习 ...

随机推荐

  1. 使用CA签发的服务器证书搭建Tomcat双向SSL认证服务

    第一部分,先说证书的申请. 这步是要到正规的CA公司申请正式的设备证书必须走的步骤. 1.先生成证书的密钥对 打开命令行,切换到某个自己新建的目录下,执行如下命令 keytool -genkey -k ...

  2. [c/c++] programming之路(30)、位运算(一)

    一.取反 ~ #include<stdio.h> #include<stdlib.h> void main(){ unsigned ; //0000 1111 char的单位是 ...

  3. python3中argparse模块

    1.定义:argparse是python标准库里面用来处理命令行参数的库 2.命令行参数分为位置参数和选项参数:         位置参数就是程序根据该参数出现的位置来确定的              ...

  4. Codeforces Round #503 (by SIS, Div. 2)

    连接:http://codeforces.com/contest/1020 C.Elections 题型:你们说水题就水题吧...我没有做出来...get到了新的思路,不虚.好像还有用三分做的? KN ...

  5. Python3 tkinter基础 Radiobutton variable 默认选中的按钮

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. 【POJ 2176】Folding

    [原题链接]传送门 [题面大意] 一个字符串,可以将它改写成循环节带括号的形式进行压缩,输出压缩长度最小的字符串. [题解思路] 1.没思路没思路,不知道怎么乱搞,大概就可以想到动态规划. 2.套路区 ...

  7. MyBatis Generator 自定义生成注释

    注释生成器 为了生成db里面的注释,必须自定义注释生成器 EmptyCommentGenerator: import org.mybatis.generator.api.CommentGenerato ...

  8. 【NOIP2015普及组】推销员_详解

    题目 题目大意 阿明是一名推销员--螺丝街是一条直线,一端有入口,一共有 \(N(<100,000)\) 家住户,第 \(i\) 家住户到入口的距离为 \(S_i\) 米.由于同一栋房子里可以有 ...

  9. T57274 黑暗城堡

    传送门 思路: 先求出各个点到 1 的最短路径.分别用两个数组将最短路径记录下来(一个要用来排序).按排序后的 dis 值从小到大枚举各点加入树有多少种方案,最后根据乘法原理把各个点的方案数乘起来就是 ...

  10. Java中的集合类,集合类有哪些,如何增添删除元素,如何遍历

    http://www.cnblogs.com/LittleHann/p/3690187.html import java.util.*; public class TestCollection { p ...