java开发中注解汇总

发布于:2025-02-11 ⋅ 阅读:(58) ⋅ 点赞:(0)
注解 作用 位置 注意
mybatis

@Data

@Getter
@Setter
@ToString
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor

@Data 代替:无参构造,get,set,toString,hashCode,equals

@Getter
@Setter

可放在类和方法上;其他放在

实体类上

安装Lombok插件

在项目中导入lombok的jar包

mybatis

@Select

@Update

@Insert

@Delete

接口方法上 xml文件中绑定使用了该注解的接口的类
mybatis @Param() 接口方法的形参类型前 基本类型需要,自定义实体类不需要
@SuppressWarnings("all") 让代码中的警告不显示 类上

spring

自动装配

@Autowired 自动装配:类型>名字 属性或set方法上 开启注解扫描支持

spring

自动装配

@Qualifier(value="xxx") 自动装配,搭配@Autowired使用 属性上

开启注解扫描支持

value的值为bean的id

和@Autowired配合使用

spring

自动装配

@Resource 自动装配:名字>类型 属性上

开启注解扫描支持

① 保证有一个bean的id为实体类的名字;② 保证class的属性值指向的实体类唯一。

spring

自动装配

@Nullable 属性上标记了这个注解,说明这个属性可以为null。 属性上 开启注解扫描支持

spring

注册bean

@Component

相当于bean.xml中注册一个bean

说明这个类被Spring管理了,就是bean!

类上 开启注解扫描支持
spring注册bean @Repository 将内容注册到Spring中 dao层类上

开启注解扫描支持

用在dao/mapper层

spring注册bean @Service 将内容注册到Spring中 service层类上

开启注解扫描支持

用在service层

spring注册bean @Controller 将内容注册到Spring中 controller层类上

开启注解扫描支持

用在controller层

会走视图解析器

spring @Value("")

相当于bean.xml中注册bean时给属性赋值

给属性赋值

属性上 开启注解扫描支持

spring

作用域

@Scope 实体类上 开启注解扫描支持
spring @Configuration 代表这是一个配置类,等同于之前的beans.xml
spring @Bean 等同于之前的beans.xml中注册一个bean 方法上 方法名即之前的xml中的bean的id。
spring @ComponentScan("com.wlp.pojo") 扫描实体类所在的包 配置类上
spring @Import(UserConfig2.class) 将其他的配置类整合到当前配置类中 配置类上 UserConfig2是User的另一个配置类,该类同样需要声明@Configuration, 通过@Import整合到UserConfig类中
spring @Aspect 标注这个类就是一个切面 类上
spring @Before("execution(* com....)") 方法上
spring @After("execution(* com...)") 方法上
spring @Around("execution(* com...)") 方法上
springmvc @RequestMapping 该注解用于映射url到控制器类或一个特定的处理程序方法。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。 类或方法上
springmvc

@GetMapping

@PostMapping

@PutMapping

@DeleteMapping

@PatchMapping

路径请求 方法上
springmvc @PathVariable 让方法参数的值对应绑定到一个URI模板变量上。/{}/{} 方法形参前
springmvc @RequestParam("username") username是提交的域的名称
springmvc @ResponseBody 配合@Controller使用,不再走视图解析器,返回的字符串不再拼接前后缀,而是直接返回字符串 方法上

直接在浏览器输入url请求,返回字符串,不再走jsp等页面

springmvc @RestController 直接返回字符串,不会被视图解析器解析 类上 代替@Controller写在类上,所有方法都返回字符串,不再走视图解析器(前后端分离的时候都会用)
@ResponseBody+@Controller = @RestController
SpringBoot @ConfigurationProperties 开启yml文件给实体类属性赋值时的自动提示 实体类上 需要setting中Annotation Processor开启“Enable annotation processor”
@Validated 与@ConfigurationProperties组合进行数据校验 实体类上 需要引入依赖spring-boot-starter-validation才能起作用。
@Log4j 输出日志 类上 需要安装lombok插件以及lombok依赖
@RequestBody 将对象转换为JSON字符串 对象形参前面


网站公告

今日签到

点亮在社区的每一天
去签到