前后端开发常用的一些注解
controller
controller层 | 作用 | 例子 | 包 | 用法 |
---|---|---|---|---|
@Api | 用于生成接口文档方便于开发 | @Api(tags = {“Login”}, description = “相关接口”) | io.swagger.annotations.Api | 在controller类上 |
@Slf4j | 用于日志输出 | @Slf4j | lombok.extern.slf4j.Slf4j | 在controller类上 |
@RestController | 相当于@ResponseBody + @Controller合在一起(无法放回jsp页面,放回json数据) | @RestController | org.springframework.web.bind.annotation.* | 在controller类上 |
@RequestMapping![]() | 用来处理请求地址映射的注解,可用于类或方法上 | @RequestMapping(value = “/myBag”, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) | org.springframework.web.bind.annotation.* | 用于类或方法 |
@Validated | 用来校验数据,如果数据异常则会统一抛出异常 | @Validated | org.springframework.validation.annotation.Validated | 在controller类上 |
@Autowired | 注入属性 | @Autowired | org.springframework.web.bind.annotation.* | 用于属性上 |
@Resources | 注入bean | @Resources | org.springframework.web.bind.annotation.* | 用于方法上 |
@ApiOperation | 用于生成接口文档方便于开发 | @ApiOperation(value = “查询所有目录(分页)”) | io.swagger.annotations.Api | 用于方法上 |
@PostMapping | 访问时的url | @PostMapping("/page") | org.springframework.web.bind.annotation.* | 用于方法 |
@RequestParam | 配置参数(可以连用) | @RequestParam(“id”) int id, @RequestParam(“fieldName”) String fieldName | org.springframework.web.bind.annotation.* | 用于参数配置(用于get,post) |
@RequestBody | 配置参数 | @RequestBody DmdImplParam implParam | org.springframework.web.bind.annotation.* | 用于参数配置(用于post) |
impl
Service层 | 作用 | 例子 | 包 | 作用 |
---|---|---|---|---|
@Service | 注入bean | @Service | org.springframework.stereotype.Service | 用于类 |
@Autowired | 注入属性 | @Autowired | org.springframework.web.bind.annotation.* | 用于属性上 |
@Value | 注入一些在配置文件的数据 | @Value("${image}") | org.springframework.web.bind.annotation.* | 一般较多用于服务层 |
mapper
mapper层 | 作用 | 列子 | 包 | 作用 |
---|---|---|---|---|
@Mapper | 注入bean | @Mapper | @Service | org.springframework.stereotype.Service |
@Param | 作用参数 | @Param(“cataTitle”) String cataTitle, @Param(“deptCode”) String deptCode | org.apache.ibatis.annotations.* | 用于方法的参数(可以设置多个) |
entity
实体层 | 作用 | 列子 | 包 | 作用 |
---|---|---|---|---|
@Pattern | 可以对一些实体进行正则校验 | @Pattern(regexp = Regex.name, message = “姓名长度不得超过50字”) | javax.validation.constraints.* | 用于实体类(Regex接口编写的正则表达式字符串) |
其他
实体层 | 作用 | 列子 | 包 | 作用 |
---|---|---|---|---|
@ConfigurationProperties | 获取配置文件中的数据 | @ConfigurationProperties(prefix = “ridden”) private String url; | org.springframework.boot.context.properties.* | 用户获取配置文件的一些值 |
小白刚接触不久为了记得深点做点笔记,希望对和小白一样的有帮助