Lombok的使用
依赖安装
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
常用注解使用
@Data:集成了@ToString, @EqualsAndHashCode, @Getter在所有字段和@Setter所有非最终字段上的快捷方式,以及 @RequiredArgsConstructor!
@NoArgsConstructor:生成无参构造
@AllArgsConstructor:生成全参构造
注:全参构造和toString的生成均为字段的先后顺序来
代码参考
实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private int testInt;
private String name;
private int age;
}
测试类
public static void main(String[] args) throws Exception {
Student stu=new Student();
stu=new Student(1,"hehe",2);
System.out.println(stu.hashCode());
System.out.println(stu.toString());
}
更多注解参考官网:https://projectlombok.org/features/all