在SpringBoot使用过程中,@data注解十分好用
平时我们定义一个实体类的话,需要重写它的构造方法、get、set、toString、hashcoe、equals等方法,这都占用了大量的篇幅
而使用@Data注解则可以自动装配这些方法
我们先看一下源码

@Data is a convenient shortcut annotation that bundles the features of
@ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together: 
In other words, @Data generates all the boilerplate that is normally
associated with simple POJOs (Plain Old Java Objects) and beans: 
getters for all fields, setters for all non-final fields, and appropriate toString, 
equals and hashCode implementations that involve the fields of the class, 
and a constructor that initializes all final fields, 
as well as all non-final fields with no initializer that have been marked with @NonNull, 
in order to ensure the field is never null.

翻译一下就是
@Data是一种方便的快捷注释,它将@ToString、@EqualsAndHashCode、@Getter/@Setter和@RequiredArgsConstructor的功能捆绑在一起:换句话说,@Data生成通常与简单POJO(普通旧Java对象)和bean关联的所有样板文件:所有字段的Getter,所有非最终字段的Setter,以及适当的toString、equals和hashCode实现,这些实现涉及类的字段,以及初始化所有最终字段的构造函数,以及所有没有使用@NonNull标记的初始化器的非最终字段,以确保字段永远不为null。
是不是十分的好用

它也相当是以下注解的集合,六合一,真的强

   @see Getter
 * @see Setter
 * @see RequiredArgsConstructor
 * @see ToString
 * @see EqualsAndHashCode
 * @see lombok.Value

还有一个很重要,使用这个注解需要安装一个Lombok插件,就是下图所示这个
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐