Skip to content

Commit 644ef85

Browse files
author
YunaiV
committed
增加 spring boot x seata
1 parent 48522b1 commit 644ef85

File tree

16 files changed

+517
-0
lines changed

16 files changed

+517
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.2.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-52-multiple-datasource</artifactId>
14+
15+
<dependencies>
16+
<!-- TODO web -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
22+
<!-- 实现对数据库连接池的自动化配置 -->
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-jdbc</artifactId>
26+
</dependency>
27+
<dependency> <!-- 本示例,我们使用 MySQL -->
28+
<groupId>mysql</groupId>
29+
<artifactId>mysql-connector-java</artifactId>
30+
<version>5.1.48</version>
31+
</dependency>
32+
33+
<!-- TODO mybatis -->
34+
<dependency>
35+
<groupId>org.mybatis.spring.boot</groupId>
36+
<artifactId>mybatis-spring-boot-starter</artifactId>
37+
<version>2.1.2</version>
38+
</dependency>
39+
40+
<!-- 实现对 dynamic-datasource 的自动化配置 -->
41+
<dependency>
42+
<groupId>com.baomidou</groupId>
43+
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
44+
<version>3.0.0</version>
45+
</dependency>
46+
47+
<!-- TODO 分布式事务 -->
48+
<dependency>
49+
<groupId>io.seata</groupId>
50+
<artifactId>seata-spring-boot-starter</artifactId>
51+
<version>1.1.0</version>
52+
</dependency>
53+
</dependencies>
54+
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab52.seatademo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class MultipleDatasourceApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(MultipleDatasourceApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.iocoder.springboot.lab52.seatademo.controller;
2+
3+
import cn.iocoder.springboot.lab52.seatademo.service.OrderService;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
@RequestMapping("/order")
14+
public class OrderController {
15+
16+
private Logger logger = LoggerFactory.getLogger(OrderController.class);
17+
18+
@Autowired
19+
private OrderService orderService;
20+
21+
@PostMapping("/create")
22+
public Integer createOrder(@RequestParam("userId") Long userId,
23+
@RequestParam("productId") Long productId,
24+
@RequestParam("price") Integer price) throws Exception {
25+
logger.info("收到下单请求,用户:{}, 商品:{}, 价格:{}", userId, productId, price);
26+
return orderService.createOrder(userId, productId, price);
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cn.iocoder.springboot.lab52.seatademo.dao;
2+
3+
4+
import org.apache.ibatis.annotations.Mapper;
5+
import org.apache.ibatis.annotations.Param;
6+
import org.apache.ibatis.annotations.Select;
7+
import org.apache.ibatis.annotations.Update;
8+
import org.springframework.stereotype.Repository;
9+
10+
@Mapper
11+
@Repository
12+
public interface AccountDao {
13+
14+
/**
15+
* 获取账户余额
16+
*
17+
* @param userId 用户 ID
18+
* @return 账户余额
19+
*/
20+
@Select("SELECT balance FROM account WHERE id = #{userId}")
21+
Integer getBalance(@Param("userId") Long userId);
22+
23+
/**
24+
* 扣减余额
25+
*
26+
* @param price 需要扣减的数目
27+
* @return 影响记录行数
28+
*/
29+
@Update("UPDATE account SET balance = balance - #{price} WHERE id = 1 AND balance >= ${price}")
30+
int reduceBalance(@Param("price") Integer price);
31+
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.iocoder.springboot.lab52.seatademo.dao;
2+
3+
import cn.iocoder.springboot.lab52.seatademo.entity.OrderDO;
4+
import org.apache.ibatis.annotations.*;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Mapper
8+
@Repository
9+
public interface OrderDao {
10+
11+
/**
12+
* 插入订单记录
13+
*
14+
* @param order 订单
15+
* @return 影响记录数量
16+
*/
17+
@Insert("INSERT INTO orders (user_id, product_id, pay_amount) VALUES (#{userId}, #{productId}, #{payAmount})")
18+
@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
19+
int saveOrder(OrderDO order);
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.iocoder.springboot.lab52.seatademo.dao;
2+
3+
4+
import org.apache.ibatis.annotations.Mapper;
5+
import org.apache.ibatis.annotations.Param;
6+
import org.apache.ibatis.annotations.Select;
7+
import org.apache.ibatis.annotations.Update;
8+
import org.springframework.stereotype.Repository;
9+
10+
@Mapper
11+
@Repository
12+
public interface ProductDao {
13+
14+
/**
15+
* 获取库存
16+
*
17+
* @param productId 商品编号
18+
* @return 库存
19+
*/
20+
@Select("SELECT stock FROM product WHERE id = #{productId}")
21+
Integer getStock(@Param("productId") Long productId);
22+
23+
/**
24+
* 扣减库存
25+
*
26+
* @param productId 商品编号
27+
* @param amount 扣减数量
28+
* @return 影响记录行数
29+
*/
30+
@Update("UPDATE product SET stock = stock - #{amount} WHERE id = #{productId} AND stock >= #{amount}")
31+
int reduceStock(@Param("productId") Long productId, @Param("amount") Integer amount);
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cn.iocoder.springboot.lab52.seatademo.entity;
2+
3+
/**
4+
* 订单实体
5+
*/
6+
public class OrderDO {
7+
8+
/** 订单编号 **/
9+
private Integer id;
10+
11+
/** 用户编号 **/
12+
private Long userId;
13+
14+
/** 产品编号 **/
15+
private Long productId;
16+
17+
/** 支付金额 **/
18+
private Integer payAmount;
19+
20+
public Integer getId() {
21+
return id;
22+
}
23+
24+
public OrderDO setId(Integer id) {
25+
this.id = id;
26+
return this;
27+
}
28+
29+
public Long getUserId() {
30+
return userId;
31+
}
32+
33+
public OrderDO setUserId(Long userId) {
34+
this.userId = userId;
35+
return this;
36+
}
37+
38+
public Long getProductId() {
39+
return productId;
40+
}
41+
42+
public OrderDO setProductId(Long productId) {
43+
this.productId = productId;
44+
return this;
45+
}
46+
47+
public Integer getPayAmount() {
48+
return payAmount;
49+
}
50+
51+
public OrderDO setPayAmount(Integer payAmount) {
52+
this.payAmount = payAmount;
53+
return this;
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.iocoder.springboot.lab52.seatademo.service;
2+
3+
/**
4+
* 订单 Service
5+
*/
6+
public interface OrderService {
7+
8+
/**
9+
* 创建订单
10+
*
11+
* @param userId 用户编号
12+
* @param productId 产品编号
13+
* @param price 价格
14+
* @return 订单编号
15+
* @throws Exception 创建订单失败,抛出异常
16+
*/
17+
Integer createOrder(Long userId, Long productId, Integer price) throws Exception;
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.iocoder.springboot.lab52.seatademo.service;
2+
3+
/**
4+
* 支付 Service
5+
*/
6+
public interface PayService {
7+
8+
/**
9+
* 扣除余额
10+
*
11+
* @param userId 用户编号
12+
* @param price 扣减金额
13+
* @throws Exception 失败时抛出异常
14+
*/
15+
void reduceBalance(Long userId, Integer price) throws Exception;
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.iocoder.springboot.lab52.seatademo.service;
2+
3+
/**
4+
* 库存 Service
5+
*/
6+
public interface StorageService {
7+
8+
/**
9+
* 扣减库存
10+
*
11+
* @param productId 商品 ID
12+
* @param amount 扣减数量
13+
* @throws Exception 扣减失败时抛出异常
14+
*/
15+
void reduceStock(Long productId, Integer amount) throws Exception;
16+
17+
}

0 commit comments

Comments
 (0)