创建maven项目
pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> <relativePath/> </parent> <groupId>jkw.life</groupId> <artifactId>java-sc-single</artifactId> <version>0.0.1</version> <dependencies> <!-- 阿里短信平台 --> <dependency> <groupId>com.aliyun</groupId> <artifactId>dysmsapi20170525</artifactId> <version>2.0.9</version> </dependency> <!-- JWT --> <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.4.0</version> </dependency> <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> <!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- elasticsearch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!-- FastDFS --> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.5</version> </dependency> <!--mongodb--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <!-- thymeleaf整合spring-security --> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> <!-- spring security --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- MyBatisPlus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.0</version> </dependency> <!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--springMVC--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--springBoot起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.2.2</version> <configuration> <!-- 镜像名 --> <imageName>${project.artifactId}</imageName> <!-- 镜像版本 --> <imageTags> <imageTag>${project.version}</imageTag> </imageTags> <!-- Docker路径 --> <dockerHost>http://ip:2375</dockerHost> <!-- Dockerfile定义 --> <baseImage>openjdk:11</baseImage> <!-- 作者 --> <maintainer>jkw</maintainer> <resources> <resource> <targetPath>/</targetPath> <!-- 从哪个包拷贝文件,target包 --> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <workdir>/</workdir> <entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint> <forceTags>true</forceTags> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
application.yml
server: port: 8032 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/moonwood_sc?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 username: root password: root # 打成jar包必须添加如下配置才能找到页面 thymeleaf: mode: HTML cache: false prefix: classpath:/templates #配置mybatis-plus(使用代码生成器时不要配置) mybatis-plus: global-config: db-config: # 设置实体类所对应的表的统一前缀 # table-prefix: t_ # 主键生成策略为自增(属性只用加上@TableId 即可) id-type: auto configuration: # 关闭列名自动驼峰命名规则映射(列名的_自动映射成驼峰)(本项目中实体类和数据库表的列名一致, map-underscore-to-camel-case: false log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启sql日志 # 日志格式 logging: pattern: console: '%d{HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n'
启动类
@MapperScan("jkw.mapper") @SpringBootApplication public class Main { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //分页插件 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } public static void main(String[] args) { SpringApplication.run(Main.class, args); } }
返回结果封装
CodeEnum
@Getter @AllArgsConstructor public enum CodeEnum { SUCCESS(200, "OK"), SYSTEM_ERROR(500, "系统异常"), FILE_FASTDFS_UPLOAD_ERROR(601,"fastdfs文件上传异常"), FILE_FASTDFS_DELETE_ERROR(602,"fastdfs文件删除异常"), ; private final Integer code; private final String message; }
BaseResult
@Data @AllArgsConstructor public class BaseResult<T> implements Serializable { //状态码(成功:200,失败:其他) private Integer code; //提示信息 private String message; //返回数据 private T data; //构建成功结果 public static <T> BaseResult<T> ok(){ return new BaseResult(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMessage(),null); } //构建带有数据的成功结果 public static <T> BaseResult<T> ok(T data){ return new BaseResult(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMessage(),data); } }
全局异常处理
自定义异常类
@Data public class MyException extends RuntimeException implements Serializable { private Integer code; private String message; public MyException() { } public MyException(Integer code, String message) { this.code = code; this.message = message; } }
全局异常处理器
@RestControllerAdvice public class GlobalExceptionHandler { //处理自定义异常 @ExceptionHandler(MyException.class) public BaseResult defaultExceptionHandler(HttpServletRequest req, HttpServletResponse resp, MyException e) { BaseResult baseResult = new BaseResult(e.getCode(), e.getMessage(), null); return baseResult; } // 处理权限不足异常,捕获到异常后再次抛出,交给AccessDeniedHandler处理 @ExceptionHandler(AccessDeniedException.class) public void defaultExceptionHandler(AccessDeniedException e) throws AccessDeniedException { throw e; } //上面处理不了的交给系统异常处理 @ExceptionHandler(java.lang.Exception.class) public BaseResult defaultExceptionHandler(HttpServletRequest req, HttpServletResponse resp, Exception e) { e.printStackTrace(); BaseResult baseResult = new BaseResult(CodeEnum.SYSTEM_ERROR.getCode(), CodeEnum.SYSTEM_ERROR.getMessage(), null); return baseResult; } }