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. nginx + tomcat = http && https

    Tomcat版块配置: vim /to/path/conf/server.xml <Server port="" shutdown="SHUTDOWN"& ...

  2. Long类型参数传到前端精度丢失的解决方案

        由于公司数据库表的id是利用雪花算法生成的,所以实体类里面定义的数据类型为Long.但是这个数据传到前端时,发生了精度丢失的现象.本文记录了从java后端的角度如何解决这个精度丢失的问题,便于 ...

  3. Java线程和多线程(十五)——线程的活性

    当开发者在应用中使用了并发来提升性能的同时,开发者也需要注意线程之间有可能会相互阻塞.当整个应用执行的速度比预期要慢的时候,也就是应用没有按照预期的执行时间执行完毕.在本章中,我们来需要仔细分析可能会 ...

  4. MySQL之索引原理

    --------------------------------------------------------------------------------堕落的状态,无疑是慢性自杀.想想自己为什 ...

  5. Python_每日习题_0003_完全平方数

    # 题目 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? # 程序分析 因为168对于指数爆炸来说实在太小了,所以可以直接省略数学分析,用最朴素的方法来获取 ...

  6. 03-HTML之body标签

    body标签 HTML标签按作用主要分为两类:字体标签和排版标签 HTML标签按级别主要分为两类:文本级标签和容器级标签 文本级标签:p.span.a.b.i.u.em.文本标签里只能放文字.图片.表 ...

  7. iOS开发之一句代码检测APP版本的更新

    提示更新效果图如下,当然也是可以自定义类似与AlertView相似的自定义view,如京东.网易云音乐都是自定义了这种提示框的view.以下只展示,从App Store获取到app信息.并解析app信 ...

  8. NSAssert和NSParameterAssert

    2016.05.05 18:34* 字数 861 阅读 5127评论 0喜欢 17 https://www.jianshu.com/p/3072e174554f NSAssert和NSParamete ...

  9. Python_生成随机百分比的方法

    可以使用random模块去实现,给定1到100的空间,使用random的choice的方法随机选取一个数字,当这个数字在某个区间时就可以认定为出发了指定的百分比的概率. 这个简单的逻辑也可以在需要时扩 ...

  10. 使用Vue自己做一个简单的MarkDown在线编辑器

    1.首先要下载mark组件. npm install marked --save 2.在Vcontent.vue中简单写一些样式. <template> <div class=&qu ...