完成基本下单的学习
This commit is contained in:
@@ -1,13 +1,21 @@
|
||||
package cn.mayiming;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement
|
||||
@MapperScan("cn.mayiming.Mapper")
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package cn.mayiming.Controller;
|
||||
|
||||
|
||||
import cn.mayiming.Entity.StockDeductDTO;
|
||||
import cn.mayiming.Service.stockService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/stock")
|
||||
public class stockController {
|
||||
@Autowired
|
||||
stockService stockService;
|
||||
|
||||
@PostMapping("/deduct")
|
||||
public int deductstock(@Valid @RequestBody StockDeductDTO stockDeductDTO) {
|
||||
return stockService.deductStock(stockDeductDTO.getId(),stockDeductDTO.getDeductNum());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cn.mayiming.Entity;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class StockDeductDTO {
|
||||
@NotNull
|
||||
@JsonProperty("id")
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
@JsonProperty("deductnum")
|
||||
private Integer deductNum;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.mayiming.Mapper;
|
||||
|
||||
import feign.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface StockMapper {
|
||||
|
||||
@Select("SELECT stock_num FROM t_stock WHERE goods_id = #{goodsId}")
|
||||
Integer selectStockByGoodsId(@Param("goodsId") Long goodsId);
|
||||
|
||||
@Update("UPDATE t_stock SET stock_num = stock_num - #{deductNum} WHERE goods_id = #{goodsId} AND stock_num >= #{deductNum}")
|
||||
int deductStock(@Param("goodsId") Long goodsId, @Param("deductNum") Integer deductNum);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package cn.mayiming.Service;
|
||||
|
||||
|
||||
import cn.mayiming.Mapper.StockMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class stockService {
|
||||
@Autowired
|
||||
StockMapper stockMapper;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int deductStock(Long goodsId, int i) {
|
||||
return stockMapper.deductStock(goodsId, i);
|
||||
}
|
||||
}
|
||||
52
stock-service/src/main/resources/application.yml
Normal file
52
stock-service/src/main/resources/application.yml
Normal file
@@ -0,0 +1,52 @@
|
||||
server:
|
||||
port: 9094
|
||||
spring:
|
||||
application:
|
||||
name: stock-service
|
||||
cloud:
|
||||
nacos:
|
||||
# 服务注册发现配置
|
||||
discovery:
|
||||
server-addr: localhost:8848 # Nacos 服务地址(默认端口 8848)
|
||||
namespace: public # 命名空间(默认 public,自定义需先在 Nacos 控制台创建)
|
||||
group: DEFAULT_GROUP # 分组(默认 DEFAULT_GROUP)
|
||||
service: stock-service # 注册到 Nacos 的服务名(建议和子项目 artifactId 一致)
|
||||
# 配置管理配置(如果引入了 config 依赖才需要)
|
||||
config:
|
||||
server-addr: localhost:8848 # 和 discovery 一致
|
||||
file-extension: yaml
|
||||
namespace: public
|
||||
group: DEFAULT_GROUP
|
||||
config:
|
||||
import: nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension}?server-addr=${spring.cloud.nacos.config.server-addr}
|
||||
datasource:
|
||||
# 数据库驱动类(MySQL 8.x 用 com.mysql.cj.jdbc.Driver,5.x 用 com.mysql.jdbc.Driver)
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
# 数据库连接 URL(替换为你的数据库地址、端口、库名,如 user_db)
|
||||
url: jdbc:mysql://rm-f8z6oc5a03331500p8o.mysql.rds.aliyuncs.com:3306/stock_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||
# 数据库用户名(默认 root,根据实际情况修改)
|
||||
username: root
|
||||
# 数据库密码(替换为你的 MySQL 密码)
|
||||
password: Root123456
|
||||
# 可选:连接池配置(推荐使用 HikariCP,Spring Boot 2.x 默认)
|
||||
hikari:
|
||||
# 连接池最大连接数
|
||||
maximum-pool-size: 10
|
||||
# 连接池最小空闲连接数
|
||||
minimum-idle: 2
|
||||
# 连接超时时间(毫秒)
|
||||
connection-timeout: 30000
|
||||
# 连接最大存活时间(毫秒)
|
||||
max-lifetime: 1800000
|
||||
#rocketmq:
|
||||
# # NameServer 地址(替换为你的 RocketMQ 服务器IP)
|
||||
# name-server: 127.0.0.1:9876
|
||||
# consumer:
|
||||
# # 消费者组名(必须唯一,建议按服务+用途命名)
|
||||
# group: order-service-consumer
|
||||
# # 消费模式:CONCURRENTLY(并发消费,默认)/ORDERLY(顺序消费)
|
||||
# consume-mode: CONCURRENTLY
|
||||
# # 批量消费最大条数(默认1,单条消费)
|
||||
# consume-message-batch-max-size: 1
|
||||
# # 最大重试次数(消费失败后自动重试,超过次数进入死信队列)
|
||||
# max-reconsume-times: 3
|
||||
Reference in New Issue
Block a user