Compare commits
10 Commits
6cb44520c3
...
b59f70afbe
Author | SHA1 | Date |
---|---|---|
|
b59f70afbe | |
|
ab4d9aa70f | |
|
22bcf26694 | |
|
0acd71150e | |
|
ee182a9c08 | |
|
7a14264bb5 | |
|
a66ce7abbf | |
|
f4c6b38175 | |
|
2dfe3cbac2 | |
|
79dc92805a |
|
@ -1,13 +0,0 @@
|
||||||
package cn.bunny;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hello world!
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class App
|
|
||||||
{
|
|
||||||
public static void main( String[] args )
|
|
||||||
{
|
|
||||||
System.out.println( "Hello World!" );
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -28,6 +28,6 @@ public class Knife4jConfig {
|
||||||
// 管理员相关分类接口
|
// 管理员相关分类接口
|
||||||
@Bean
|
@Bean
|
||||||
public GroupedOpenApi groupedOpenAdminApi() {
|
public GroupedOpenApi groupedOpenAdminApi() {
|
||||||
return GroupedOpenApi.builder().group("默认请求接口").pathsToMatch("/api/**").build();
|
return GroupedOpenApi.builder().group("默认请求接口").pathsToMatch("/**").build();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,11 +0,0 @@
|
||||||
package cn.bunny.dao.constant;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class LocalDateTimeConstant {
|
|
||||||
public static final String YYYY_MM_DD = "yyyy-MM-dd";
|
|
||||||
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
|
||||||
public static final String YYYY_MM_DD_HH_MM_SS_SLASH = "yyyy/MM/dd HH:mm:ss";
|
|
||||||
public static final String YYYY_MM_DD_HH_MM_SS_UNDERLINE = "yyyy_MM_dd_HH_mm_ss_SSS";
|
|
||||||
}
|
|
|
@ -1,35 +0,0 @@
|
||||||
package cn.bunny.dao.constant;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class MinioConstant {
|
|
||||||
public static final String favicon = "favicon";
|
|
||||||
public static final String avatar = "avatar";
|
|
||||||
public static final String message = "message";
|
|
||||||
public static final String carousel = "carousel";
|
|
||||||
public static final String feedback = "feedback";
|
|
||||||
public static final String backup = "backup";
|
|
||||||
public static final Map<String, String> typeMap = new HashMap<>();
|
|
||||||
|
|
||||||
static {
|
|
||||||
typeMap.put(favicon, "/favicon/");
|
|
||||||
typeMap.put(avatar, "/avatar/");
|
|
||||||
typeMap.put(message, "/message/");
|
|
||||||
typeMap.put(carousel, "/carousel/");
|
|
||||||
typeMap.put(feedback, "/feedback/");
|
|
||||||
typeMap.put(backup, "/backup/");
|
|
||||||
typeMap.put("images", "/images/");
|
|
||||||
typeMap.put("video", "/video/");
|
|
||||||
typeMap.put("default", "/default/");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getType(String type) {
|
|
||||||
String value = typeMap.get(type);
|
|
||||||
if (value != null) return value;
|
|
||||||
throw new RuntimeException("上传类型错误或缺失");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
package cn.bunny.dao.constant;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Redis用户前缀设置
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class RedisUserConstant {
|
|
||||||
// 过期时间
|
|
||||||
public static final Long REDIS_EXPIRATION_TIME = 7L;// 7 天/分钟 Redis过期
|
|
||||||
public static final Integer Cookie_EXPIRATION_TIME = 5 * 60 * 60;// cookies 过期时间 5 分钟
|
|
||||||
public static final String WEB_CONFIG_KEY = "webConfig::platformConfig";// web配置
|
|
||||||
|
|
||||||
private static final String ADMIN_LOGIN_INFO_PREFIX = "admin::login_info::";
|
|
||||||
private static final String ADMIN_EMAIL_CODE_PREFIX = "admin::email_code::";
|
|
||||||
private static final String USER_LOGIN_INFO_PREFIX = "user::login_info::";
|
|
||||||
private static final String USER_EMAIL_CODE_PREFIX = "user::email_code::";
|
|
||||||
|
|
||||||
public static String getAdminLoginInfoPrefix(String adminUser) {
|
|
||||||
return ADMIN_LOGIN_INFO_PREFIX + adminUser;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getAdminUserEmailCodePrefix(String adminUser) {
|
|
||||||
return ADMIN_EMAIL_CODE_PREFIX + adminUser;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
package cn.bunny.dao.constant;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class UserConstant {
|
|
||||||
public static final String USER_AVATAR = "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132";
|
|
||||||
public static final String PERSON_DESCRIPTION = "这个人很懒没有介绍...";
|
|
||||||
}
|
|
|
@ -1,18 +0,0 @@
|
||||||
package cn.bunny.dao.entity;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
|
||||||
@Data
|
|
||||||
@Schema(name = "BaseUserEntity", title = "基础信息字段包含用户信息", description = "基础信息字段包含用户信息")
|
|
||||||
public class BaseUserEntity extends BaseEntity {
|
|
||||||
|
|
||||||
@Schema(name = "username", title = "用户名")
|
|
||||||
private String createUsername;
|
|
||||||
|
|
||||||
@Schema(name = "nickname", title = "昵称")
|
|
||||||
private String updateUsername;
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
package cn.bunny.dao.enums;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Schema(description = "Email Template Types")
|
|
||||||
public enum EmailTemplateEnums {
|
|
||||||
VERIFICATION_CODE("verification_code", "邮箱验证码发送"),
|
|
||||||
NOTIFICATION("notification", "通知型邮件"),
|
|
||||||
WARNING("warning", "警告型邮件"),
|
|
||||||
;
|
|
||||||
|
|
||||||
private final String type;
|
|
||||||
private final String summary;
|
|
||||||
|
|
||||||
EmailTemplateEnums(String type, String summary) {
|
|
||||||
this.type = type;
|
|
||||||
this.summary = summary;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,20 +0,0 @@
|
||||||
package cn.bunny.dao.enums;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
public enum JobEnums {
|
|
||||||
FINISH("finish", "完成"),
|
|
||||||
UNFINISHED("unfinished", "未完成"),
|
|
||||||
RUNNING("running", "正在运行"),
|
|
||||||
ERROR("error", "错误"),
|
|
||||||
;
|
|
||||||
|
|
||||||
private final String type;
|
|
||||||
private final String summary;
|
|
||||||
|
|
||||||
JobEnums(String type, String summary) {
|
|
||||||
this.type = type;
|
|
||||||
this.summary = summary;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,18 +0,0 @@
|
||||||
package cn.bunny.dao.vo;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
|
||||||
@Data
|
|
||||||
@Schema(name = "BaseVo", title = "基础返回对象内容包含用户信息", description = "基础返回对象内容包含用户信息")
|
|
||||||
public class BaseUserVo extends BaseVo {
|
|
||||||
|
|
||||||
@Schema(name = "username", title = "用户名")
|
|
||||||
private String createUsername;
|
|
||||||
|
|
||||||
@Schema(name = "nickname", title = "昵称")
|
|
||||||
private String updateUsername;
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>cn.bunny</groupId>
|
||||||
|
<artifactId>cloud1</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>gateway</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>gateway</name>
|
||||||
|
<url>https://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<scope>annotationProcessor</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,13 @@
|
||||||
|
package cn.bunny;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class GatewayMainApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(GatewayMainApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package cn.bunny.config;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
|
||||||
|
import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class VipRoutePredicateFactory extends AbstractRoutePredicateFactory<VipRoutePredicateFactory.Config> {
|
||||||
|
public VipRoutePredicateFactory() {
|
||||||
|
super(Config.class);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> shortcutFieldOrder() {
|
||||||
|
return Arrays.asList("param", "value");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Predicate<ServerWebExchange> apply(Config config) {
|
||||||
|
return (GatewayPredicate) serverWebExchange -> {
|
||||||
|
ServerHttpRequest request = serverWebExchange.getRequest();
|
||||||
|
|
||||||
|
String first = request.getQueryParams().getFirst(config.getParam());
|
||||||
|
|
||||||
|
return StringUtils.hasText(first) && first.equals(config.getValue());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 可以配置的参数
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Validated
|
||||||
|
public static class Config {
|
||||||
|
@NotEmpty
|
||||||
|
private String param;
|
||||||
|
|
||||||
|
@NotEmpty
|
||||||
|
private String value;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package cn.bunny.filter;
|
||||||
|
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||||
|
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class OnceTokenGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
|
||||||
|
@Override
|
||||||
|
public GatewayFilter apply(NameValueConfig config) {
|
||||||
|
|
||||||
|
return (exchange, chain) -> chain.filter(exchange)
|
||||||
|
.then(Mono.fromRunnable(() -> {
|
||||||
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
HttpHeaders headers = response.getHeaders();
|
||||||
|
String value = config.getValue();
|
||||||
|
|
||||||
|
if ("uuid".equalsIgnoreCase(value)) {
|
||||||
|
value = UUID.randomUUID().toString();
|
||||||
|
} else if ("uuid2".equalsIgnoreCase(value)) {
|
||||||
|
value = "JWT生产内容";
|
||||||
|
}
|
||||||
|
|
||||||
|
headers.add(config.getName(), value);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package cn.bunny.filter;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class RouterGlobalFilter implements GlobalFilter, Ordered {
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
|
||||||
|
log.info("请求【{}】开始;开始时间:{}", request.getURI(), System.currentTimeMillis());
|
||||||
|
|
||||||
|
// 因为异步编程,不能直接在 chain.filter(exchange) 后面写因为是异步的
|
||||||
|
return chain.filter(exchange)
|
||||||
|
.doFinally(result -> {
|
||||||
|
log.info("请求【{}】结束;开始时间:{}", request.getURI(), System.currentTimeMillis());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
spring:
|
||||||
|
cloud:
|
||||||
|
gateway:
|
||||||
|
globalCors:
|
||||||
|
cors-configurations:
|
||||||
|
'[/**]':
|
||||||
|
allowed-origin-patterns:
|
||||||
|
- "*"
|
||||||
|
allowed-headers:
|
||||||
|
- "*"
|
||||||
|
allowed-methods:
|
||||||
|
- "*"
|
||||||
|
routes:
|
||||||
|
- id: service-baidu
|
||||||
|
uri: https://www.baidu.com
|
||||||
|
predicates:
|
||||||
|
- name: Path
|
||||||
|
args:
|
||||||
|
patterns: /s
|
||||||
|
- name: Query
|
||||||
|
args:
|
||||||
|
param: wd
|
||||||
|
regexp: .*
|
||||||
|
# 从这个往上只要:http://localhost:8800/s?wd=49 即可通过
|
||||||
|
|
||||||
|
- name: Vip # 名称为 Vip RoutePredicateFactory
|
||||||
|
args:
|
||||||
|
param: user
|
||||||
|
value: bunny
|
||||||
|
# 从这个往上:http://localhost:8800/s?wd=49&user=bunny
|
||||||
|
- id: service-cloud1
|
||||||
|
uri: lb://service-cloud1
|
||||||
|
predicates:
|
||||||
|
- name: Path
|
||||||
|
args:
|
||||||
|
patterns: /api/cloud1/**
|
||||||
|
matchTrailingSlash: true
|
||||||
|
filters:
|
||||||
|
- RewritePath=/api/cloud1/?(?<segment>.*), /cloud1/$\{segment} # 网关路径重写
|
||||||
|
#- AddResponseHeader=X-Response-Abc,123 # 在请求头中添加
|
||||||
|
order: 1
|
||||||
|
|
||||||
|
- id: service-cloud2
|
||||||
|
uri: lb://service-cloud2 # 使用负载均衡代理请求
|
||||||
|
predicates:
|
||||||
|
- Path=/api/cloud2/** # 匹配规则,开头/api/**
|
||||||
|
filters:
|
||||||
|
- RewritePath=/api/cloud2/?(?<segment>.*), /cloud2/$\{segment} # 网关路径重写
|
||||||
|
# 默认过滤器
|
||||||
|
default-filters:
|
||||||
|
- AddResponseHeader=X-Response-Abc,123 # 在请求头中添加
|
||||||
|
- OnceToken=X-Response-Token, uuid # 配置完成后在响应头中添加 UUID
|
|
@ -0,0 +1,12 @@
|
||||||
|
server:
|
||||||
|
port: 8800
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: service-gateway
|
||||||
|
profiles:
|
||||||
|
include:
|
||||||
|
- route
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
server-addr: 192.168.3.132
|
||||||
|
|
|
@ -0,0 +1,99 @@
|
||||||
|
CREATE DATABASE IF NOT EXISTS `storage_db`;
|
||||||
|
USE `storage_db`;
|
||||||
|
DROP TABLE IF EXISTS `storage_tbl`;
|
||||||
|
CREATE TABLE `storage_tbl`
|
||||||
|
(
|
||||||
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`commodity_code` varchar(255) DEFAULT NULL,
|
||||||
|
`count` int(11) DEFAULT 0,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY (`commodity_code`)
|
||||||
|
) ENGINE = InnoDB
|
||||||
|
DEFAULT CHARSET = utf8;
|
||||||
|
INSERT INTO storage_tbl (commodity_code, count)
|
||||||
|
VALUES ('P0001', 100);
|
||||||
|
INSERT INTO storage_tbl (commodity_code, count)
|
||||||
|
VALUES ('B1234', 10);
|
||||||
|
|
||||||
|
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
|
||||||
|
DROP TABLE IF EXISTS `undo_log`;
|
||||||
|
CREATE TABLE `undo_log`
|
||||||
|
(
|
||||||
|
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||||
|
`branch_id` bigint(20) NOT NULL,
|
||||||
|
`xid` varchar(100) NOT NULL,
|
||||||
|
`context` varchar(128) NOT NULL,
|
||||||
|
`rollback_info` longblob NOT NULL,
|
||||||
|
`log_status` int(11) NOT NULL,
|
||||||
|
`log_created` datetime NOT NULL,
|
||||||
|
`log_modified` datetime NOT NULL,
|
||||||
|
`ext` varchar(100) DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
|
||||||
|
) ENGINE = InnoDB
|
||||||
|
AUTO_INCREMENT = 1
|
||||||
|
DEFAULT CHARSET = utf8;
|
||||||
|
|
||||||
|
CREATE DATABASE IF NOT EXISTS `order_db`;
|
||||||
|
USE `order_db`;
|
||||||
|
DROP TABLE IF EXISTS `order_tbl`;
|
||||||
|
CREATE TABLE `order_tbl`
|
||||||
|
(
|
||||||
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`user_id` varchar(255) DEFAULT NULL,
|
||||||
|
`commodity_code` varchar(255) DEFAULT NULL,
|
||||||
|
`count` int(11) DEFAULT 0,
|
||||||
|
`money` int(11) DEFAULT 0,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) ENGINE = InnoDB
|
||||||
|
DEFAULT CHARSET = utf8;
|
||||||
|
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
|
||||||
|
DROP TABLE IF EXISTS `undo_log`;
|
||||||
|
CREATE TABLE `undo_log`
|
||||||
|
(
|
||||||
|
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||||
|
`branch_id` bigint(20) NOT NULL,
|
||||||
|
`xid` varchar(100) NOT NULL,
|
||||||
|
`context` varchar(128) NOT NULL,
|
||||||
|
`rollback_info` longblob NOT NULL,
|
||||||
|
`log_status` int(11) NOT NULL,
|
||||||
|
`log_created` datetime NOT NULL,
|
||||||
|
`log_modified` datetime NOT NULL,
|
||||||
|
`ext` varchar(100) DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
|
||||||
|
) ENGINE = InnoDB
|
||||||
|
AUTO_INCREMENT = 1
|
||||||
|
DEFAULT CHARSET = utf8;
|
||||||
|
|
||||||
|
CREATE DATABASE IF NOT EXISTS `account_db`;
|
||||||
|
USE `account_db`;
|
||||||
|
DROP TABLE IF EXISTS `account_tbl`;
|
||||||
|
CREATE TABLE `account_tbl`
|
||||||
|
(
|
||||||
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`user_id` varchar(255) DEFAULT NULL,
|
||||||
|
`money` int(11) DEFAULT 0,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) ENGINE = InnoDB
|
||||||
|
DEFAULT CHARSET = utf8;
|
||||||
|
INSERT INTO account_tbl (user_id, money)
|
||||||
|
VALUES ('1', 10000);
|
||||||
|
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
|
||||||
|
DROP TABLE IF EXISTS `undo_log`;
|
||||||
|
CREATE TABLE `undo_log`
|
||||||
|
(
|
||||||
|
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||||
|
`branch_id` bigint(20) NOT NULL,
|
||||||
|
`xid` varchar(100) NOT NULL,
|
||||||
|
`context` varchar(128) NOT NULL,
|
||||||
|
`rollback_info` longblob NOT NULL,
|
||||||
|
`log_status` int(11) NOT NULL,
|
||||||
|
`log_created` datetime NOT NULL,
|
||||||
|
`log_modified` datetime NOT NULL,
|
||||||
|
`ext` varchar(100) DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
|
||||||
|
) ENGINE = InnoDB
|
||||||
|
AUTO_INCREMENT = 1
|
||||||
|
DEFAULT CHARSET = utf8;
|
10
pom.xml
10
pom.xml
|
@ -19,6 +19,7 @@
|
||||||
<module>service</module>
|
<module>service</module>
|
||||||
<module>common</module>
|
<module>common</module>
|
||||||
<module>dao</module>
|
<module>dao</module>
|
||||||
|
<module>gateway</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -64,4 +65,13 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@Tag(name = "云服务接口", description = "云服务接口")
|
@Tag(name = "云服务接口", description = "云服务接口")
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/cloud1")
|
||||||
public class CloudController {
|
public class CloudController {
|
||||||
|
|
||||||
@Qualifier("cn.bunny.feign.CloudFeignClient")
|
@Qualifier("cn.bunny.feign.CloudFeignClient")
|
||||||
|
|
|
@ -2,6 +2,7 @@ package cn.bunny.controller;
|
||||||
|
|
||||||
import cn.bunny.feign.CloudFeignClient;
|
import cn.bunny.feign.CloudFeignClient;
|
||||||
import cn.bunny.properties.OrderProperties;
|
import cn.bunny.properties.OrderProperties;
|
||||||
|
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -12,7 +13,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/cloud1")
|
||||||
@Tag(name = "获取配置属性名称", description = "获取配置属性名称")
|
@Tag(name = "获取配置属性名称", description = "获取配置属性名称")
|
||||||
public class PropertiesController {
|
public class PropertiesController {
|
||||||
|
|
||||||
|
@ -24,15 +25,31 @@ public class PropertiesController {
|
||||||
private CloudFeignClient cloudFeignClient;
|
private CloudFeignClient cloudFeignClient;
|
||||||
|
|
||||||
@GetMapping("config")
|
@GetMapping("config")
|
||||||
|
@SentinelResource(value = "getConfig", fallback = "getConfigFallBack")
|
||||||
@Operation(summary = "获取云端配置属性", description = "获取云端配置属性")
|
@Operation(summary = "获取云端配置属性", description = "获取云端配置属性")
|
||||||
public String getConfig() {
|
public String getConfig() {
|
||||||
|
// 也可以使用try进行捕获和处理
|
||||||
|
// try {
|
||||||
|
// SphU.entry("zzz");
|
||||||
|
// } catch (BlockException e) {
|
||||||
|
// throw new RuntimeException(e);
|
||||||
|
// }
|
||||||
return "timeout:" + orderProperties.getTimeout() + ";autoConfirm:" + orderProperties.getConfirm() + ";dbUrl:" + orderProperties.getDbUrl();
|
return "timeout:" + orderProperties.getTimeout() + ";autoConfirm:" + orderProperties.getConfirm() + ";dbUrl:" + orderProperties.getDbUrl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当获取速度过快时会走这个
|
||||||
|
* Throwable 好处还可以处理业务错误也可以走这个
|
||||||
|
*/
|
||||||
|
public String getConfigFallBack(Throwable throwable) {
|
||||||
|
return "获取云端配置属性,操作过快。。。";
|
||||||
|
}
|
||||||
|
|
||||||
@Operation(summary = "远程调用拦截token", description = "远程调用拦截token")
|
@Operation(summary = "远程调用拦截token", description = "远程调用拦截token")
|
||||||
@GetMapping("product")
|
@GetMapping("product")
|
||||||
String getProduct(@RequestParam("id") Long id) {
|
public String getProduct(@RequestParam("id") Long id) {
|
||||||
// TokenRequestInterceptor 会将内容放到请求头中之后在另一个微服务中查看请求头即可
|
// TokenRequestInterceptor 会将内容放到请求头中之后在另一个微服务中查看请求头即可
|
||||||
return cloudFeignClient.getProduct(id);
|
return cloudFeignClient.getProduct(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
@FeignClient(value = "service-cloud2", path = "/api")
|
@FeignClient(value = "service-cloud2", path = "/cloud2")
|
||||||
public interface CloudFeignClient {
|
public interface CloudFeignClient {
|
||||||
|
|
||||||
// 调用时如果请求类型不同需要考虑加上 @RequestParam
|
// 调用时如果请求类型不同需要考虑加上 @RequestParam
|
||||||
|
|
|
@ -35,6 +35,7 @@ spring:
|
||||||
transport:
|
transport:
|
||||||
dashboard: 192.168.3.132:8858
|
dashboard: 192.168.3.132:8858
|
||||||
eager: true # 项目启动就连接
|
eager: true # 项目启动就连接
|
||||||
|
|
||||||
feign:
|
feign:
|
||||||
sentinel:
|
sentinel:
|
||||||
enabled: true
|
enabled: true
|
|
@ -8,6 +8,9 @@ import org.springframework.web.client.RestTemplate;
|
||||||
@Configuration
|
@Configuration
|
||||||
public class CloudConfiguration {
|
public class CloudConfiguration {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加使用负载均衡调用
|
||||||
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
@LoadBalanced
|
@LoadBalanced
|
||||||
public RestTemplate restTemplate() {
|
public RestTemplate restTemplate() {
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
package cn.bunny.config;
|
|
||||||
|
|
||||||
import io.swagger.v3.oas.models.ExternalDocumentation;
|
|
||||||
import io.swagger.v3.oas.models.OpenAPI;
|
|
||||||
import io.swagger.v3.oas.models.info.Contact;
|
|
||||||
import io.swagger.v3.oas.models.info.Info;
|
|
||||||
import io.swagger.v3.oas.models.info.License;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springdoc.core.models.GroupedOpenApi;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@Slf4j
|
|
||||||
public class Knife4jConfig {
|
|
||||||
@Bean
|
|
||||||
public OpenAPI openAPI() {
|
|
||||||
// 作者等信息
|
|
||||||
Contact contact = new Contact().name("Bunny").email("1319900154@qq.com").url("http://z-bunny.cn");
|
|
||||||
// 使用协议
|
|
||||||
License license = new License().name("MIT").url("https://MUT.com");
|
|
||||||
// 相关信息
|
|
||||||
Info info = new Info().title("Bunny-Order").description("权限管理模板").version("v1.0.0").contact(contact).license(license).termsOfService("MIT");
|
|
||||||
|
|
||||||
return new OpenAPI().info(info).externalDocs(new ExternalDocumentation());
|
|
||||||
}
|
|
||||||
|
|
||||||
// 管理员相关分类接口
|
|
||||||
@Bean
|
|
||||||
public GroupedOpenApi groupedOpenAdminApi() {
|
|
||||||
return GroupedOpenApi.builder().group("默认请求接口").pathsToMatch("/api/**").build();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@Tag(name = "云服务接口", description = "云服务接口")
|
@Tag(name = "云服务接口", description = "云服务接口")
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/cloud2")
|
||||||
public class CloudController {
|
public class CloudController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|
|
@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
// 如果公共路径相同需要加上 path = "/api"
|
// 如果公共路径相同需要加上 path = "/api"
|
||||||
@FeignClient(value = "service-cloud1", path = "/api")
|
@FeignClient(value = "service-cloud1", path = "/cloud1")
|
||||||
public interface CloudFeignClient {
|
public interface CloudFeignClient {
|
||||||
|
|
||||||
@GetMapping("feignCloud1")
|
@GetMapping("feignCloud1")
|
||||||
|
|
|
@ -12,6 +12,10 @@
|
||||||
<modules>
|
<modules>
|
||||||
<module>cloud-demo1</module>
|
<module>cloud-demo1</module>
|
||||||
<module>cloud-demo2</module>
|
<module>cloud-demo2</module>
|
||||||
|
<module>seata-account</module>
|
||||||
|
<module>seata-business</module>
|
||||||
|
<module>seata-order</module>
|
||||||
|
<module>seata-storage</module>
|
||||||
</modules>
|
</modules>
|
||||||
<name>service</name>
|
<name>service</name>
|
||||||
<url>https://maven.apache.org</url>
|
<url>https://maven.apache.org</url>
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>cn.bunny</groupId>
|
||||||
|
<artifactId>service</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>seata-account</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>3.0.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.atguigu.account;
|
||||||
|
|
||||||
|
|
||||||
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@MapperScan("com.atguigu.account.mapper")
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SeataAccountMainApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SeataAccountMainApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.atguigu.account.bean;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @TableName account_tbl
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class AccountTbl implements Serializable {
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String userId;
|
||||||
|
|
||||||
|
private Integer money;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.atguigu.account.controller;
|
||||||
|
|
||||||
|
import com.atguigu.account.service.AccountService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class AccountRestController {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
AccountService accountService;
|
||||||
|
/**
|
||||||
|
* 扣减账户余额
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/debit")
|
||||||
|
public String debit(@RequestParam("userId") String userId,
|
||||||
|
@RequestParam("money") int money){
|
||||||
|
accountService.debit(userId, money);
|
||||||
|
return "account debit success";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.atguigu.account.mapper;
|
||||||
|
|
||||||
|
import com.atguigu.account.bean.AccountTbl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lfy
|
||||||
|
* @description 针对表【account_tbl】的数据库操作Mapper
|
||||||
|
* @createDate 2025-01-08 18:32:50
|
||||||
|
* @Entity com.atguigu.account.bean.AccountTbl
|
||||||
|
*/
|
||||||
|
public interface AccountTblMapper {
|
||||||
|
|
||||||
|
int deleteByPrimaryKey(Long id);
|
||||||
|
|
||||||
|
int insert(AccountTbl record);
|
||||||
|
|
||||||
|
int insertSelective(AccountTbl record);
|
||||||
|
|
||||||
|
AccountTbl selectByPrimaryKey(Long id);
|
||||||
|
|
||||||
|
int updateByPrimaryKeySelective(AccountTbl record);
|
||||||
|
|
||||||
|
int updateByPrimaryKey(AccountTbl record);
|
||||||
|
|
||||||
|
void debit(String userId, int money);
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.atguigu.account.service;
|
||||||
|
|
||||||
|
public interface AccountService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从用户账户中扣减
|
||||||
|
* @param userId 用户id
|
||||||
|
* @param money 扣减金额
|
||||||
|
*/
|
||||||
|
void debit(String userId, int money);
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.atguigu.account.service.impl;
|
||||||
|
|
||||||
|
import com.atguigu.account.mapper.AccountTblMapper;
|
||||||
|
import com.atguigu.account.service.AccountService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AccountServiceImpl implements AccountService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
AccountTblMapper accountTblMapper;
|
||||||
|
@Override
|
||||||
|
public void debit(String userId, int money) {
|
||||||
|
// 扣减账户余额
|
||||||
|
accountTblMapper.debit(userId,money);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: seata-account
|
||||||
|
datasource:
|
||||||
|
url: jdbc:mysql://192.168.3.133:3306/account_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
|
||||||
|
username: root
|
||||||
|
password: "123456"
|
||||||
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
server-addr: 192.168.3.132:8848
|
||||||
|
config:
|
||||||
|
import-check:
|
||||||
|
enabled: false
|
||||||
|
server:
|
||||||
|
port: 10000
|
||||||
|
|
||||||
|
mybatis:
|
||||||
|
mapper-locations: classpath:mapper/*.xml
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.atguigu.account.mapper.AccountTblMapper">
|
||||||
|
|
||||||
|
<resultMap id="BaseResultMap" type="com.atguigu.account.bean.AccountTbl">
|
||||||
|
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||||
|
<result property="userId" column="user_id" jdbcType="VARCHAR"/>
|
||||||
|
<result property="money" column="money" jdbcType="INTEGER"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<sql id="Base_Column_List">
|
||||||
|
id,user_id,money
|
||||||
|
</sql>
|
||||||
|
|
||||||
|
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
|
||||||
|
select
|
||||||
|
<include refid="Base_Column_List" />
|
||||||
|
from account_tbl
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
|
||||||
|
delete from account_tbl
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</delete>
|
||||||
|
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.atguigu.account.bean.AccountTbl" useGeneratedKeys="true">
|
||||||
|
insert into account_tbl
|
||||||
|
( id,user_id,money
|
||||||
|
)
|
||||||
|
values (#{id,jdbcType=INTEGER},#{userId,jdbcType=VARCHAR},#{money,jdbcType=INTEGER}
|
||||||
|
)
|
||||||
|
</insert>
|
||||||
|
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.atguigu.account.bean.AccountTbl" useGeneratedKeys="true">
|
||||||
|
insert into account_tbl
|
||||||
|
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||||
|
<if test="id != null">id,</if>
|
||||||
|
<if test="userId != null">user_id,</if>
|
||||||
|
<if test="money != null">money,</if>
|
||||||
|
</trim>
|
||||||
|
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||||
|
<if test="id != null">#{id,jdbcType=INTEGER},</if>
|
||||||
|
<if test="userId != null">#{userId,jdbcType=VARCHAR},</if>
|
||||||
|
<if test="money != null">#{money,jdbcType=INTEGER},</if>
|
||||||
|
</trim>
|
||||||
|
</insert>
|
||||||
|
<update id="updateByPrimaryKeySelective" parameterType="com.atguigu.account.bean.AccountTbl">
|
||||||
|
update account_tbl
|
||||||
|
<set>
|
||||||
|
<if test="userId != null">
|
||||||
|
user_id = #{userId,jdbcType=VARCHAR},
|
||||||
|
</if>
|
||||||
|
<if test="money != null">
|
||||||
|
money = #{money,jdbcType=INTEGER},
|
||||||
|
</if>
|
||||||
|
</set>
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</update>
|
||||||
|
<update id="updateByPrimaryKey" parameterType="com.atguigu.account.bean.AccountTbl">
|
||||||
|
update account_tbl
|
||||||
|
set
|
||||||
|
user_id = #{userId,jdbcType=VARCHAR},
|
||||||
|
money = #{money,jdbcType=INTEGER}
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</update>
|
||||||
|
<update id="debit">
|
||||||
|
update account_tbl
|
||||||
|
set money = money - #{money,jdbcType=INTEGER}
|
||||||
|
where user_id = #{userId,jdbcType=VARCHAR}
|
||||||
|
</update>
|
||||||
|
</mapper>
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>cn.bunny</groupId>
|
||||||
|
<artifactId>service</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>seata-business</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.atguigu.business;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
|
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SeataBusinessMainApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SeataBusinessMainApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.atguigu.business.controller;
|
||||||
|
|
||||||
|
import com.atguigu.business.service.BusinessService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class PurchaseRestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
BusinessService businessService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 购买
|
||||||
|
* @param userId 用户ID
|
||||||
|
* @param commodityCode 商品编码
|
||||||
|
* @param orderCount 数量
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/purchase")
|
||||||
|
public String purchase(@RequestParam("userId") String userId,
|
||||||
|
@RequestParam("commodityCode") String commodityCode,
|
||||||
|
@RequestParam("count") int orderCount){
|
||||||
|
businessService.purchase(userId, commodityCode, orderCount);
|
||||||
|
return "business purchase success";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.atguigu.business.service;
|
||||||
|
|
||||||
|
public interface BusinessService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 采购
|
||||||
|
* @param userId 用户id
|
||||||
|
* @param commodityCode 商品编号
|
||||||
|
* @param orderCount 购买数量
|
||||||
|
*/
|
||||||
|
void purchase(String userId, String commodityCode, int orderCount);
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.atguigu.business.service.impl;
|
||||||
|
|
||||||
|
import com.atguigu.business.service.BusinessService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class BusinessServiceImpl implements BusinessService {
|
||||||
|
@Override
|
||||||
|
public void purchase(String userId, String commodityCode, int orderCount) {
|
||||||
|
//TODO 1. 扣减库存
|
||||||
|
|
||||||
|
//TODO 2. 创建订单
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: seata-business
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
server-addr: 192.168.3.132:8848
|
||||||
|
config:
|
||||||
|
import-check:
|
||||||
|
enabled: false
|
||||||
|
server:
|
||||||
|
port: 11000
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>cn.bunny</groupId>
|
||||||
|
<artifactId>service</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>seata-order</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>3.0.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.atguigu.order;
|
||||||
|
|
||||||
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
|
|
||||||
|
@MapperScan("com.atguigu.order.mapper")
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SeataOrderMainApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SeataOrderMainApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.atguigu.order.bean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @TableName order_tbl
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class OrderTbl implements Serializable {
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String userId;
|
||||||
|
|
||||||
|
private String commodityCode;
|
||||||
|
|
||||||
|
private Integer count;
|
||||||
|
|
||||||
|
private Integer money;
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.atguigu.order.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import com.atguigu.order.bean.OrderTbl;
|
||||||
|
import com.atguigu.order.service.OrderService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class OrderRestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
OrderService orderService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建订单
|
||||||
|
* @param userId
|
||||||
|
* @param commodityCode
|
||||||
|
* @param orderCount
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/create")
|
||||||
|
public String create(@RequestParam("userId") String userId,
|
||||||
|
@RequestParam("commodityCode") String commodityCode,
|
||||||
|
@RequestParam("count") int orderCount)
|
||||||
|
{
|
||||||
|
OrderTbl tbl = orderService.create(userId, commodityCode, orderCount);
|
||||||
|
return "order create success = 订单id:【"+tbl.getId()+"】";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.atguigu.order.mapper;
|
||||||
|
|
||||||
|
import com.atguigu.order.bean.OrderTbl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lfy
|
||||||
|
* @description 针对表【order_tbl】的数据库操作Mapper
|
||||||
|
* @createDate 2025-01-08 18:34:18
|
||||||
|
* @Entity com.atguigu.order.bean.OrderTbl
|
||||||
|
*/
|
||||||
|
public interface OrderTblMapper {
|
||||||
|
|
||||||
|
int deleteByPrimaryKey(Long id);
|
||||||
|
|
||||||
|
int insert(OrderTbl record);
|
||||||
|
|
||||||
|
int insertSelective(OrderTbl record);
|
||||||
|
|
||||||
|
OrderTbl selectByPrimaryKey(Long id);
|
||||||
|
|
||||||
|
int updateByPrimaryKeySelective(OrderTbl record);
|
||||||
|
|
||||||
|
int updateByPrimaryKey(OrderTbl record);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.atguigu.order.service;
|
||||||
|
|
||||||
|
import com.atguigu.order.bean.OrderTbl;
|
||||||
|
|
||||||
|
public interface OrderService {
|
||||||
|
/**
|
||||||
|
* 创建订单
|
||||||
|
* @param userId 用户id
|
||||||
|
* @param commodityCode 商品编码
|
||||||
|
* @param orderCount 商品数量
|
||||||
|
*/
|
||||||
|
OrderTbl create(String userId, String commodityCode, int orderCount);
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.atguigu.order.service.impl;
|
||||||
|
|
||||||
|
import com.atguigu.order.bean.OrderTbl;
|
||||||
|
import com.atguigu.order.mapper.OrderTblMapper;
|
||||||
|
import com.atguigu.order.service.OrderService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class OrderServiceImpl implements OrderService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
OrderTblMapper orderTblMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OrderTbl create(String userId, String commodityCode, int orderCount) {
|
||||||
|
//1、计算订单价格
|
||||||
|
int orderMoney = calculate(commodityCode, orderCount);
|
||||||
|
|
||||||
|
//TODO 2、扣减账户余额
|
||||||
|
|
||||||
|
//3、保存订单
|
||||||
|
OrderTbl orderTbl = new OrderTbl();
|
||||||
|
orderTbl.setUserId(userId);
|
||||||
|
orderTbl.setCommodityCode(commodityCode);
|
||||||
|
orderTbl.setCount(orderCount);
|
||||||
|
orderTbl.setMoney(orderMoney);
|
||||||
|
|
||||||
|
orderTblMapper.insert(orderTbl);
|
||||||
|
|
||||||
|
return orderTbl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算价格
|
||||||
|
private int calculate(String commodityCode, int orderCount) {
|
||||||
|
return 9*orderCount;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: seata-order
|
||||||
|
datasource:
|
||||||
|
url: jdbc:mysql://192.168.3.133:3306/order_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
|
||||||
|
username: root
|
||||||
|
password: 123456
|
||||||
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
server-addr: 192.168.3.132:8848
|
||||||
|
config:
|
||||||
|
import-check:
|
||||||
|
enabled: false
|
||||||
|
|
||||||
|
server:
|
||||||
|
port: 12000
|
||||||
|
mybatis:
|
||||||
|
mapper-locations: classpath:mapper/*.xml
|
|
@ -0,0 +1,83 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.atguigu.order.mapper.OrderTblMapper">
|
||||||
|
|
||||||
|
<resultMap id="BaseResultMap" type="com.atguigu.order.bean.OrderTbl">
|
||||||
|
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||||
|
<result property="userId" column="user_id" jdbcType="VARCHAR"/>
|
||||||
|
<result property="commodityCode" column="commodity_code" jdbcType="VARCHAR"/>
|
||||||
|
<result property="count" column="count" jdbcType="INTEGER"/>
|
||||||
|
<result property="money" column="money" jdbcType="INTEGER"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<sql id="Base_Column_List">
|
||||||
|
id,user_id,commodity_code,
|
||||||
|
count,money
|
||||||
|
</sql>
|
||||||
|
|
||||||
|
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
|
||||||
|
select
|
||||||
|
<include refid="Base_Column_List" />
|
||||||
|
from order_tbl
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
|
||||||
|
delete from order_tbl
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</delete>
|
||||||
|
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.atguigu.order.bean.OrderTbl"
|
||||||
|
useGeneratedKeys="true">
|
||||||
|
insert into order_tbl
|
||||||
|
( id,user_id,commodity_code
|
||||||
|
,count,money)
|
||||||
|
values (#{id,jdbcType=INTEGER},#{userId,jdbcType=VARCHAR},#{commodityCode,jdbcType=VARCHAR}
|
||||||
|
,#{count,jdbcType=INTEGER},#{money,jdbcType=INTEGER})
|
||||||
|
</insert>
|
||||||
|
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.atguigu.order.bean.OrderTbl" useGeneratedKeys="true">
|
||||||
|
insert into order_tbl
|
||||||
|
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||||
|
<if test="id != null">id,</if>
|
||||||
|
<if test="userId != null">user_id,</if>
|
||||||
|
<if test="commodityCode != null">commodity_code,</if>
|
||||||
|
<if test="count != null">count,</if>
|
||||||
|
<if test="money != null">money,</if>
|
||||||
|
</trim>
|
||||||
|
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||||
|
<if test="id != null">#{id,jdbcType=INTEGER},</if>
|
||||||
|
<if test="userId != null">#{userId,jdbcType=VARCHAR},</if>
|
||||||
|
<if test="commodityCode != null">#{commodityCode,jdbcType=VARCHAR},</if>
|
||||||
|
<if test="count != null">#{count,jdbcType=INTEGER},</if>
|
||||||
|
<if test="money != null">#{money,jdbcType=INTEGER},</if>
|
||||||
|
</trim>
|
||||||
|
</insert>
|
||||||
|
<update id="updateByPrimaryKeySelective" parameterType="com.atguigu.order.bean.OrderTbl">
|
||||||
|
update order_tbl
|
||||||
|
<set>
|
||||||
|
<if test="userId != null">
|
||||||
|
user_id = #{userId,jdbcType=VARCHAR},
|
||||||
|
</if>
|
||||||
|
<if test="commodityCode != null">
|
||||||
|
commodity_code = #{commodityCode,jdbcType=VARCHAR},
|
||||||
|
</if>
|
||||||
|
<if test="count != null">
|
||||||
|
count = #{count,jdbcType=INTEGER},
|
||||||
|
</if>
|
||||||
|
<if test="money != null">
|
||||||
|
money = #{money,jdbcType=INTEGER},
|
||||||
|
</if>
|
||||||
|
</set>
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</update>
|
||||||
|
<update id="updateByPrimaryKey" parameterType="com.atguigu.order.bean.OrderTbl">
|
||||||
|
update order_tbl
|
||||||
|
set
|
||||||
|
user_id = #{userId,jdbcType=VARCHAR},
|
||||||
|
commodity_code = #{commodityCode,jdbcType=VARCHAR},
|
||||||
|
count = #{count,jdbcType=INTEGER},
|
||||||
|
money = #{money,jdbcType=INTEGER}
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</update>
|
||||||
|
</mapper>
|
|
@ -0,0 +1,49 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>cn.bunny</groupId>
|
||||||
|
<artifactId>service</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>seata-storage</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>3.0.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.atguigu.storage;
|
||||||
|
|
||||||
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
|
||||||
|
@MapperScan("com.atguigu.storage.mapper")
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SeataStorageMainApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SeataStorageMainApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.atguigu.storage.bean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @TableName storage_tbl
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class StorageTbl implements Serializable {
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String commodityCode;
|
||||||
|
|
||||||
|
private Integer count;
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.atguigu.storage.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import com.atguigu.storage.service.StorageService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class StorageRestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
StorageService storageService;
|
||||||
|
|
||||||
|
@GetMapping("/deduct")
|
||||||
|
public String deduct(@RequestParam("commodityCode") String commodityCode,
|
||||||
|
@RequestParam("count") Integer count) {
|
||||||
|
|
||||||
|
storageService.deduct(commodityCode, count);
|
||||||
|
return "storage deduct success";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.atguigu.storage.mapper;
|
||||||
|
|
||||||
|
import com.atguigu.storage.bean.StorageTbl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lfy
|
||||||
|
* @description 针对表【storage_tbl】的数据库操作Mapper
|
||||||
|
* @createDate 2025-01-08 18:35:07
|
||||||
|
* @Entity com.atguigu.storage.bean.StorageTbl
|
||||||
|
*/
|
||||||
|
public interface StorageTblMapper {
|
||||||
|
|
||||||
|
int deleteByPrimaryKey(Long id);
|
||||||
|
|
||||||
|
int insert(StorageTbl record);
|
||||||
|
|
||||||
|
int insertSelective(StorageTbl record);
|
||||||
|
|
||||||
|
StorageTbl selectByPrimaryKey(Long id);
|
||||||
|
|
||||||
|
int updateByPrimaryKeySelective(StorageTbl record);
|
||||||
|
|
||||||
|
int updateByPrimaryKey(StorageTbl record);
|
||||||
|
|
||||||
|
void deduct(String commodityCode, int count);
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.atguigu.storage.service;
|
||||||
|
|
||||||
|
public interface StorageService {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扣除存储数量
|
||||||
|
* @param commodityCode 商品编码
|
||||||
|
* @param count 数量
|
||||||
|
*/
|
||||||
|
void deduct(String commodityCode, int count);
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.atguigu.storage.service.impl;
|
||||||
|
|
||||||
|
import com.atguigu.storage.mapper.StorageTblMapper;
|
||||||
|
import com.atguigu.storage.service.StorageService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class StorageServiceImpl implements StorageService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
StorageTblMapper storageTblMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deduct(String commodityCode, int count) {
|
||||||
|
storageTblMapper.deduct(commodityCode, count);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: seata-storage
|
||||||
|
datasource:
|
||||||
|
url: jdbc:mysql://192.168.3.133:3306/storage_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
|
||||||
|
username: root
|
||||||
|
password: 123456
|
||||||
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
server-addr: 192.168.3.132:8848
|
||||||
|
config:
|
||||||
|
import-check:
|
||||||
|
enabled: false
|
||||||
|
server:
|
||||||
|
port: 13000
|
||||||
|
|
||||||
|
mybatis:
|
||||||
|
mapper-locations: classpath:mapper/*.xml
|
|
@ -0,0 +1,72 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.atguigu.storage.mapper.StorageTblMapper">
|
||||||
|
|
||||||
|
<resultMap id="BaseResultMap" type="com.atguigu.storage.bean.StorageTbl">
|
||||||
|
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||||
|
<result property="commodityCode" column="commodity_code" jdbcType="VARCHAR"/>
|
||||||
|
<result property="count" column="count" jdbcType="INTEGER"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<sql id="Base_Column_List">
|
||||||
|
id,commodity_code,count
|
||||||
|
</sql>
|
||||||
|
|
||||||
|
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
|
||||||
|
select
|
||||||
|
<include refid="Base_Column_List" />
|
||||||
|
from storage_tbl
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
|
||||||
|
delete from storage_tbl
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</delete>
|
||||||
|
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.atguigu.storage.bean.StorageTbl" useGeneratedKeys="true">
|
||||||
|
insert into storage_tbl
|
||||||
|
( id,commodity_code,count
|
||||||
|
)
|
||||||
|
values (#{id,jdbcType=INTEGER},#{commodityCode,jdbcType=VARCHAR},#{count,jdbcType=INTEGER}
|
||||||
|
)
|
||||||
|
</insert>
|
||||||
|
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.atguigu.storage.bean.StorageTbl" useGeneratedKeys="true">
|
||||||
|
insert into storage_tbl
|
||||||
|
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||||
|
<if test="id != null">id,</if>
|
||||||
|
<if test="commodityCode != null">commodity_code,</if>
|
||||||
|
<if test="count != null">count,</if>
|
||||||
|
</trim>
|
||||||
|
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||||
|
<if test="id != null">#{id,jdbcType=INTEGER},</if>
|
||||||
|
<if test="commodityCode != null">#{commodityCode,jdbcType=VARCHAR},</if>
|
||||||
|
<if test="count != null">#{count,jdbcType=INTEGER},</if>
|
||||||
|
</trim>
|
||||||
|
</insert>
|
||||||
|
<update id="updateByPrimaryKeySelective" parameterType="com.atguigu.storage.bean.StorageTbl">
|
||||||
|
update storage_tbl
|
||||||
|
<set>
|
||||||
|
<if test="commodityCode != null">
|
||||||
|
commodity_code = #{commodityCode,jdbcType=VARCHAR},
|
||||||
|
</if>
|
||||||
|
<if test="count != null">
|
||||||
|
count = #{count,jdbcType=INTEGER},
|
||||||
|
</if>
|
||||||
|
</set>
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</update>
|
||||||
|
<update id="updateByPrimaryKey" parameterType="com.atguigu.storage.bean.StorageTbl">
|
||||||
|
update storage_tbl
|
||||||
|
set
|
||||||
|
commodity_code = #{commodityCode,jdbcType=VARCHAR},
|
||||||
|
count = #{count,jdbcType=INTEGER}
|
||||||
|
where id = #{id,jdbcType=INTEGER}
|
||||||
|
</update>
|
||||||
|
<update id="deduct">
|
||||||
|
update storage_tbl
|
||||||
|
set count = count - #{count}
|
||||||
|
where commodity_code = #{commodityCode}
|
||||||
|
</update>
|
||||||
|
</mapper>
|
Loading…
Reference in New Issue