forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Apr 1, 2020
1 parent
48522b1
commit 644ef85
Showing
16 changed files
with
517 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.2.2.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-52-multiple-datasource</artifactId> | ||
|
||
<dependencies> | ||
<!-- TODO web --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 实现对数据库连接池的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-jdbc</artifactId> | ||
</dependency> | ||
<dependency> <!-- 本示例,我们使用 MySQL --> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.48</version> | ||
</dependency> | ||
|
||
<!-- TODO mybatis --> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>2.1.2</version> | ||
</dependency> | ||
|
||
<!-- 实现对 dynamic-datasource 的自动化配置 --> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>dynamic-datasource-spring-boot-starter</artifactId> | ||
<version>3.0.0</version> | ||
</dependency> | ||
|
||
<!-- TODO 分布式事务 --> | ||
<dependency> | ||
<groupId>io.seata</groupId> | ||
<artifactId>seata-spring-boot-starter</artifactId> | ||
<version>1.1.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...ce/src/main/java/cn/iocoder/springboot/lab52/seatademo/MultipleDatasourceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.iocoder.springboot.lab52.seatademo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class MultipleDatasourceApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(MultipleDatasourceApplication.class, args); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...ource/src/main/java/cn/iocoder/springboot/lab52/seatademo/controller/OrderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cn.iocoder.springboot.lab52.seatademo.controller; | ||
|
||
import cn.iocoder.springboot.lab52.seatademo.service.OrderService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/order") | ||
public class OrderController { | ||
|
||
private Logger logger = LoggerFactory.getLogger(OrderController.class); | ||
|
||
@Autowired | ||
private OrderService orderService; | ||
|
||
@PostMapping("/create") | ||
public Integer createOrder(@RequestParam("userId") Long userId, | ||
@RequestParam("productId") Long productId, | ||
@RequestParam("price") Integer price) throws Exception { | ||
logger.info("收到下单请求,用户:{}, 商品:{}, 价格:{}", userId, productId, price); | ||
return orderService.createOrder(userId, productId, price); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...ltiple-datasource/src/main/java/cn/iocoder/springboot/lab52/seatademo/dao/AccountDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cn.iocoder.springboot.lab52.seatademo.dao; | ||
|
||
|
||
import org.apache.ibatis.annotations.Mapper; | ||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.Select; | ||
import org.apache.ibatis.annotations.Update; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Mapper | ||
@Repository | ||
public interface AccountDao { | ||
|
||
/** | ||
* 获取账户余额 | ||
* | ||
* @param userId 用户 ID | ||
* @return 账户余额 | ||
*/ | ||
@Select("SELECT balance FROM account WHERE id = #{userId}") | ||
Integer getBalance(@Param("userId") Long userId); | ||
|
||
/** | ||
* 扣减余额 | ||
* | ||
* @param price 需要扣减的数目 | ||
* @return 影响记录行数 | ||
*/ | ||
@Update("UPDATE account SET balance = balance - #{price} WHERE id = 1 AND balance >= ${price}") | ||
int reduceBalance(@Param("price") Integer price); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...multiple-datasource/src/main/java/cn/iocoder/springboot/lab52/seatademo/dao/OrderDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cn.iocoder.springboot.lab52.seatademo.dao; | ||
|
||
import cn.iocoder.springboot.lab52.seatademo.entity.OrderDO; | ||
import org.apache.ibatis.annotations.*; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Mapper | ||
@Repository | ||
public interface OrderDao { | ||
|
||
/** | ||
* 插入订单记录 | ||
* | ||
* @param order 订单 | ||
* @return 影响记录数量 | ||
*/ | ||
@Insert("INSERT INTO orders (user_id, product_id, pay_amount) VALUES (#{userId}, #{productId}, #{payAmount})") | ||
@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id") | ||
int saveOrder(OrderDO order); | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...ltiple-datasource/src/main/java/cn/iocoder/springboot/lab52/seatademo/dao/ProductDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cn.iocoder.springboot.lab52.seatademo.dao; | ||
|
||
|
||
import org.apache.ibatis.annotations.Mapper; | ||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.Select; | ||
import org.apache.ibatis.annotations.Update; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Mapper | ||
@Repository | ||
public interface ProductDao { | ||
|
||
/** | ||
* 获取库存 | ||
* | ||
* @param productId 商品编号 | ||
* @return 库存 | ||
*/ | ||
@Select("SELECT stock FROM product WHERE id = #{productId}") | ||
Integer getStock(@Param("productId") Long productId); | ||
|
||
/** | ||
* 扣减库存 | ||
* | ||
* @param productId 商品编号 | ||
* @param amount 扣减数量 | ||
* @return 影响记录行数 | ||
*/ | ||
@Update("UPDATE product SET stock = stock - #{amount} WHERE id = #{productId} AND stock >= #{amount}") | ||
int reduceStock(@Param("productId") Long productId, @Param("amount") Integer amount); | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...ltiple-datasource/src/main/java/cn/iocoder/springboot/lab52/seatademo/entity/OrderDO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cn.iocoder.springboot.lab52.seatademo.entity; | ||
|
||
/** | ||
* 订单实体 | ||
*/ | ||
public class OrderDO { | ||
|
||
/** 订单编号 **/ | ||
private Integer id; | ||
|
||
/** 用户编号 **/ | ||
private Long userId; | ||
|
||
/** 产品编号 **/ | ||
private Long productId; | ||
|
||
/** 支付金额 **/ | ||
private Integer payAmount; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public OrderDO setId(Integer id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public Long getUserId() { | ||
return userId; | ||
} | ||
|
||
public OrderDO setUserId(Long userId) { | ||
this.userId = userId; | ||
return this; | ||
} | ||
|
||
public Long getProductId() { | ||
return productId; | ||
} | ||
|
||
public OrderDO setProductId(Long productId) { | ||
this.productId = productId; | ||
return this; | ||
} | ||
|
||
public Integer getPayAmount() { | ||
return payAmount; | ||
} | ||
|
||
public OrderDO setPayAmount(Integer payAmount) { | ||
this.payAmount = payAmount; | ||
return this; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...-datasource/src/main/java/cn/iocoder/springboot/lab52/seatademo/service/OrderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cn.iocoder.springboot.lab52.seatademo.service; | ||
|
||
/** | ||
* 订单 Service | ||
*/ | ||
public interface OrderService { | ||
|
||
/** | ||
* 创建订单 | ||
* | ||
* @param userId 用户编号 | ||
* @param productId 产品编号 | ||
* @param price 价格 | ||
* @return 订单编号 | ||
* @throws Exception 创建订单失败,抛出异常 | ||
*/ | ||
Integer createOrder(Long userId, Long productId, Integer price) throws Exception; | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...le-datasource/src/main/java/cn/iocoder/springboot/lab52/seatademo/service/PayService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.iocoder.springboot.lab52.seatademo.service; | ||
|
||
/** | ||
* 支付 Service | ||
*/ | ||
public interface PayService { | ||
|
||
/** | ||
* 扣除余额 | ||
* | ||
* @param userId 用户编号 | ||
* @param price 扣减金额 | ||
* @throws Exception 失败时抛出异常 | ||
*/ | ||
void reduceBalance(Long userId, Integer price) throws Exception; | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...atasource/src/main/java/cn/iocoder/springboot/lab52/seatademo/service/StorageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.iocoder.springboot.lab52.seatademo.service; | ||
|
||
/** | ||
* 库存 Service | ||
*/ | ||
public interface StorageService { | ||
|
||
/** | ||
* 扣减库存 | ||
* | ||
* @param productId 商品 ID | ||
* @param amount 扣减数量 | ||
* @throws Exception 扣减失败时抛出异常 | ||
*/ | ||
void reduceStock(Long productId, Integer amount) throws Exception; | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...ce/src/main/java/cn/iocoder/springboot/lab52/seatademo/service/impl/OrderServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cn.iocoder.springboot.lab52.seatademo.service.impl; | ||
|
||
import cn.iocoder.springboot.lab52.seatademo.dao.OrderDao; | ||
import cn.iocoder.springboot.lab52.seatademo.entity.OrderDO; | ||
import cn.iocoder.springboot.lab52.seatademo.service.OrderService; | ||
import cn.iocoder.springboot.lab52.seatademo.service.PayService; | ||
import cn.iocoder.springboot.lab52.seatademo.service.StorageService; | ||
import com.baomidou.dynamic.datasource.annotation.DS; | ||
import io.seata.core.context.RootContext; | ||
import io.seata.spring.annotation.GlobalTransactional; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class OrderServiceImpl implements OrderService { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private OrderDao orderDao; | ||
|
||
@Autowired | ||
private PayService payService; | ||
|
||
@Autowired | ||
private StorageService storageService; | ||
|
||
@GlobalTransactional | ||
@Override | ||
@DS(value = "order-ds") | ||
public Integer createOrder(Long userId, Long productId, Integer price) throws Exception { | ||
Integer amount = 1; // 购买数量,暂时设置为 1。 | ||
|
||
logger.info("[createOrder] 当前 XID: {}", RootContext.getXID()); | ||
|
||
// 扣减库存 | ||
storageService.reduceStock(productId, amount); | ||
|
||
// 扣减余额 | ||
payService.reduceBalance(userId, price); | ||
|
||
// 保存订单 | ||
OrderDO order = new OrderDO().setUserId(userId).setProductId(productId).setPayAmount(amount * price); | ||
orderDao.saveOrder(order); | ||
logger.info("[createOrder] 保存订单: {}", order.getId()); | ||
|
||
// 返回订单编号 | ||
return order.getId(); | ||
} | ||
|
||
} |
Oops, something went wrong.