diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..410be32 --- /dev/null +++ b/.gitignore @@ -0,0 +1,44 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +.mvn/ +test/ +test/* +# But not empty folders +!*/ + + + + + diff --git a/README.en.md b/README.en.md deleted file mode 100644 index e9f6b80..0000000 --- a/README.en.md +++ /dev/null @@ -1,36 +0,0 @@ -# springboot-template - -#### Description -快速搭建spingboot项目让你上手就开始写业务,帮你依赖了目前国内比较火的框架、数据源、yml通用配置:MySQL、Redis、mybatis-plus... - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README.md b/README.md index f045032..7e4db18 100644 --- a/README.md +++ b/README.md @@ -1,37 +1,103 @@ -# springboot-template +# 这个项目是干什么的 +- 提供基础框架,让你进行快速开发。对于每次新建项目时,各种的建包、基础的日志监控、以及提供标准的日志格式文件等等...通用的功能太耽误我们的时间于是,于是萌生这个想法将这些东西抽离出来,使用的时候将他拉取下来改文件夹名字,就搭建好了基础框架。就像是代码生成器 +- 记得我学习spring-boot的时候,有一句话到现在仍记得`约定大于配置`,所以我希望使用本项目的遵从我的约定,当然你也可以进行改动 +## 目前已搭建好 -#### 介绍 -快速搭建spingboot项目让你上手就开始写业务,帮你依赖了目前国内比较火的框架、数据源、yml通用配置:MySQL、Redis、mybatis-plus... +1. API请求日志监控 +2. 全局异常日志监控 +3. 提供日志文件格式 +4. 全局响应类 +5. Redis序列化 +6. swagger通用配置 +7. knife4j框架集成 +8. 跨域配置 +9. 通用的yml配置 -#### 软件架构 -软件架构说明 +## 目前已集成的依赖 +### spring-boot帮我们做版本依赖 -#### 安装教程 +1. `spring-boot-starter-web`:servlet +2. `spring-boot-starter-aop`:aop编程 +3. `spring-boot-starter-test`:模块测试 +4. `spring-boot-starter-validation`:参数校验 +5. `spring-boot-starter-data-redis`:redis集成 +6. `mysql-connector-j`:MySQL驱动 +7. `spring-boot-starter-jdbc`:基于MySQL驱动提供jdbc简单编程 +8. `lombok`:get/set方法 -1. xxxx -2. xxxx -3. xxxx +### 需要去官网/仓库去看版本信息 -#### 使用说明 +1. `hutool-all`:java语法糖 +2. `knife4j-openapi3-jakarta-spring-boot-starter`:基于swagger的接口测试框架 +3. `mybatis-plus-boot-starter`:用代码写SQL +4. `pagehelper-spring-boot-starter`:分页工具 -1. xxxx -2. xxxx -3. xxxx +### 测试说明 -#### 参与贡献 +1. 在`com.template.controller`包下有三个测试文件测试项目集成的依赖、功能能否使用。当然这样存放文件是不标准,测试结束没问题可以删除,注意exception包的文件不可以删除可以修改 + - Redis集成测试 + - MySQL集成测试 + - 日志监控测试 +2. 测试接口框架:查看`localhost:8080/doc.html` +3. 在`application-dev.yml`文件中 + - redis:需要连接自己的 + - mysql:需要来连接自己数据库,**script/init.sql**提供了sql脚本请务必使用,否则关于MySQL的测试可能失败 -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request +## 启动类说明 +1. 项目中能使用并带有`Enable`的注解都已经加上 -#### 特技 -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) + + +## 包介绍 +- **文件存放请按照图中来** +```text +main/ +├── java +│   └── com +│   └── template +│   ├── annotation 自定义注解包 +│   ├── Application.java 启动类 +│   ├── config 配置包 +│   ├── controller controller层 +│   │   ├── exception +│   │   │   └── ExceptionController.java 全局异常类 +│   │   ├── TestController.java 测试类 +│   │   ├── TestUseEntity.java 测试类 +│   │   └── TestUserMapper.java 测试类 +│   ├── core 核心包,这里面都是增加的功能一般不用动这个包,也不要把文件创建在这里 +│   │   ├── log +│   │   │   ├── BaseLog.java +│   │   │   └── MonitorAPI.java +│   │   ├── redis +│   │   │   └── config +│   │   │   └── RedisConfig.java +│   │   ├── resp +│   │   │   ├── Resp.java 全局响应类 +│   │   │   └── R.java 自定义响应需要实现这个接口 +│   │   ├── swagger +│   │   │   └── SwaggerConfig.java +│   │   └── web +│   │   └── SpringMvcWebConfig.java 跨域配置 +│   ├── dictionary 字典包 +│   ├── domain 领域包,可以自定义以下包是推荐的 +│   │   ├── dto +│   │   ├── service +│   │   └── vo +│   ├── entities ActiveRecord包,也就是实体类包 +│   ├── exception 自定义异常包 +│   ├── filter 过滤器包 +│   ├── interceptor 拦截器包 +│   ├── mapper 相当于mapper包但是这样命名更加规范 +│   └── utils 工具包 +└── resources + ├── application-config.yml + ├── application-dev.yml + ├── application.yml + ├── logback.xml 日志格式文件 + ├── mapper mybatis文件 + └── script + └── init.sql sql脚本 +``` \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4e3be46 --- /dev/null +++ b/pom.xml @@ -0,0 +1,118 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.1.3 + + + + com.xxl + springboot-template + version:3.1.3 + + + springboot-template + spring boot项目模板 + + + + 17 + UTF-8 + UTF-8 + 17 + 17 + + 4.2.0 + 3.5.3.1 + 1.4.6 + 5.8.20 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-jdbc + + + + com.mysql + mysql-connector-j + runtime + + + + com.baomidou + mybatis-plus-boot-starter + ${com.baomidou.version} + + + + org.springframework.boot + spring-boot-starter-aop + + + + org.springframework.boot + spring-boot-starter-validation + + + + cn.hutool + hutool-all + ${cn.hutool.version} + + + + com.github.pagehelper + pagehelper-spring-boot-starter + ${pagehelper-spring-boot-starter.version} + + + + com.github.xiaoymin + knife4j-openapi3-jakarta-spring-boot-starter + ${com.github.xiaoymin.version} + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/src/main/java/com/template/Application.java b/src/main/java/com/template/Application.java new file mode 100644 index 0000000..e634f19 --- /dev/null +++ b/src/main/java/com/template/Application.java @@ -0,0 +1,37 @@ +package com.template; + +import lombok.extern.slf4j.Slf4j; +import org.mybatis.spring.annotation.MapperScan; +import org.mybatis.spring.annotation.MapperScans; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.stereotype.Indexed; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +/** + * @author: anyone + * @since: 2023/9/16 + * @description: 启动类 + */ +@EnableAspectJAutoProxy +@EnableAsync +@EnableScheduling +@EnableTransactionManagement +@Slf4j +@Indexed +@MapperScans({@MapperScan("com.template.mapper"),@MapperScan("com.template.controller")}) +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + try { + SpringApplication.run(Application.class, args); + log.info("项目启动成功(ง ˙o˙)ว"); + } catch (Exception e) { + log.error("启动失败:",e); + } + } +} diff --git a/src/main/java/com/template/controller/TestController.java b/src/main/java/com/template/controller/TestController.java new file mode 100644 index 0000000..1a61321 --- /dev/null +++ b/src/main/java/com/template/controller/TestController.java @@ -0,0 +1,82 @@ +package com.template.controller; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.template.core.resp.R; +import com.template.core.resp.Resp; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * 测试项目是否可以运行 + * @author xxl + * @since 2023/9/16 + */ +@RestController +@RequestMapping(value = "/test", produces = "application/json") +@Slf4j +@Tag(name = "测试API") +public class TestController { + /** + * 测试接口 + * @return + */ + @GetMapping("/hello") + @Operation(summary = "测试接口") + public Resp hello() { + return R.success(); + } + + @Resource + RedisTemplate redisTemplate; + /** + * 测试redis + * @param key + * @return 成功返回OK + */ + @GetMapping("redis") + @Operation(summary = "测试redis") + @Parameter(name = "key",description = "redis的key") + public Resp redis(@RequestParam("key")String key) { + redisTemplate.opsForValue().set("username","xxl"); + Object o = redisTemplate.opsForValue().get(key); + log.info("查询:key=>{},value=>{}",key,o); + redisTemplate.keys("*").forEach(dto -> log.info("key=>{},value=>{}",dto,redisTemplate.opsForValue().get(dto))); + return R.success(); + } + + /** + * 测试MySQL + * @param id + * @return 成功返回OK + */ + @GetMapping("mysql") + @Operation(summary = "测试mysql") + @Parameter(name = "id",description = "列id") + public Resp mysql(@RequestParam("id")String id) { + TestUseEntity user = new TestUseEntity(); + //插入 + user.setId("16494"); + user.setUsername("XXL"); + user.setPasswd("123456"); + user.insert(); + + // 指定查询 + TestUseEntity testUseEntity = user.selectById(id); + log.info("id:{},value:{}",id,testUseEntity.toString()); + + //查询所有 + List testUseEntities = user.selectList(new LambdaQueryWrapper<>()); + testUseEntities.forEach(dto -> log.info(dto.toString())); + return R.success(); + } +} diff --git a/src/main/java/com/template/controller/TestUseEntity.java b/src/main/java/com/template/controller/TestUseEntity.java new file mode 100644 index 0000000..9ec2dbe --- /dev/null +++ b/src/main/java/com/template/controller/TestUseEntity.java @@ -0,0 +1,31 @@ +package com.template.controller; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.activerecord.Model; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +import java.io.Serial; +import java.io.Serializable; + +/** + * @author xxl + * @since 2023/9/17 + */ +@EqualsAndHashCode(callSuper = true) +@Data +@Accessors(chain = true) +@TableName("test_user") +public class TestUseEntity extends Model implements Serializable { + @Serial + private static final long serialVersionUID = -6083882932972222185L; + @TableId + private String id; + @TableField("username") + private String username; + @TableField("passwd") + private String passwd; +} diff --git a/src/main/java/com/template/controller/TestUserMapper.java b/src/main/java/com/template/controller/TestUserMapper.java new file mode 100644 index 0000000..85de0a0 --- /dev/null +++ b/src/main/java/com/template/controller/TestUserMapper.java @@ -0,0 +1,11 @@ +package com.template.controller; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** + * @author xxl + * @since 2023/9/17 + */ +public interface TestUserMapper extends BaseMapper { + +} diff --git a/src/main/java/com/template/controller/exception/ExceptionController.java b/src/main/java/com/template/controller/exception/ExceptionController.java new file mode 100644 index 0000000..ea9a09e --- /dev/null +++ b/src/main/java/com/template/controller/exception/ExceptionController.java @@ -0,0 +1,24 @@ +package com.template.controller.exception; + +import com.template.core.resp.R; +import com.template.core.resp.Resp; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * 对异常的统一返回 + * @author xxl + * @since 2023/9/16 + */ +@RestControllerAdvice +@Slf4j +public class ExceptionController { + /** + * 捕捉spring boot容器所有的未知异常 + */ + @ExceptionHandler(Exception.class) + public Resp exception(Exception exception) { + return R.fail(); + } +} diff --git a/src/main/java/com/template/core/log/BaseLog.java b/src/main/java/com/template/core/log/BaseLog.java new file mode 100644 index 0000000..b789c31 --- /dev/null +++ b/src/main/java/com/template/core/log/BaseLog.java @@ -0,0 +1,73 @@ +package com.template.core.log; + +import cn.hutool.core.util.ObjectUtil; +import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Arrays; + +/** + * 日志监控 + * @author xxl + * @since 2023/9/16 + */ +@Aspect +@Component +@Slf4j +public class BaseLog { + @Resource + HttpServletRequest request; + @Pointcut("execution(* com.template.controller..* (..)) && !execution(* com.template.controller.exception..*(..))") + public void apiPointCut() { + } + @Pointcut("execution(* com.template.controller.exception..* (..))") + public void exceptionPointCut() { + } + + + private MonitorAPI monitor; + + @Autowired(required = false) + public void setMonitor(MonitorAPI monitor) { + this.monitor = monitor; + } + + /** + * com.template.controller包下的所有接口的aop监控 + * @param point 切点 + * @return 接口返回结果 + * @throws Throwable + */ + + @Around("apiPointCut()") + public Object apiLog(ProceedingJoinPoint point) throws Throwable { + Object proceed = null; + if (ObjectUtil.isNotNull(monitor)) {point = (ProceedingJoinPoint) monitor.getPoint(point, request);} + + try { + proceed = point.proceed(); + } finally { + log.info("[请求API:[{}],请求参数:[{}],返回结果:[{}]]", request.getRequestURI(), Arrays.toString(point.getArgs()),proceed); + } + return proceed; + } + + /** + * 全局异常的监控 + * @param point 切点 + */ + @Before("exceptionPointCut()") + public void exceptionLog(JoinPoint point) { + log.error("[请求错误API:[{}],错误类型:[{}],错误原因:[{}]]", request.getRequestURI(), point.getSignature().toString(), Arrays.toString(point.getArgs())); + } + +} diff --git a/src/main/java/com/template/core/log/MonitorAPI.java b/src/main/java/com/template/core/log/MonitorAPI.java new file mode 100644 index 0000000..a8360e5 --- /dev/null +++ b/src/main/java/com/template/core/log/MonitorAPI.java @@ -0,0 +1,13 @@ +package com.template.core.log; + +import jakarta.servlet.http.HttpServletRequest; +import org.aspectj.lang.JoinPoint; + +/** + * 获取监控API对象,假如对某个IP进行监控,如果不操作就返回空 + * @author xxl + * @since 2023/9/16 + */ +public interface MonitorAPI { + JoinPoint getPoint(JoinPoint joinPoint, HttpServletRequest request); +} diff --git a/src/main/java/com/template/core/redis/config/RedisConfig.java b/src/main/java/com/template/core/redis/config/RedisConfig.java new file mode 100644 index 0000000..06e9094 --- /dev/null +++ b/src/main/java/com/template/core/redis/config/RedisConfig.java @@ -0,0 +1,39 @@ +package com.template.core.redis.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * redis序列化 + * @author xxl + * @since 2023/9/16 + */ +@Configuration +public class RedisConfig { + /** + * 序列化相关的配置 + */ + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + //支持事务 + redisTemplate.setEnableTransactionSupport(true); + //json方式序列化 + Jackson2JsonRedisSerializer jackson = new Jackson2JsonRedisSerializer<>(Object.class); + redisTemplate.setConnectionFactory(factory); + //String方式序列化 + StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); + //设置所有的key为string方式序列化 + redisTemplate.setKeySerializer(stringRedisSerializer); + redisTemplate.setHashKeySerializer(stringRedisSerializer); + //设置所有的value为jackson方式序列化 + redisTemplate.setValueSerializer(jackson); + redisTemplate.setHashValueSerializer(jackson); + redisTemplate.afterPropertiesSet(); + return redisTemplate; + } +} diff --git a/src/main/java/com/template/core/resp/R.java b/src/main/java/com/template/core/resp/R.java new file mode 100644 index 0000000..7c53a70 --- /dev/null +++ b/src/main/java/com/template/core/resp/R.java @@ -0,0 +1,39 @@ +package com.template.core.resp; + +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import static org.springframework.http.HttpStatus.OK; + +/** + * 自定义响应需要集成这个类 + * @author xxl + * @since 2023/9/16 + */ +public interface R { + /** + * 成功 + */ + Resp success(String message,T t); + Resp success(String code,String message); + Resp success(String message); + + /** + * 失败 + */ + Resp fail(String message); + Resp fail(String message,T t); + Resp fail(String code,String message); + + + + static Resp success(){ + String reasonPhrase = OK.getReasonPhrase(); + int value = OK.value(); + return Resp.baseResp(String.valueOf(value),reasonPhrase,null); + } + + static Resp fail() { + String reasonPhrase = INTERNAL_SERVER_ERROR.getReasonPhrase(); + int value = INTERNAL_SERVER_ERROR.value(); + return Resp.baseResp(String.valueOf(value),reasonPhrase,null); + } +} diff --git a/src/main/java/com/template/core/resp/Resp.java b/src/main/java/com/template/core/resp/Resp.java new file mode 100644 index 0000000..23ab3f1 --- /dev/null +++ b/src/main/java/com/template/core/resp/Resp.java @@ -0,0 +1,38 @@ +package com.template.core.resp; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serial; +import java.io.Serializable; + +/** + * 统一响应基础类 + * @author xxl + * @since 2023/9/16 + */ +@Data +@Accessors(chain = true) +@Schema(description = "响应对象") +public class Resp implements Serializable { + @Serial + private static final long serialVersionUID = -3917323953100432259L; + + @Schema(description = "状态码") + private String code; + @Schema(description = "提示信息") + private String message; + @Schema(description = "返回数据") + private T data; + + public Resp(String code, String message, T data) { + this.code = code; + this.message = message; + this.data = data; + } + + public static Resp baseResp(String code, String message, T data) { + return new Resp(code, message,data); + } +} diff --git a/src/main/java/com/template/core/swagger/SwaggerConfig.java b/src/main/java/com/template/core/swagger/SwaggerConfig.java new file mode 100644 index 0000000..57f073c --- /dev/null +++ b/src/main/java/com/template/core/swagger/SwaggerConfig.java @@ -0,0 +1,24 @@ +package com.template.core.swagger; + +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.info.Contact; +import io.swagger.v3.oas.annotations.info.Info; +import io.swagger.v3.oas.annotations.servers.Server; +import org.springframework.context.annotation.Configuration; + +/** + * knife4j的配置 + * @author xxl + * @since 2023/9/16 + */ +@OpenAPIDefinition( + info = @Info( + title = "接口文档",description = "接口文档",version = "version:1.0.0", + contact = @Contact(name = "XXL",email = "3578144921@qq.com") + ), + servers = {@Server(url = "http://localhost:8080/",description = "本地环境")} +) +@Configuration +public class SwaggerConfig { + +} diff --git a/src/main/java/com/template/core/web/SpringMvcWebConfig.java b/src/main/java/com/template/core/web/SpringMvcWebConfig.java new file mode 100644 index 0000000..2f44d62 --- /dev/null +++ b/src/main/java/com/template/core/web/SpringMvcWebConfig.java @@ -0,0 +1,32 @@ +package com.template.core.web; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import org.springframework.web.filter.CorsFilter; + +/** + * @author xxl + * @since 2023/9/16 + */ +@Configuration +public class SpringMvcWebConfig { + /** + * 跨域配置 + */ + @Bean + @Lazy + public CorsFilter corsFilter() { + final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + final CorsConfiguration config = new CorsConfiguration(); + config.addAllowedOrigin("*"); + config.addAllowedHeader("*"); + // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了,这里设置2个小时 + config.setMaxAge(60*60*2L); + config.addAllowedMethod("*"); + source.registerCorsConfiguration("/**", config); + return new CorsFilter(source); + } +} diff --git a/src/main/resources/application-config.yml b/src/main/resources/application-config.yml new file mode 100644 index 0000000..57341f0 --- /dev/null +++ b/src/main/resources/application-config.yml @@ -0,0 +1,20 @@ +#========================swagger 配置======================================== +springdoc: + api-docs: + enabled: true #是否开启文档功能关系到swagger-ui能否开启 + path: /v3/api-docs #swagger后端请求地址,查看swagger所有配置,同时也是配置web端的接口配置,必须写 + swagger-ui: + tags-sorter: alpha #标签排序 + operations-sorter: alpha #操作排序 + path: /swagger #自定义swagger前端请求路径,输入http:127.0.0.1:8080/swagger会自动重定向到swagger页面注意是path不是url + group-configs: + - group: 测试API + paths-to-match: + - /test/** +knife4j: # knife4j的增强配置,不需要增强可以不配 + enable: true #开启访问默认路径doc.html + setting: + language: zh_cn + enable-request-cache: false #不开启缓存请求 + cors: true #跨域请求 + production: false #是否是生产环境,如果设置true,doc.html就不能访问 diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml new file mode 100644 index 0000000..448cebc --- /dev/null +++ b/src/main/resources/application-dev.yml @@ -0,0 +1,14 @@ +#测试环境 +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + #用于连接本地数据库测试是否能正确运行 + url: jdbc:mysql://192.168.93.135:13306/test_database?userSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 + username: root + password: root123456 + hikari: + max-lifetime: 500000 #连接的最大生命周期,表示一个连接在被关闭之前的最长使用时间。设置为30分钟(1800000毫秒) + data: + redis: + port: 6379 + host: 192.168.93.135 \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..5d5dbc5 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,42 @@ +#==========================spring设置=========================================== +spring: + main: + lazy-initialization: true #懒加载 + profiles: + active: + - config #swagger配置 + - dev #数据源配置 + task: + scheduling: + pool: + size: 10 #定时任务线程池大小设置默认1设置为5 + jackson: + time-zone: GMT+8 + date-format: java.text.SimpleDateFormat #jackson时间格式化类 + mvc: + format: + date-time: yyyy-MM-dd HH:mm:ss #日期格式化 + servlet: + multipart: + max-request-size: 31457280 #文件上传大小。默认10mb,字节为单位 + file-size-threshold: 0 #文件写入磁盘的阈值,设置为0,即上传就存储在服务器,如果是100则表示小于100会存储在内存 + max-file-size: 31457280 #单个文件上传的最大值 +server: + port: 8080 +#==========================mybatis配置======================================= +mybatis-plus: + global-config: + db-config: + id-type: assign_id #全局主键使用雪花算法生成id + update-strategy: ignored #更新不忽略null + configuration: + log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl #打印sql + mapper-locations: "classpath:mapper/*.xml" +#==========================分页设置=========================================== +pagehelper: + helper-dialect: mysql #分页方言设置 + properties: propertyValue + reasonable: true # 是否启用合理化分页(根据页码自动计算合理的值) + defaultCount: true # 分页插件默认参数支持 default-count 形式,自定义扩展的参数,必须大小写一致 + params: count=countSql # 分页参数映射,count指定总数,可以自定义 + support-methods-arguments: true #支持自定义参数 diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..12eaa40 --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,84 @@ + + + + + + + + + + + %d{yyyy-MM-dd HH:mm} [%thread] %-5level %logger{36} - %msg%n + utf-8 + + + + + + + ${LOG_HOME}/${appName}.log + + + + ${LOG_HOME}/${appName}-%d{yyyy-MM-dd}-%i.log + + 36500 + + + 100MB + + + + + + + %d{yyyy-MM-dd HH:mm} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/script/init.sql b/src/main/resources/script/init.sql new file mode 100644 index 0000000..ffec8a5 --- /dev/null +++ b/src/main/resources/script/init.sql @@ -0,0 +1,13 @@ +drop database if exists test_database; +create database test_database; +use test_database; +create table test_user( + id varchar(50) primary key, + username varchar(50), + passwd varchar(50) +)engine = innodb charset = UTF8; + +insert into test_user(id, username, passwd) +values ('1','迪丽热巴','123'), + ('2','古力娜扎','456'), + ('3','欧阳娜娜','789');