Victor Bu

户外, 旅行, 读书, 生活, 有趣

  • 首页
  • 旅行
  • 户外
  • 读书
  • 急救
  • 挨踢
所有文章 友链 关于我

Victor Bu

户外, 旅行, 读书, 生活, 有趣

  • 首页
  • 旅行
  • 户外
  • 读书
  • 急救
  • 挨踢

Spring Boot 构建电商基础秒杀项目 (十) 交易下单

2019-03-23

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

新建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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

1
2
3
4
5
6
7
8
9
10
11
12
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
@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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@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

赏

谢谢你请我吃糖果

支付宝
微信
  • Java
  • Spring Boot
  • IT

扫一扫,分享到微信

微信分享二维码
Spring Boot 构建电商基础秒杀项目 (十一) 秒杀
Spring Boot 构建电商基础秒杀项目 (九) 商品列表 & 详情
© 2021 Victor Bu
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • 海岛
  • 香港
  • 攻略
  • 急救
  • 徒步
  • 泰国
  • 东南亚
  • 柬埔寨
  • 越南
  • 甘肃
  • 深圳
  • 香港文化博物馆
  • 树莓派
  • Raspbian
  • Python
  • Samba
  • CentOS 7
  • Linux
  • Windows
  • Travis CI
  • Hexo
  • GitHub
  • GIS
  • Leaflet
  • VLC
  • SQL Server
  • hls
  • m3u8
  • WindowsAPICodePack-Shell
  • DotNetty
  • Modbus
  • CRC
  • HJ212
  • ngrok
  • js
  • Java
  • Spring
  • Spring Boot
  • Mybatis
  • Spring MVC
  • Netty
  • RESTful API
  • Unit testing
  • PLC
  • JPA
  • MySQL
  • Redis
  • Shell32
  • IDE
  • IDEA
  • MyBatis
  • Microservices
  • Spring Cloud
  • Eureka
  • Spring Security
  • OAuth2
  • JWT
  • Ribbon
  • Feign
  • Hystrix
  • Hystrix Dashboard
  • Turbine
  • Zuul
  • Spring Cloud Config
  • Spring Cloud Sleuth
  • Zipkin
  • Spring Boot Admin
  • UUID
  • Hibernate
  • Swagger
  • snowflake
  • CORS
  • RabbitMQ
  • Elasticsearch
  • Sharding-JDBC
  • MongoDB
  • Tomcat
  • JDK
  • MQTT
  • WebSocket
  • Kafka
  • Alibaba
  • Nacos
  • Spring Cloud Gateway
  • Dubbo
  • Sentinel
  • SkyWalking
  • Seata
  • MyBatis-Plus
  • RestTemplate
  • Jasypt
  • XXL-JOB
  • JJWT
  • Hyper-V
  • Flyway
  • Elastic
  • Kibana
  • Beats
  • Logstash
  • canal
  • MinIO
  • OSS
  • Shell
  • inotify
  • Paho
  • Loki
  • Promtail
  • Grafana
  • Prometheus
  • node exporter
  • Docker
  • Kubernetes
  • Alibaba Cloud
  • Jenkins
  • Maven
  • Git
  • Node.js
  • EMQ X
  • Google Authenticator
  • 医疗

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 友情链接1
  • 友情链接2
  • 友情链接3
  • 友情链接4
  • 友情链接5
  • 友情链接6
很惭愧

只做了一点微小的工作
谢谢大家