一、前言
- 文章记录的注解是自身开发中用得相对较多的注解,后续不断补充完善。从事开发不久,也刚开始编写博客,内容输出以日常积累为主,各位前辈若看到文章,发现什么错误 或 有什么意见建议,还请不吝指教,万分感谢!
二、注解
@Controller
- 使用场景:用于控制层,表示类为控制器。不过在开发中,一般使用@RestController,该注解相当于 @RequestBody + @Controller
- 其中@ResponseBody 的作用是将 接口 的 方法返回 封装成 Body
@Service
@Repository
- 使用场景:用于持久层,即数据访问层,表示类为一个 DAO 类。
@RequestMapping
- 使用场景:用于 web 请求的路径映射,该注解包含多个参数如下,介绍几个可能会用上的:
value
和 path
:均用于指定路径,选用其一即可。
- 例如:@RequestMapping(value = “/path”)
method
:指定 Http 的请求方式,包括:GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE。
- 例如:@RequestMapping(method = RequestMethod.GET)
params
:映射请求的参数。
- 例如:@RequestMapping( params={“userName=zhangsan”}),请求路径必须包含参数名 userName、参数值:zhangsan
consumes
:指定一个或多个前端给后端的媒体类型,其中一个必须与请求的 “Content-Type” 报头匹配。
- 例如:@RequestMapping(consumes = {“text/plain”, “application/json”}
produces
:指定一个或多个后端给给前端的媒体类型,其中一个必须通过与请求的 “Accept” 包头匹配。
- 例如:@RequestMapping(produces = {“application/json”}}
@PathVariable
@GetMapping(value = "/user/{id}", produces = {"application/json"})
public AjaxResult getUserById(@PathVariable("id") Long id) {
return AjaxResult.success(userService.getUserById(id));
}
@RequestPram
@GetMapping(value = "/user", produces = {"application/json"})
public AjaxResult getUserById(@RequestParam("id") Long id) {
return AjaxResult.success(userService.getUserById(id));
}
@PostMapping
- 使用场景:指定请求路径,一般用于新增数据,相当于 @RequestMapping(method = RequestMethod.POST)
- 该注解通常与 @RequestBody 配合接收请求参体
- 不支持幂等
@PutMapping
- 使用场景:指定请求路径,一般用于更新数据,相当于 @RequestMapping(method = RequestMethod.PUT)
- 支持幂等(幂等,即重复调用接口的结果和单次调用的结果一致)
@DeleteMapping
- 使用场景:指定请求路径,一般用于删除数据,相当于 @RequestMapping(method = RequestMethod.Delete)
- 支持幂等
@GetMapping
- 使用场景:指定请求路径,一般用于获取数据,相当于 @RequestMapping(method = RequestMethod.GET)
- 该注解通常与 @PathVarible 或 @RequestParam 配合接收请求参数
- 支持幂等
-
@Value
- 使用场景:用于从 yml 配置中读取属性
- 使用示例:
# yml 文件配置
server:
port: 9020
# 在代码中读取
class ConfigurationReadExplain {
@Value("${server.port}")
private Long serverPort;
}
@ConfigurationProperties
- 使用场景:用于批量注入配置文件中的属性,功能类似@Value
- 使用示例:
# yml 文件配置
read:
app-id: 123
secret: abc
# 在代码中配置
@Data
@Component
@ConfigurationProperties(prefix = "read")
public class ConfigurationReadExplain {
private Long appId;
private String secret;
}
# 在代码中使用
public class ConfigurationUseSample {
@Autowrie
ConfigurationReadExplain configurationReadExplain;
public void getConfiguration() {
Long appId = configurationReadExplain.getAppId();
String secret = configurationReadExplain.getSecret();
}
}
@JsonIgnore
- 使用场景:一般用于标记属性,使其不参与序列化和反序列化,也就是说:被标记的属性不在接口返回中显示
@JsonInclude
- 使用场景:一般用于指明属性何时被序列化,以减少要回传的属性数量
- 例如:@JsonInclude(JsonInclude.Include.NON_NULL) 表示属性不为空时才被序列化,即属性为空值是不在返回中显示
@JsonFormat
- 使用场景:设置时间格式,表示返回给前端的数据——后台到前台
- 例如:@JsonFormat(pattern = “YYYY-MM-dd HH:mm:ss”, timezone = “GMT+8”)
@DateTimeFormat
- 使用场景:设置时间格式,表示接收的前端数据格式——前台到后台
- 例如:@DateTimeFormat(pattern = “yyyy-MM-dd”)
@TableName
- 使用场景:该注解为 Mybatis 的注解,注解在类上,用于使实体类关联上对应的表,value 若未写,默认根据类名找表
- 例如:@TableName(value = “mc_user”)
@TableField
- 使用场景:
- 该注解为 Mybatis 的注解,注解在属性上,用于使属性关联上表字段。若类上标记了@TableName,该注解可以不用写,默认根据属性名匹配。
- 若在 POJO 类中声明了未在表中存在得到属性,则可用 @TableField(exist = false) 标记
@Options
- 使用场景:该注解为 Mybatis 的注解,一般注解于 set 方法,设置id自增
- 例如:@Options(useGeneratedKeys=true, keyProperty=“id”)