# spring_mybatis_generator
**Repository Path**: fgffgdvf/spring_mybatis_generator
## Basic Information
- **Project Name**: spring_mybatis_generator
- **Description**: springboot3 + mybatisplus + swagger3 + knife4j
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2024-08-17
- **Last Updated**: 2024-10-11
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Getting Started
### Reference Documentation
For further reference, please consider the following sections:
* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.6.13/maven-plugin/reference/html/)
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.6.13/maven-plugin/reference/html/#build-image)
* [Spring Web](https://docs.spring.io/spring-boot/docs/2.6.13/reference/htmlsingle/#web)
### Guides
The following guides illustrate how to use some features concretely:
* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)
* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/)
* [Building REST services with Spring](https://spring.io/guides/tutorials/rest/)
# springboot3.x + mybatisplus + swagger3.x结合knife4j + JSR303数据校验
### 第一步:引入依赖
com.github.xiaoymin
knife4j-openapi3-jakarta-spring-boot-starter
4.1.0
### 第二步:配置SwaggerConfig 配置类
主要类容分两种: 有分组 和 没有分组两种
### 第三步:配置application.yml
* 1.配置knife4j 增强
knife4j:
# 开启增强配置
enable: true
# 开启Swagger的Basic认证功能,默认是false
basic:
enable: true
# Basic认证用户名
username: test
# Basic认证密码
password: 123
* 2.配置mybatisplus及数据库等其他配置
### 第四步:编写controller
@RestController
@RequestMapping("/user")
@Tag(name = "用户Controller", description = "用户Controller")
public class UserController {
@Autowired
UserService userService;
@PostMapping("/save")
@Operation(summary = "新增用户",
description = "新增用户",
tags = {"用户Controller"},
responses = {
@ApiResponse(description = "新增用户成功", responseCode = "200", content = @Content(schema = @Schema(implementation = String.class))),
@ApiResponse(description = "请求参数错误", responseCode = "400", content = @Content(schema = @Schema(implementation = String.class))),
@ApiResponse(description = "服务器内部错误", responseCode = "500", content = @Content(schema = @Schema(implementation = String.class)))
})
public void save(@RequestBody User user) {
userService.save(user);
}
}
### 第五步:编写service
....
### swagger常用注解
swagger3注解
@Tag(name = "用户服务", description = "用户服务接口")
@Operation(summary = "发送短信",
description = "发送短信到指定的手机号码",
tags = {"短信服务"},
requestBody = @RequestBody(description = "发送短信的请求体", required = true
, content = @Content(schema = @Schema(implementation = SmsVo.class))
),
responses = {
@ApiResponse
(responseCode = " 200 ", description = " 短信发送成功 ", content = @ Content ( schema = @ Schema ( implementation = String.class))),
@ApiResponse
(responseCode = " 400 ", description = " 请求参数错误 ", content = @ Content ( schema = @ Schema ( implementation = String.class))),
@ApiResponse
(responseCode = " 500 ", description = " 服务器内部错误 ", content = @ Content ( schema = @ Schema ( implementation = String.class)))
})
@Schema(description = "SmsVo模型数据描述",
example = "{phoneNumber:...;message:...}}",
requiredMode = Schema.RequiredMode.REQUIRED,
name = "SmsVo模型数据",
defaultValue = "SmsVo")
1. 基本信息注解
@OpenAPIDefinition
描述:用于定义整个 API 文档的基本信息。
可用于:类、接口。
属性:
info:指定 @Info 注解的对象,用于描述 API 文档的基本信息。
@Info
描述:用于定义 API 文档的基本信息。
可用于:类、接口。
属性:
title:API 的标题。
description:API 的描述。
version:API 的版本号。
termsOfService:服务条款的 URL。
contact:指定 @Contact 注解的对象,用于描述联系人信息。
license:指定 @License 注解的对象,用于描述许可证信息。
@Contact
描述:用于定义 API 文档中的联系人信息。
可用于:类、接口。
属性:
name:联系人的名称。
url:联系人的网址。
email:联系人的电子邮件地址。
@License
描述:用于定义 API 文档中的许可证信息。
可用于:类、接口。
属性:
name:许可证的名称。
url:许可证的网址。
2. 分组注解
@Tag
描述:用于给 API 分组,用途类似于为 API 文档添加标签。
可用于:方法、类、接口。
属性:
name:分组的名称。
3. 请求方法注解
以下注解用于描述 API 的请求方法:
@Operation
描述:用于描述 API 的操作。
可用于:方法。
属性:
summary:操作的摘要信息。
description:操作的详细描述。
tags:指定 @Tag 注解的对象数组,用于将操作归类到特定的分组。
parameters:指定 @Parameter 注解的对象数组,用于描述操作的输入参数。
responses:指定 @ApiResponse 注解的对象数组,用于描述操作的响应结果。
requestBody:指定 @RequestBody 注解的对象,用于描述操作的请求体。
@Parameter
描述:用于描述操作的输入参数。
可用于:方法。
属性:
name:参数的名称。
in:参数的位置,可以是 path、query、header、cookie 中的一种。
description:参数的描述。
required:参数是否必需,默认为 false。
schema:指定 @Schema 注解的对象,用于描述参数的数据类型。
@RequestBody
描述:用于描述操作的请求体。
可用于:方法。
属性:
required:请求体是否必需,默认为 false。
content:指定 @Content 注解的对象数组,用于描述请求体的内容。
@ApiResponse
描述:用于描述操作的响应结果。
可用于:方法。
属性:
responseCode:响应的状态码。
description:响应的描述。
content:指定 @Content 注解的对象数组,用于描述响应的内容。
@Content
描述:用于描述请求体或响应的内容。
可用于:方法。
属性:
mediaType:内容的媒体类型。
schema:指定 @Schema 注解的对象,用于描述内容的数据类型。
@Schema
描述:用于描述数据模型的属性。
可用于:方法、类、接口。
属性:
title:数据模型的标题。
description:数据模型的描述。
type:数据模型的类型。
format:数据模型的格式。
4. 路径注解
以下注解用于描述 API 的路径:
@Path
描述:用于定义路径参数。
可用于:方法。
属性:
value:路径参数的名称。
@PathVariable
描述:用于描述路径参数。
可用于:方法的参数。
属性:
value:路径参数的名称。
@RequestParam
描述:用于描述查询参数。
可用于:方法的参数。
属性:
value:查询参数的名称。
required:查询参数是否必需,默认为 false。
@RequestBody
描述:用于描述请求体。
可用于:方法的参数。
5. 响应注解
以下注解用于描述 API 的响应结果:
@ApiResponse
描述:用于描述响应结果。
可用于:方法。
属性:
responseCode:响应的状态码。
description:响应的描述。
content:指定 @Content 注解的对象数组,用于描述响应的内容。
@Content
描述:用于描述响应结果的内容。
可用于:方法。
属性:
mediaType:内容的媒体类型。
schema:指定 @Schema 注解的对象,用于描述内容的数据类型。
@Schema
描述:用于描述数据模型的属性。
可用于:方法、类、接口。
属性:
title:数据模型的标题。
description:数据模型的描述。
type:数据模型的类型。
format:数据模型的格式。
### 第六步 使用JSR303校验
1.1 导入依赖
org.springframework.boot
spring-boot-starter-validation
1.2 按照需求进行自定义校验规则
@Pattern(regexp = "(^[a-zA-Z0-9_-]{6,16}$)|(^[\\u2E80-\\u9FFF]{2,10})",
message = "用户名必须是2-10位中文或者6-16位英文和数字的组合",
groups = {AddGroup.class, UpdateGroup.class})
private String name;
/**
* 用户年龄
*/
@ListValue(values ={10,20,30} ,groups = {AddGroup.class, UpdateGroup.class })
@NotNull(message = "年龄不能为空", groups = {AddGroup.class, UpdateGroup.class })
private Integer age;
1.3 在需要校验的实体类上加上 @Valid 注解
@PostMapping("/update")
public void update(@Valid @RequestBody User user) {
System.out.println("user = " + user);
userService.updateById(user);
}
1.4 测试(postman 等方式)
1.5 获取错误提示信息
@GetMapping("/into")
public String into(@Validated({AddGroup.class,UpdateGroup.class}) User user , BindingResult bindingResult) {
System.out.println("user = " + user);
//获取错误的校验结果
if(bindingResult.hasErrors()){
Map map = new HashMap<>();
bindingResult.getFieldErrors().forEach((item) ->{
//错误提示信息
String msg = item.getDefaultMessage();
//获取错误的属性或字段名称
String fie = item.getField();
map.put(fie,msg);
});
return map.toString();
}
return "200";
}
#### 使用分组
2.1 新增校验的分组接口
public interface AddGroup {
}
public interface UpdateGroup {
}
2.2 在实体类的属性字段上新增分组属性
@Pattern(regexp = "(^[a-zA-Z0-9_-]{6,16}$)|(^[\\u2E80-\\u9FFF]{2,10})",
message = "用户名必须是2-10位中文或者6-16位英文和数字的组合",
groups = {AddGroup.class, UpdateGroup.class})
private String name;
/**
* 用户年龄
*/
@ListValue(values ={10,20,30} ,groups = {AddGroup.class, UpdateGroup.class })
@NotNull(message = "年龄不能为空", groups = {AddGroup.class, UpdateGroup.class })
private Integer age;
2.3 在Controller 层的方法上新增 @Validated({xxx.class}) 分组属性
@PostMapping("/update")
public void update(@Validated(UpdateGroup.class) @RequestBody User user) {
System.out.println("user = " + user);
userService.updateById(user);
}
2.4 测试(postman 等方式)
#### 对自定义的注解进行配置
3.1 新增自定义校验注解
ListValue 接口内容
3.2 配置文件:ValidationMessages.properties,配置的是默认的错误提示信息
com.jwq.generator.enums.ListValue.message=显示信息只能是10 20 30
3.3 编写自定义的校验器类。配置该注解的校验规则 CustomConstraintValidator
3.4 在实体类的属性字段上新增分组属性 @ListValue
/**
* 用户年龄
*/
@ListValue(values ={10,20,30} ,groups = {AddGroup.class, UpdateGroup.class })
@NotNull(message = "年龄不能为空", groups = {AddGroup.class, UpdateGroup.class })
private Integer age;
3.5 在Controller 层的方法上新增 @Validated({xxx.class}) 分组属性
3.6 测试(postman 等方式)
常用注解:
@Null 限制只能为null
@NotNull 限制必须不为null
@AssertFalse 限制必须为false
@AssertTrue 限制必须为true
@DecimalMax(value) 限制必须为一个不大于指定值的数字
@DecimalMin(value) 限制必须为一个不小于指定值的数字
@Digits(integer,fraction) 限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过fraction
@Future 限制必须是一个将来的日期
@Max(value) 限制必须为一个不大于指定值的数字
@Min(value) 限制必须为一个不小于指定值的数字
@Past 限制必须是一个过去的日期
@Pattern(value) 限制必须符合指定的正则表达式
@Size(max,min) 限制字符长度必须在min到max之间
@Past 验证注解的元素值(日期类型)比当前时间早
@NotEmpty 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)
@NotBlank 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格
@Email 验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式
### 第六步 使用JSR303校验
1.1 导入依赖
org.springframework.boot
spring-boot-starter-validation
1.2 按照需求进行自定义校验规则
@Pattern(regexp = "(^[a-zA-Z0-9_-]{6,16}$)|(^[\\u2E80-\\u9FFF]{2,10})",
message = "用户名必须是2-10位中文或者6-16位英文和数字的组合",
groups = {AddGroup.class, UpdateGroup.class})
private String name;
/**
* 用户年龄
*/
@ListValue(values ={10,20,30} ,groups = {AddGroup.class, UpdateGroup.class })
@NotNull(message = "年龄不能为空", groups = {AddGroup.class, UpdateGroup.class })
private Integer age;
1.3 在需要校验的实体类上加上 @Valid 注解
@PostMapping("/update")
public void update(@Valid @RequestBody User user) {
System.out.println("user = " + user);
userService.updateById(user);
}
1.4 测试(postman 等方式)
1.5 获取错误提示信息
@GetMapping("/into")
public String into(@Validated({AddGroup.class,UpdateGroup.class}) User user , BindingResult bindingResult) {
System.out.println("user = " + user);
//获取错误的校验结果
if(bindingResult.hasErrors()){
Map map = new HashMap<>();
bindingResult.getFieldErrors().forEach((item) ->{
//错误提示信息
String msg = item.getDefaultMessage();
//获取错误的属性或字段名称
String fie = item.getField();
map.put(fie,msg);
});
return map.toString();
}
return "200";
}
#### 使用分组
2.1 新增校验的分组接口
public interface AddGroup {
}
public interface UpdateGroup {
}
2.2 在实体类的属性字段上新增分组属性
@Pattern(regexp = "(^[a-zA-Z0-9_-]{6,16}$)|(^[\\u2E80-\\u9FFF]{2,10})",
message = "用户名必须是2-10位中文或者6-16位英文和数字的组合",
groups = {AddGroup.class, UpdateGroup.class})
private String name;
/**
* 用户年龄
*/
@ListValue(values ={10,20,30} ,groups = {AddGroup.class, UpdateGroup.class })
@NotNull(message = "年龄不能为空", groups = {AddGroup.class, UpdateGroup.class })
private Integer age;
2.3 在Controller 层的方法上新增 @Validated({xxx.class}) 分组属性
@PostMapping("/update")
public void update(@Validated(UpdateGroup.class) @RequestBody User user) {
System.out.println("user = " + user);
userService.updateById(user);
}
2.4 测试(postman 等方式)
#### 对自定义的注解进行配置
3.1 新增自定义校验注解
ListValue 接口内容
3.2 配置文件:ValidationMessages.properties,配置的是默认的错误提示信息
com.jwq.generator.enums.ListValue.message=显示信息只能是10 20 30
3.3 编写自定义的校验器类。配置该注解的校验规则 CustomConstraintValidator
3.4 在实体类的属性字段上新增分组属性 @ListValue
/**
* 用户年龄
*/
@ListValue(values ={10,20,30} ,groups = {AddGroup.class, UpdateGroup.class })
@NotNull(message = "年龄不能为空", groups = {AddGroup.class, UpdateGroup.class })
private Integer age;
3.5 在Controller 层的方法上新增 @Validated({xxx.class}) 分组属性
3.6 测试(postman 等方式)
常用注解:
@Null 限制只能为null
@NotNull 限制必须不为null
@AssertFalse 限制必须为false
@AssertTrue 限制必须为true
@DecimalMax(value) 限制必须为一个不大于指定值的数字
@DecimalMin(value) 限制必须为一个不小于指定值的数字
@Digits(integer,fraction) 限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过fraction
@Future 限制必须是一个将来的日期
@Max(value) 限制必须为一个不大于指定值的数字
@Min(value) 限制必须为一个不小于指定值的数字
@Past 限制必须是一个过去的日期
@Pattern(value) 限制必须符合指定的正则表达式
@Size(max,min) 限制字符长度必须在min到max之间
@Past 验证注解的元素值(日期类型)比当前时间早
@NotEmpty 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)
@NotBlank 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格
@Email 验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式