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. permute

    repmat函数用法 复制和平铺矩阵   函数repmat 格式:  B = repmat(A, m, n) %将矩阵A复制m*n块,即B由m*n块A平铺而成 B = repmat(A, [m n]) ...

  2. 测试工具使用-Qunit单元测试使用过程

    031302620 应课程要求写一篇单元测试工具的博客,但是暂时没用到java,所以不想使用junit(对各种类都不熟悉的也不好谈什么测试),原计划是要用phpunit,但是安装经历了三个小时,查阅各 ...

  3. .NET开源项目 QuarkDoc 一款自带极简主义属性的文档管理系统

    有些话说在前头 因为公司产品业务重构且功能拆分组件化,往后会有很多的接口文档需要留存,所以急需一款文档管理系统.当时选型要求3点: 1.不能是云平台上的Saas服务,整个系统都要在自己公司部署维护(数 ...

  4. .NET 框架 Microsoft .NET Framework (更新至.NET Framework4.8)

    https://dotnet.microsoft.com/download/dotnet-framework 产品名称 离线安装包 .NET Framework 4.8 点击下载 .NET Frame ...

  5. 使用 IIS 在 Windows 上托管 ASP.NET Core2.0

    准备: 操作系统:Windows Server 2008 R2 或更高版本 开发环境:VS2017 第一步:新建项目ASP.NET Core Web应用程序 在 Visual Studio 中,选择“ ...

  6. 助力ASP.NET Core 2.1开发!Layx 企业级弹窗插件发布!

    我们在开发B/S架构企业管理系统时经常用到弹窗.目前市场上主要有两大弹窗:layer/artdialog,这两款做的都非常的棒.由于我们ERP系统比较复杂.需要能够拥有和Windows弹窗一样的弹窗组 ...

  7. C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 增强服务安全、阻止非授权的用户非法调用

    多一道防线,多一些安全保障,当程序发布到互联网上,再有成千上万的用户在用,总会有各种牛人出现,万一遇到破坏分子,那会有灾难性的打击. 只要跟利益有关系的,跟资金有关系,跟财务有关系,有竞争对手,软件系 ...

  8. H5 58-网页的布局方式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. matplotlib中subplot的使用

    #plt.subplot的使用 import numpy as npimport matplotlib.pyplot as pltx=[1,2,3,4]y=[5,4,3,2]plt.subplot(2 ...

  10. ibeacon和蓝牙有什么区别_它们的区别在哪里

    iBeacon概述 iBeacon是苹果公司2013年9月发布的移动设备用OS(iOS7)上配备的新功能.其工作方式是,配备有低功耗蓝牙(BLE)通信功能的设备使用BLE技术向周围发送自己特有的ID, ...