SpringBoot构建电商基础秒杀项目 学习笔记

新建表

create table if not exists order_info (
id varchar(32) not null default '',
user_id int not null default 0,
item_id int not null default 0,
item_price double(10, 2) not null default 0,
amount int not null default 0,
order_price double(10, 2) default 0,
primary key (id)
); create table if not exists sequence_info (
name varchar(64) not null default '',
current_value int not null default 0,
step int not null default 1,
primary key (name)
);
insert into sequence_info (name, current_value, step) values ('order_info', 1, 1);

新增 OrderModel

public class OrderModel {
private String id;
private Integer userId;
private Integer itemId;
private BigDecimal itemPrice;
private Integer amount;
private BigDecimal orderPrice; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public Integer getUserId() {
return userId;
} public void setUserId(Integer userId) {
this.userId = userId;
} public Integer getItemId() {
return itemId;
} public void setItemId(Integer itemId) {
this.itemId = itemId;
} public BigDecimal getItemPrice() {
return itemPrice;
} public void setItemPrice(BigDecimal itemPrice) {
this.itemPrice = itemPrice;
} public Integer getAmount() {
return amount;
} public void setAmount(Integer amount) {
this.amount = amount;
} public BigDecimal getOrderPrice() {
return orderPrice;
} public void setOrderPrice(BigDecimal orderPrice) {
this.orderPrice = orderPrice;
}
}

新增 ItemService

public interface ItemService {

    ItemModel createItem(ItemModel itemModel) throws BusinessException;

    List<ItemModel> listItem();

    ItemModel getItemById(Integer id);

    boolean decreaseStock(Integer itemId, Integer amount);

    void increaseSales(Integer itemId, Integer amount) throws BusinessException;
}

新增 ItemServiceImpl

@Service
public class ItemServiceImpl implements ItemService { @Autowired
private ValidatorImpl validator; @Autowired
private ItemDOMapper itemDOMapper; @Autowired
private ItemStockDOMapper itemStockDOMapper; @Override
@Transactional
public ItemModel createItem(ItemModel itemModel) throws BusinessException { ValidationResult result = validator.validate(itemModel);
if(result.isHasErrors()){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
} ItemDO itemDO = convertFromModel(itemModel);
itemDOMapper.insertSelective(itemDO); itemModel.setId(itemDO.getId()); ItemStockDO itemStockDO = convertItemStockFromModel(itemModel);
itemStockDOMapper.insertSelective(itemStockDO); return getItemById(itemModel.getId());
} @Override
public List<ItemModel> listItem() {
List<ItemDO> itemDOList = itemDOMapper.listItem(); List<ItemModel> itemModelList = itemDOList.stream().map(itemDO -> {
ItemStockDO itemStockDO = itemStockDOMapper.selectByItemId(itemDO.getId());
ItemModel itemModel = convertFromDataObject(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 = convertFromDataObject(itemDO, itemStockDO); return itemModel;
} private ItemDO convertFromModel(ItemModel itemModel){
if(itemModel == null){
return null;
} ItemDO itemDO = new ItemDO();
BeanUtils.copyProperties(itemModel, itemDO); itemDO.setPrice(itemModel.getPrice().doubleValue()); return itemDO;
} private ItemStockDO convertItemStockFromModel(ItemModel itemModel){
if(itemModel == null){
return null;
} ItemStockDO itemStockDO = new ItemStockDO();
itemStockDO.setItemId(itemModel.getId());
itemStockDO.setStock(itemModel.getStock()); return itemStockDO;
} private ItemModel convertFromDataObject(ItemDO itemDO, ItemStockDO itemStockDO){
if(itemDO == null){
return null;
} ItemModel itemModel = new ItemModel();
BeanUtils.copyProperties(itemDO, itemModel); itemModel.setPrice(new BigDecimal(itemDO.getPrice())); itemModel.setStock(itemStockDO.getStock()); return itemModel;
} @Override
@Transactional
public boolean decreaseStock(Integer itemId, Integer amount) { int affectedRow = itemStockDOMapper.decreaseStock(itemId, amount); return affectedRow > 0;
} @Override
@Transactional
public void increaseSales(Integer itemId, Integer amount) throws BusinessException {
itemDOMapper.increaseSales(itemId, amount);
}
}

新增 OrderController

@Controller("order")
@RequestMapping("/order")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class OrderController extends BaseController { @Autowired
private OrderService orderService;
@Autowired
private HttpServletRequest httpServletRequest; @RequestMapping(value = "/createorder", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType createOrder(@RequestParam(name="itemId") Integer itemId,
@RequestParam(name="amount") Integer amount)
throws BusinessException { Boolean isLogin = (Boolean)httpServletRequest.getSession().getAttribute("LOGIN");
if(isLogin == null || !isLogin.booleanValue()){
throw new BusinessException(EmBusinessError.USER_NOT_LOGIN);
} UserModel userModel = (UserModel)httpServletRequest.getSession().getAttribute("LOGIN_USER"); OrderModel orderModel = orderService.createOrder(userModel.getId(), itemId, amount); return CommonReturnType.create(null);
}
}

源码:spring-boot-seckill

Spring Boot 构建电商基础秒杀项目 (十) 交易下单的更多相关文章

  1. Spring Boot 构建电商基础秒杀项目 (十二) 总结 (完结)

    SpringBoot构建电商基础秒杀项目 学习笔记 系统架构 存在问题 如何发现容量问题 如何使得系统水平扩展 查询效率低下 活动开始前页面被疯狂刷新 库存行锁问题 下单操作步骤多,缓慢 浪涌流量如何 ...

  2. Spring Boot 构建电商基础秒杀项目 (一) 项目搭建

    SpringBoot构建电商基础秒杀项目 学习笔记 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 maven 整合了所有的 jar 包, Spring Boot ...

  3. Spring Boot 构建电商基础秒杀项目 (十一) 秒杀

    SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists promo ( id int not null auto_increment, pro ...

  4. Spring Boot 构建电商基础秒杀项目 (九) 商品列表 & 详情

    SpringBoot构建电商基础秒杀项目 学习笔记 ItemDOMapper.xml 添加 <select id="listItem" resultMap="Bas ...

  5. Spring Boot 构建电商基础秒杀项目 (八) 商品创建

    SpringBoot构建电商基础秒杀项目 学习笔记 新建数据表 create table if not exists item ( id int not null auto_increment, ti ...

  6. Spring Boot 构建电商基础秒杀项目 (七) 自动校验

    SpringBoot构建电商基础秒杀项目 学习笔记 修改 UserModel 添加注解 public class UserModel { private Integer id; @NotBlank(m ...

  7. Spring Boot 构建电商基础秒杀项目 (六) 用户登陆

    SpringBoot构建电商基础秒杀项目 学习笔记 userDOMapper.xml 添加 <select id="selectByTelphone" resultMap=& ...

  8. Spring Boot 构建电商基础秒杀项目 (五) 用户注册

    SpringBoot构建电商基础秒杀项目 学习笔记 UserService 添加 void register(UserModel userModel) throws BusinessException ...

  9. Spring Boot 构建电商基础秒杀项目 (四) getotp 页面

    SpringBoot构建电商基础秒杀项目 学习笔记 BaseController 添加 public static final String CONTENT_TYPE_FORMED = "a ...

随机推荐

  1. Java中class的getName()和getCanonicalName()两个方法的区别

    getName()返回的是虚拟机里面的class的表示 getCanonicalName()返回的是更容易理解的表示 对于普通类来说,二者没什么区别,只是对于特殊的类型上有点表示差异 比如byte[] ...

  2. Luogu3605 [USACO17JAN]Promotion Counting晋升者计数

    Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...

  3. windows系统VS2017编译boost

    1. 下载boost, 解压,进入boost源目录 2. 打开vs2017 x86 CMD工具,输入bootstrap.bat,等待初始化完毕 x86编译 bjam stage --toolset=m ...

  4. 洛谷 P1596 [USACO10OCT]湖计数Lake Counting

    题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...

  5. c# 利用百度图像处理【人像分割】一键抠图

    百度AI开放平台-人像分割: http://ai.baidu.com/tech/body/seg 注意本文后面的话,百度这个技术效果太差劲了,国外这 https://www.remove.bg/ 个比 ...

  6. JavaEE学习之Maven配置文件pom.xml详解(转)

    一.引言 (本文转载自:http://blog.csdn.net/longeremmy/article/details/9670619) 使用maven有一些时间了,一直没有好好将pom配置文件每个节 ...

  7. SpringBoot如何使用拦截器

    1.配置拦截器 @Configuration public class WebMvcConfigurer extends WebMvcConfigurerAdapter { @Override pub ...

  8. 【Java并发.5】基础构建模块

    本章会介绍一些最有用的并发构建模块,有丶东西(最后一小节,纯干货). 5.1 同步容器类 同步容器类包括 Vector 和 Hashtable ,这些类实现线程安全的方式是:将它们的状态封装起来,并对 ...

  9. 作为一个.NET开发者,怎么看待和选择层出不穷的新技术,新架构?

    经常在一些技术社区看到这些的问题,一个.NET开发者去求职,看到应聘的公司的技术栈还是比较老的ASP.NET WEBFORM的时候,希望了解未来会否使用ASP.NET MVC的时候,没有获得肯定答复, ...

  10. Filebeat简介

    原文地址:http://blog.51cto.com/seekerwolf/2110174 收集日志的目的是有效的利用日志,有效利用日志的前提是日志经过格式化符合我们的要求,这样才能真正的高效利用收集 ...