commit e02fa342050fe78f123fa91009a3b0c6e2052708 Author: DongZeLiang <2746733890@qq.com> Date: Thu May 23 18:02:32 2024 +0800 feat(): 初始化项目 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8648a78 --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +/.idea + + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store diff --git a/cloud-common/pom.xml b/cloud-common/pom.xml new file mode 100644 index 0000000..74fd4b0 --- /dev/null +++ b/cloud-common/pom.xml @@ -0,0 +1,109 @@ + + + 4.0.0 + + com.muyu + cloud + 1.0.0 + + + cloud-common + + + 8 + 8 + UTF-8 + + + + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + io.jsonwebtoken + jjwt + 0.9.1 + + + + com.alibaba + fastjson + 1.2.80 + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + com.alibaba + druid-spring-boot-starter + 1.2.8 + + + + com.mysql + mysql-connector-j + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.4.1 + + + + org.springframework.boot + spring-boot-starter-validation + + + + org.apache.commons + commons-lang3 + + + + org.projectlombok + lombok + + + + + diff --git a/cloud-common/src/main/java/com/muyu/common/constant/Constants.java b/cloud-common/src/main/java/com/muyu/common/constant/Constants.java new file mode 100644 index 0000000..c88c620 --- /dev/null +++ b/cloud-common/src/main/java/com/muyu/common/constant/Constants.java @@ -0,0 +1,18 @@ +package com.muyu.common.constant; + +/** + * @description: 系统常量 + * @author DongZl + */ +public class Constants { + /** + * 成功标记 + */ + public static final Integer SUCCESS = 200; + public static final String SUCCESS_MSG = "操作成功"; + /** + * 失败标记 + */ + public static final Integer ERROR = 500; + public static final String ERROR_MSG = "操作异常"; +} diff --git a/cloud-common/src/main/java/com/muyu/common/constant/JwtConstants.java b/cloud-common/src/main/java/com/muyu/common/constant/JwtConstants.java new file mode 100644 index 0000000..28226ca --- /dev/null +++ b/cloud-common/src/main/java/com/muyu/common/constant/JwtConstants.java @@ -0,0 +1,27 @@ +package com.muyu.common.constant; + +/** + * @author DongZl + * @description: Jwt常量 + */ +public class JwtConstants { + /** + * 用户ID字段 + */ + public static final String DETAILS_USER_ID = "user_id"; + + /** + * 用户名字段 + */ + public static final String DETAILS_USERNAME = "username"; + + /** + * 用户标识 + */ + public static final String USER_KEY = "user_key"; + + /** + * 令牌秘钥 + */ + public final static String SECRET = "abcdefghijklmnopqrstuvwxyz"; +} diff --git a/cloud-common/src/main/java/com/muyu/common/constant/TokenConstants.java b/cloud-common/src/main/java/com/muyu/common/constant/TokenConstants.java new file mode 100644 index 0000000..3ab1a45 --- /dev/null +++ b/cloud-common/src/main/java/com/muyu/common/constant/TokenConstants.java @@ -0,0 +1,24 @@ +package com.muyu.common.constant; + +/** + * @author DongZl + * @description: 令牌常量 + */ +public class TokenConstants { + /** + * 缓存有效期,默认720(分钟) + */ + public final static long EXPIRATION = 720; + /** + * 缓存刷新时间,默认120(分钟) + */ + public final static long REFRESH_TIME = 120; + /** + * 权限缓存前缀 + */ + public final static String LOGIN_TOKEN_KEY = "login_tokens:"; + /** + * token标识 + */ + public static final String TOKEN = "token"; +} diff --git a/cloud-common/src/main/java/com/muyu/common/result/PageResult.java b/cloud-common/src/main/java/com/muyu/common/result/PageResult.java new file mode 100644 index 0000000..b2cf555 --- /dev/null +++ b/cloud-common/src/main/java/com/muyu/common/result/PageResult.java @@ -0,0 +1,34 @@ +package com.muyu.common.result; + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * @author DongZl + * @description: 列表返回结果集 + */ +@Data +public class PageResult implements Serializable { + /** + * 总条数 + */ + private long total; + /** + * 结果集合 + */ + private List list; + public PageResult() { + } + public PageResult(long total, List list) { + this.total = total; + this.list = list; + } + public static PageResult toPageResult(long total, List list){ + return new PageResult(total , list); + } + public static Result> toResult(long total, List list){ + return Result.success(PageResult.toPageResult(total,list)); + } +} diff --git a/cloud-common/src/main/java/com/muyu/common/result/Result.java b/cloud-common/src/main/java/com/muyu/common/result/Result.java new file mode 100644 index 0000000..1076493 --- /dev/null +++ b/cloud-common/src/main/java/com/muyu/common/result/Result.java @@ -0,0 +1,53 @@ +package com.muyu.common.result; + +import com.muyu.common.constant.Constants; +import lombok.Data; + +import java.io.Serializable; + +/** + * @description: 响应信息主体 + * @author DongZl + */ +@Data +public class Result implements Serializable { + private static final long serialVersionUID = 1L; + /** 成功 */ + public static final int SUCCESS = Constants.SUCCESS; + /** 失败 */ + public static final int FAIL = Constants.ERROR; + private int code; + private String msg; + private T data; + public static Result success() { + return restResult(null, SUCCESS, Constants.SUCCESS_MSG); + } + public static Result success(T data) { + return restResult(data, SUCCESS, Constants.SUCCESS_MSG); + } + public static Result success(T data, String msg) { + return restResult(data, SUCCESS, msg); + } + public static Result error() { + return restResult(null, FAIL, Constants.ERROR_MSG); + } + public static Result error(String msg) { + return restResult(null, FAIL, msg); + } + public static Result error(T data) { + return restResult(data, FAIL, Constants.ERROR_MSG); + } + public static Result error(T data, String msg) { + return restResult(data, FAIL, msg); + } + public static Result error(int code, String msg) { + return restResult(null, code, msg); + } + private static Result restResult(T data, int code, String msg) { + Result apiResult = new Result<>(); + apiResult.setCode(code); + apiResult.setData(data); + apiResult.setMsg(msg); + return apiResult; + } +} diff --git a/cloud-common/src/main/java/com/muyu/common/utils/JwtUtils.java b/cloud-common/src/main/java/com/muyu/common/utils/JwtUtils.java new file mode 100644 index 0000000..b91af0e --- /dev/null +++ b/cloud-common/src/main/java/com/muyu/common/utils/JwtUtils.java @@ -0,0 +1,104 @@ +package com.muyu.common.utils; + +import com.muyu.common.constant.JwtConstants; +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; + +import java.util.Map; + +/** + * @description: Jwt工具类 + * @author DongZl + */ +public class JwtUtils { + + public static String secret = JwtConstants.SECRET; + /** + * 从数据声明生成令牌 + * + * @param claims 数据声明 + * @return 令牌 + */ + public static String createToken(Map claims){ + String token = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact(); + return token; + } + /** + * 从令牌中获取数据声明 + * + * @param token 令牌 + * @return 数据声明 + */ + public static Claims parseToken(String token){ + return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody(); + } + /** + * 根据令牌获取用户标识 + * + * @param token 令牌 + * @return 用户ID + */ + public static String getUserKey(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.USER_KEY); + } + /** + * 根据令牌获取用户标识 + * + * @param claims 身份信息 + * @return 用户ID + */ + public static String getUserKey(Claims claims){ + return getValue(claims, JwtConstants.USER_KEY); + } + /** + * 根据令牌获取用户ID + * + * @param token 令牌 + * @return 用户ID + */ + public static String getUserId(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.DETAILS_USER_ID); + } + /** + * 根据身份信息获取用户ID + * + * @param claims 身份信息 + * @return 用户ID + */ + public static String getUserId(Claims claims){ + return getValue(claims, JwtConstants.DETAILS_USER_ID); + } + /** + * 根据令牌获取用户名 + * + * @param token 令牌 + * @return 用户名 + */ + public static String getUserName(String token){ + Claims claims = parseToken(token); + return getValue(claims, JwtConstants.DETAILS_USERNAME); + } + /** + * 根据身份信息获取用户名 + * + * @param claims 身份信息 + * @return 用户名 + */ + public static String getUserName(Claims claims){ + return getValue(claims, JwtConstants.DETAILS_USERNAME); + } + /** + * 根据身份信息获取键值 + * + * @param claims 身份信息 + * @param key 键 + * @return 值 + */ + public static String getValue(Claims claims, String key){ + Object obj = claims.get(key); + return obj == null ? "" : obj.toString(); + } +} diff --git a/cloud-common/src/main/java/com/muyu/common/utils/StringUtils.java b/cloud-common/src/main/java/com/muyu/common/utils/StringUtils.java new file mode 100644 index 0000000..c806d19 --- /dev/null +++ b/cloud-common/src/main/java/com/muyu/common/utils/StringUtils.java @@ -0,0 +1,68 @@ +package com.muyu.common.utils; + +import org.springframework.util.AntPathMatcher; + +import java.util.Collection; +import java.util.List; + +/** + * @author DongZl + * @description: 字符串处理工具类 + */ +public class StringUtils extends org.apache.commons.lang3.StringUtils { + + /** + * * 判断一个对象是否为空 + * + * @param object Object + * @return true:为空 false:非空 + */ + public static boolean isNull(Object object) { + return object == null; + } + + /** + * * 判断一个Collection是否为空, 包含List,Set,Queue + * + * @param coll 要判断的Collection + * @return true:为空 false:非空 + */ + public static boolean isEmpty(Collection coll) { + return isNull(coll) || coll.isEmpty(); + } + + /** + * 查找指定字符串是否匹配指定字符串列表中的任意一个字符串 + * + * @param str 指定字符串 + * @param strs 需要检查的字符串数组 + * @return 是否匹配 + */ + public static boolean matches(String str, List strs) { + if (isEmpty(str) || isEmpty(strs)) { + return false; + } + for (String pattern : strs) { + if (isMatch(pattern, str)) + { + return true; + } + } + return false; + } + + /** + * 判断url是否与规则配置: + * ? 表示单个字符; + * * 表示一层路径内的任意字符串,不可跨层级; + * ** 表示任意层路径; + * + * @param pattern 匹配规则 + * @param url 需要匹配的url + * @return + */ + public static boolean isMatch(String pattern, String url) { + AntPathMatcher matcher = new AntPathMatcher(); + return matcher.match(pattern, url); + } +} diff --git a/cloud-gateway/pom.xml b/cloud-gateway/pom.xml new file mode 100644 index 0000000..c4935e4 --- /dev/null +++ b/cloud-gateway/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + com.muyu + cloud + 1.0.0 + + + cloud-gateway + + + 8 + 8 + UTF-8 + + + + + + com.muyu + cloud-common + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + com.alibaba.cloud + spring-cloud-alibaba-sentinel-gateway + + + + com.alibaba.csp + sentinel-spring-cloud-gateway-adapter + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + diff --git a/cloud-gateway/src/main/java/com/muyu/CloudGatewayApplication.java b/cloud-gateway/src/main/java/com/muyu/CloudGatewayApplication.java new file mode 100644 index 0000000..1870f08 --- /dev/null +++ b/cloud-gateway/src/main/java/com/muyu/CloudGatewayApplication.java @@ -0,0 +1,20 @@ +package com.muyu; + +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; + +/** + * @Author: DongZeLiang + * @date: 2024/5/23 + * @Description: 云网关启动类 + * @Version: 1.0 + */ +@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class}) +public class CloudGatewayApplication { + + public static void main (String[] args) { + SpringApplication.run(CloudGatewayApplication.class, args); + } +} diff --git a/cloud-gateway/src/main/java/com/muyu/config/GatewaySentinelConfig.java b/cloud-gateway/src/main/java/com/muyu/config/GatewaySentinelConfig.java new file mode 100644 index 0000000..79176f2 --- /dev/null +++ b/cloud-gateway/src/main/java/com/muyu/config/GatewaySentinelConfig.java @@ -0,0 +1,71 @@ +package com.muyu.config; + +import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule; +import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager; +import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.http.codec.ServerCodecConfigurer; +import org.springframework.web.reactive.result.view.ViewResolver; + +import javax.annotation.PostConstruct; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @deprecation: 网关限流控件 + * @author DongZl + */ +@Configuration +public class GatewaySentinelConfig { + /** + * 查看解析器 + */ + private final List viewResolvers; + /** + * 服务器编解码器配置 + */ + private final ServerCodecConfigurer serverCodecConfigurer; + public GatewaySentinelConfig(ObjectProvider> viewResolversProvider, + ServerCodecConfigurer serverCodecConfigurer) { + this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList); + this.serverCodecConfigurer = serverCodecConfigurer; + } + /** + * Sentinel 网关块异常处理程序 + * @return + */ + @Bean + @Order(Ordered.HIGHEST_PRECEDENCE) + public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() { + // 给 Spring Cloud Gateway 注册块异常处理程序。 + return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer); + } + + /** + * 初始化网关配置 + */ + @PostConstruct + public void doInit() { + initGatewayRules(); + } + /** + * 配置限流规则 + */ + private void initGatewayRules() { + Set rules = new HashSet<>(); + rules.add(new GatewayFlowRule("cloud-user") + // 限流阈值 + .setCount(1) + // 统计时间窗口,单位是秒,默认是 1 秒 + .setIntervalSec(5) + ); + //添加到限流规则当中 + GatewayRuleManager.loadRules(rules); + } +} diff --git a/cloud-gateway/src/main/java/com/muyu/config/IgnoreWhiteConfig.java b/cloud-gateway/src/main/java/com/muyu/config/IgnoreWhiteConfig.java new file mode 100644 index 0000000..216d521 --- /dev/null +++ b/cloud-gateway/src/main/java/com/muyu/config/IgnoreWhiteConfig.java @@ -0,0 +1,31 @@ +package com.muyu.config; + +import com.alibaba.fastjson.JSONObject; +import lombok.Data; +import lombok.extern.log4j.Log4j2; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +/** + * @description: 放行白名单配置 + * @author DongZl + */ +@Configuration +@RefreshScope +@ConfigurationProperties(prefix = "ignore") +@Data +@Log4j2 +public class IgnoreWhiteConfig { + /** + * 放行白名单配置,网关不校验此处的白名单 + */ + private List whites = new ArrayList<>(); + public void setWhites(List whites) { + log.info("加载网关路径白名单:{}", JSONObject.toJSONString(whites)); + this.whites = whites; + } +} diff --git a/cloud-gateway/src/main/java/com/muyu/utils/GatewayUtils.java b/cloud-gateway/src/main/java/com/muyu/utils/GatewayUtils.java new file mode 100644 index 0000000..dcbe7d8 --- /dev/null +++ b/cloud-gateway/src/main/java/com/muyu/utils/GatewayUtils.java @@ -0,0 +1,73 @@ +package com.muyu.utils; + +import com.alibaba.fastjson.JSONObject; +import com.muyu.common.result.Result; +import com.muyu.common.utils.StringUtils; +import lombok.extern.log4j.Log4j2; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +/** + * @author DongZl + * @description: 网关处理工具类 + */ +@Log4j2 +public class GatewayUtils { + /** + * 添加请求头参数 + * @param mutate 修改对象 + * @param key 键 + * @param value 值 + */ + public static void addHeader(ServerHttpRequest.Builder mutate, String key, Object value) { + if (StringUtils.isEmpty(key)){ + log.warn("添加请求头参数键不可以为空"); + return; + } + if (value == null) { + log.warn("添加请求头参数:[{}]值为空",key); + return; + } + String valueStr = value.toString(); + mutate.header(key, valueStr); + log.info("添加请求头参数成功 - 键:[{}] , 值:[{}]", key , value); + } + /** + * 删除请求头参数 + * @param mutate 修改对象 + * @param key 键 + */ + public static void removeHeader(ServerHttpRequest.Builder mutate, String key) { + if (StringUtils.isEmpty(key)){ + log.warn("删除请求头参数键不可以为空"); + return; + } + mutate.headers(httpHeaders -> httpHeaders.remove(key)).build(); + log.info("删除请求头参数 - 键:[{}]",key); + } + /** + * 错误结果响应 + * @param exchange 响应上下文 + * @param msg 响应消息 + * @return + */ + public static Mono errorResponse(ServerWebExchange exchange, String msg) { + ServerHttpResponse response = exchange.getResponse(); + //设置HTTP响应头状态 + response.setStatusCode(HttpStatus.OK); + //设置HTTP响应头文本格式 + response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json"); + //定义响应内容 + Result result = Result.error(msg); + String resultJson = JSONObject.toJSONString(result); + log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson); + DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes()); + //进行响应 + return response.writeWith(Mono.just(dataBuffer)); + } + } diff --git a/cloud-gateway/src/main/resources/bootstrap.yml b/cloud-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..79174f6 --- /dev/null +++ b/cloud-gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,31 @@ +# 指定项目启动的端口号 +server: + port: 8080 + +# 配置nacos的地址 +nacos: + server-addr: 47.92.86.60:8848 + +spring: + application: + # 配置项目的名称 + name: cloud-gateway + profiles: + # 指定项目启动使用的环境配置文件 + active: dev + cloud: + nacos: + discovery: + # nacos 注册中心的地址 + server-addr: ${nacos.server-addr} + config: + # nacos 配置中心的地址 + server-addr: ${nacos.server-addr} + # nacos 配置中心使用的文件后缀(格式) + file-extension: yml + # nacos 配置中心的共享配置文件 + shared-configs: + # 程序公共配置文件 + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + # 程序Redis公共配置文件 + - application-redis-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/modules/cloud-system/pom.xml b/modules/cloud-system/pom.xml new file mode 100644 index 0000000..b6ed763 --- /dev/null +++ b/modules/cloud-system/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + com.muyu + modules + 1.0.0 + + + cloud-system + pom + + system-common + system-remote + system-server + + + + 8 + 8 + UTF-8 + + + diff --git a/modules/cloud-system/system-common/pom.xml b/modules/cloud-system/system-common/pom.xml new file mode 100644 index 0000000..6d35f31 --- /dev/null +++ b/modules/cloud-system/system-common/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + com.muyu + cloud-system + 1.0.0 + + + system-common + + + 8 + 8 + UTF-8 + + + + + + com.muyu + cloud-common + + + + diff --git a/modules/cloud-system/system-common/src/main/java/com/muyu/cloud/system/domain/BookInfo.java b/modules/cloud-system/system-common/src/main/java/com/muyu/cloud/system/domain/BookInfo.java new file mode 100644 index 0000000..4c73570 --- /dev/null +++ b/modules/cloud-system/system-common/src/main/java/com/muyu/cloud/system/domain/BookInfo.java @@ -0,0 +1,48 @@ +package com.muyu.cloud.system.domain; + +import com.muyu.cloud.system.domain.request.BookInfoSaveReq; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.math.BigDecimal; + +/** + * @Author: DongZeLiang + * @date: 2024/5/23 + * @Description: 书籍信息 + * @Version: 1.0 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class BookInfo { + + /** + * 主键 + */ + private Integer id; + /** + * 书籍名称 + */ + private String name; + /** + * 书籍作者 + */ + private String author; + /** + * 价格 + */ + private BigDecimal price; + + + public static BookInfo saveReqBuild(BookInfoSaveReq bookInfoSaveReq){ + return BookInfo.builder() + .name(bookInfoSaveReq.getName()) + .author(bookInfoSaveReq.getAuthor()) + .price(bookInfoSaveReq.getPrice()) + .build(); + } +} diff --git a/modules/cloud-system/system-common/src/main/java/com/muyu/cloud/system/domain/request/BookInfoSaveReq.java b/modules/cloud-system/system-common/src/main/java/com/muyu/cloud/system/domain/request/BookInfoSaveReq.java new file mode 100644 index 0000000..783ebf2 --- /dev/null +++ b/modules/cloud-system/system-common/src/main/java/com/muyu/cloud/system/domain/request/BookInfoSaveReq.java @@ -0,0 +1,39 @@ +package com.muyu.cloud.system.domain.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotBlank; +import java.math.BigDecimal; + +/** + * @Author: DongZeLiang + * @date: 2024/5/23 + * @Description: 书籍添加请求对象 + * @Version: 1.0 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class BookInfoSaveReq { + + /** + * 书籍名称 + */ + @NotBlank(message = "书籍名称不可为空") + private String name; + /** + * 书籍作者 + */ + @NotBlank(message = "作者名称不可为空") + private String author; + /** + * 价格 + */ + @Min(value = 0L, message = "价格最小不可小于0元") + private BigDecimal price; +} diff --git a/modules/cloud-system/system-remote/pom.xml b/modules/cloud-system/system-remote/pom.xml new file mode 100644 index 0000000..1c245a9 --- /dev/null +++ b/modules/cloud-system/system-remote/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + com.muyu + cloud-system + 1.0.0 + + + system-remote + + + 8 + 8 + UTF-8 + + + + + + com.muyu + system-common + + + diff --git a/modules/cloud-system/system-server/pom.xml b/modules/cloud-system/system-server/pom.xml new file mode 100644 index 0000000..2b67d61 --- /dev/null +++ b/modules/cloud-system/system-server/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + com.muyu + cloud-system + 1.0.0 + + + system-server + + + 8 + 8 + UTF-8 + + + + + + com.muyu + system-common + + + + org.springframework.boot + spring-boot-starter-web + + + diff --git a/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/CloudSystemApplication.java b/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/CloudSystemApplication.java new file mode 100644 index 0000000..f6604d5 --- /dev/null +++ b/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/CloudSystemApplication.java @@ -0,0 +1,20 @@ +package com.muyu.cloud.system; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; + +/** + * @Author: DongZeLiang + * @date: 2024/5/23 + * @Description: 云系统服务启动类 + * @Version: 1.0 + */ +@SpringBootApplication +@EnableFeignClients( basePackages = {"com.muyu.**"}) +public class CloudSystemApplication { + + public static void main(String[] args) { + SpringApplication.run(CloudSystemApplication.class, args); + } +} diff --git a/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/controller/BookInfoController.java b/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/controller/BookInfoController.java new file mode 100644 index 0000000..d226fea --- /dev/null +++ b/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/controller/BookInfoController.java @@ -0,0 +1,91 @@ +package com.muyu.cloud.system.controller; + +import com.muyu.cloud.system.domain.BookInfo; +import com.muyu.cloud.system.domain.request.BookInfoSaveReq; +import com.muyu.cloud.system.service.BookInfoService; +import com.muyu.common.result.Result; +import lombok.AllArgsConstructor; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * @Author: DongZeLiang + * @date: 2024/5/23 + * @Description: 书籍控制层 + * @Version: 1.0 + */ +@RestController +@AllArgsConstructor +@RequestMapping("/book-info") +public class BookInfoController { + + /** + * 书籍业务层 + */ + private final BookInfoService bookInfoService; + + /** + * 添加数据 + * @param bookInfoSaveReq 数据对象 + */ + @PostMapping + public Result save (@RequestBody @Validated BookInfoSaveReq bookInfoSaveReq) { + bookInfoService.save(BookInfo.saveReqBuild(bookInfoSaveReq)); + return Result.success(); + } + + /** + * 通过书籍ID查询书籍信息 + * + * @param bookId 书籍ID + * + * @return 数据信息 + */ + @GetMapping("/{bookId}") + public Result selectByBookId (@PathVariable("bookId") Integer bookId) { + BookInfo bookInfo = bookInfoService.selectByBookId(bookId); + return Result.success(bookInfo); + } + + /** + * 通过书籍信息查询集合 + * + * @param bookInfo 数据信息 + * + * @return 符合集合内容 + */ + @PostMapping("/list") + public Result> list (@RequestBody BookInfo bookInfo) { + List bookInfoList = bookInfoService.list(bookInfo); + return Result.success(bookInfoList); + } + + /** + * 通过ID修改书籍信息 + * + * @param bookInfo 数据信息 + * + * @return 修改条数 + */ + @PutMapping() + public Result updateByBookId (BookInfo bookInfo) { + bookInfoService.updateByBookId(bookInfo); + return Result.success(); + } + + /** + * 通过书籍ID删除书籍信息 + * + * @param bookId 书籍ID + * + * @return 返回删除条数 + */ + @DeleteMapping("/{bookId}") + public Result deleteByBookId (@PathVariable("bookId") Integer bookId) { + bookInfoService.deleteByBookId(bookId); + return Result.success(); + } + +} diff --git a/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/mapper/BookInfoMapper.java b/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/mapper/BookInfoMapper.java new file mode 100644 index 0000000..84fb710 --- /dev/null +++ b/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/mapper/BookInfoMapper.java @@ -0,0 +1,53 @@ +package com.muyu.cloud.system.mapper; + +import com.muyu.cloud.system.domain.BookInfo; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * @Author: DongZeLiang + * @date: 2024/5/23 + * @Description: 书籍持久层 + * @Version: 1.0 + */ +@Mapper +public interface BookInfoMapper { + + /** + * 添加数据 + * @param bookInfo 数据对象 + * @return 返回添加的自增ID + */ + int insert(BookInfo bookInfo); + + + /** + * 通过书籍ID查询书籍信息 + * @param bookId 书籍ID + * @return 数据信息 + */ + BookInfo selectByBookId(@Param("bookId") Integer bookId); + + /** + * 通过书籍信息查询集合 + * @param bookInfo 数据信息 + * @return 符合集合内容 + */ + List list(BookInfo bookInfo); + + /** + * 通过ID修改书籍信息 + * @param bookInfo 数据信息 + * @return 修改条数 + */ + int updateByBookId(BookInfo bookInfo); + + /** + * 通过书籍ID删除书籍信息 + * @param bookId 书籍ID + * @return 返回删除条数 + */ + int deleteByBookId(@Param("bookId") Integer bookId); +} diff --git a/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/service/BookInfoService.java b/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/service/BookInfoService.java new file mode 100644 index 0000000..3807777 --- /dev/null +++ b/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/service/BookInfoService.java @@ -0,0 +1,50 @@ +package com.muyu.cloud.system.service; + +import com.muyu.cloud.system.domain.BookInfo; + +import java.util.List; + +/** + * @Author: DongZeLiang + * @date: 2024/5/23 + * @Description: 书籍业务层 + * @Version: 1.0 + */ +public interface BookInfoService { + + /** + * 添加数据 + * @param bookInfo 数据对象 + * @return 返回添加的自增ID + */ + int save (BookInfo bookInfo); + + + /** + * 通过书籍ID查询书籍信息 + * @param bookId 书籍ID + * @return 数据信息 + */ + BookInfo selectByBookId(Integer bookId); + + /** + * 通过书籍信息查询集合 + * @param bookInfo 数据信息 + * @return 符合集合内容 + */ + List list(BookInfo bookInfo); + + /** + * 通过ID修改书籍信息 + * @param bookInfo 数据信息 + * @return 修改条数 + */ + int updateByBookId(BookInfo bookInfo); + + /** + * 通过书籍ID删除书籍信息 + * @param bookId 书籍ID + * @return 返回删除条数 + */ + int deleteByBookId(Integer bookId); +} diff --git a/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/service/impl/BookInfoServiceImpl.java b/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/service/impl/BookInfoServiceImpl.java new file mode 100644 index 0000000..5261c89 --- /dev/null +++ b/modules/cloud-system/system-server/src/main/java/com/muyu/cloud/system/service/impl/BookInfoServiceImpl.java @@ -0,0 +1,86 @@ +package com.muyu.cloud.system.service.impl; + +import com.muyu.cloud.system.domain.BookInfo; +import com.muyu.cloud.system.mapper.BookInfoMapper; +import com.muyu.cloud.system.service.BookInfoService; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * @Author: DongZeLiang + * @date: 2024/5/23 + * @Description: 书籍业务实现层 + * @Version: 1.0 + */ +@Service +@AllArgsConstructor +public class BookInfoServiceImpl implements BookInfoService { + + /** + * 书籍持久层 + */ + private final BookInfoMapper bookInfoMapper; + + + /** + * 添加数据 + * + * @param bookInfo 数据对象 + * + * @return 返回添加的自增ID + */ + @Override + public int save (BookInfo bookInfo) { + return bookInfoMapper.insert(bookInfo); + } + + /** + * 通过书籍ID查询书籍信息 + * + * @param bookId 书籍ID + * + * @return 数据信息 + */ + @Override + public BookInfo selectByBookId (Integer bookId) { + return bookInfoMapper.selectByBookId(bookId); + } + + /** + * 通过书籍信息查询集合 + * + * @param bookInfo 数据信息 + * + * @return 符合集合内容 + */ + @Override + public List list (BookInfo bookInfo) { + return bookInfoMapper.list(bookInfo); + } + + /** + * 通过ID修改书籍信息 + * + * @param bookInfo 数据信息 + * + * @return 修改条数 + */ + @Override + public int updateByBookId (BookInfo bookInfo) { + return bookInfoMapper.updateByBookId(bookInfo); + } + + /** + * 通过书籍ID删除书籍信息 + * + * @param bookId 书籍ID + * + * @return 返回删除条数 + */ + @Override + public int deleteByBookId (Integer bookId) { + return bookInfoMapper.deleteByBookId(bookId); + } +} diff --git a/modules/cloud-system/system-server/src/main/resources/bootstrap.yml b/modules/cloud-system/system-server/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..67def2b --- /dev/null +++ b/modules/cloud-system/system-server/src/main/resources/bootstrap.yml @@ -0,0 +1,38 @@ +# Tomcat +server: + port: 9201 + +# 配置nacos的地址 +nacos: + server-addr: 47.92.86.60:8848 + +# Spring +spring: + main: + allow-circular-references: true + jackson: + date-format: yyyy-MM-dd HH:mm:ss + time-zone: GMT+8 + application: + # 应用名称 + name: cloud-system + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: ${nacos.server-addr} + config: + # 配置中心地址 + server-addr: ${nacos.server-addr} + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + # 程序Redis公共配置文件 + - application-redis-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + # 程序MySQL公共配置文件 + - application-mysql-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} diff --git a/modules/cloud-system/system-server/src/main/resources/mapper/BookInfoMapper.xml b/modules/cloud-system/system-server/src/main/resources/mapper/BookInfoMapper.xml new file mode 100644 index 0000000..cfd3cfc --- /dev/null +++ b/modules/cloud-system/system-server/src/main/resources/mapper/BookInfoMapper.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + select + id, + name, + author, + price + from + book_info + + + + INSERT INTO book_info + (`name`, `author`, `price`) + VALUES + ( #{name}, #{author}, #{price}) + + + UPDATE book_info SET + `name` = #{name}, + `author` = #{author}, + `price` = #{price} + WHERE + `id` = #{id} + + + delete from book_info where id = #{bookId} + + + + + diff --git a/modules/pom.xml b/modules/pom.xml new file mode 100644 index 0000000..0ceb5a5 --- /dev/null +++ b/modules/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + com.muyu + cloud + 1.0.0 + + + modules + pom + + cloud-system + + + + 8 + 8 + UTF-8 + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ba84e45 --- /dev/null +++ b/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + + com.muyu + cloud + 1.0.0 + pom + + cloud-common + cloud-gateway + modules + + + + 8 + 8 + UTF-8 + + + + + + spring-boot-starter-parent + org.springframework.boot + 2.7.15 + + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + 2021.0.8 + pom + import + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + 2021.0.5.0 + pom + import + + + + com.alibaba.nacos + nacos-client + 2.0.4 + + + + com.muyu + cloud-common + 1.0.0 + + + + + com.muyu + system-common + 1.0.0 + + + + + + +