Lombok
解释:
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
重点:
- java library Java库
- plugs 插件
- build tools 构建工具
- with one annotation your class 在类上打上注解
1.使用步骤
- 在IDEA中安装Lombok插件
- File–>Settings–>plugins–>搜索Lombok下载
- 在项目中导入lombok的jar包
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
- 在实体类上加上注解即可
@Data//无参构造,get set toString hashCode,equals...
@AllArgsConstructor//有参构造,发现无参构造没了 显示的定义了有参构造之后 必须显示定义无参构造
@NoArgsConstructor//无参构造
/**
* 实体类
*/
@Data//无参构造,get set toString hashCode,equals...
@AllArgsConstructor//有参构造,发现无参构造没了 显示的定义了有参构造之后 必须显示定义无参构造
@NoArgsConstructor//无参构造
public class User {
private int id;
private String name;
private String password;
//也还可以自己加
}
其他注解:
@Getter and @Setter
@FieldNameConstants
@ToString
@EqualsAndHashCode
@AllArgsConstructor, @RequiredArgsConstructor and @NoArgsConstructor
@Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog, @Flogger
@Data
@Builder
@Singular
@Delegate
@Value
@Accessors
@Wither
@SneakyThrows
比较常见的:
@Data:无参构造,get、set、tostring、hashcode,equals
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@ToString
@Getter