docs/工作收获/2024.5.27.md
2024-05-27 13:20:09 +08:00

120 lines
4.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# swagger3使用文档
https://blog.csdn.net/YXXXYX/article/details/124952856
# swagger基本注解
https://blog.csdn.net/qq_52774158/article/details/131081371
# 各种mapping注解的含义
1. @RequestMapping (Spring MVC)
用途: 用于映射HTTP请求到MVC和REST控制器的处理方法上。它可以应用于类级别定义控制器处理的基路径和方法级别细化具体请求路径
2. @GetMapping, @PostMapping, @PutMapping, @DeleteMapping (Spring MVC)
用途: 这些是@RequestMapping的特化版本分别对应HTTP的GET、POST、PUT、DELETE方法使代码更简洁易读。
3. @RequestBody (Spring MVC)
用途: 用于将HTTP请求体中的数据绑定到方法参数上通常用于接收JSON、XML等格式的数据。
列如:
```java
public ResponseEntity<User> createUser(@RequestBody User newUser) { ... }
```
4. @ResponseBody (Spring MVC)
之前已解释: 将方法的返回值直接写入HTTP响应体中常用于返回JSON、XML等数据。
5. @PathVariable
用途: 用于从URL路径中提取变量值并绑定到方法参数上。
列如:
```java
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) { ... }
```
6. @RequestParam
用途: 用于将请求参数绑定到方法参数上。
列入:
```java
@GetMapping("/users")
public List<User> searchUsers(@RequestParam("name") String name) { ... }
```
@Table 指定数据库表名及选项;@Column 定义实体字段与数据库表列的映射关系。
```java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users") // 指定表名
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 主键生成策略
private Long id;
@Column(name = "username") // 指定数据库列名
private String username;
@Column(name = "email", nullable = false) // 邮箱列,不允许为空
private String email;
@Column(length = 100) // 限制密码列长度为100字符
private String password;
// 省略getter和setter方法
}
```
1. name: 指定数据库表中的列名如果不提供默认使用属性名
2. nullable: 指定该列是否允许null值默认为true允许)。
3. length: 设置列的最大长度适用于字符串类型
4. unique: 指定该列是否需要唯一约束默认为false
5. updatable: 指定在更新操作时该字段是否可更新默认为true
6. insertable: 指定在插入操作时该字段是否可插入默认为true
7. columnDefinition: 允许直接提供SQL片段来定义列覆盖默认的列定义
8. precision scale: 用于数值类型precision指总精度scale指小数点后的精度
# 如果实体类中有应该属性在对应的数据库表中没有该字段
1. 使用@Transient注解
2. MyBatis Plus中的@TableField(exist = false)
# 使用swagger3的步骤
1. 添加依赖
```java
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.5.0</version>
</dependency>
```
2. 写配置类:
配置类文件
3. 地址端口
```
springdoc.api-docs.path=/v3/api-docs
springdoc.api-docs.enabled=true
springdoc.swagger-ui.enabled=true
springdoc.swagger-ui.path=/swagger-ui/index.html
```
/**
* Swagger的配置类
*/
/**
* Swagger配置类该类里面的应该是固定的主要用来设置文档的主题信息比如文档的大标题副标题公司名
*/
@Configuration//托管spring
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30) // v2 不同
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.springbootmybatisplus"))//扫描该包下面的API注解
.build();
}
}
```