Table of contents
problem analysis
In the SpringBoot project, when creating an entity class, although there are only a few member variables in the entity class, the methods such as set/get/toString are fixed and cumbersome
package com.example.pojo;
// 创建实体类User用于封装查询的数据
public class User {
private Integer id;
private String name;
private short age;
private short gender;
private String phone;
// 创建set、get方法
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public short getAge() {
return age;
}
public void setAge(short age) {
this.age = age;
}
public short getGender() {
return gender;
}
public void setGender(short gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
// 创建toString()方法
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", phone='" + phone + '\'' +
'}';
}
// 创建构造方法
public User(Integer id, String name, short age, short gender, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
}
public User() {
}
}
The above problems can be solved by lombok
lombok
- lombok is a Java class library that can automatically generate constructors, getter/setter, equals, hashcode, toString and other methods in the form of annotations, and can automatically generate log variables to simplify Java development and improve efficiency.
-
@Getter
and@Setter
: Automatically generate getter and setter methods for the class. -
@ToString
: The toString() method of the auto-generated class. -
@EqualsAndHashCode
: Automatically generate the equals() and hashCode() methods of the class. -
@NoArgsConstructor
: Automatically generate a parameterless constructor -
@RequiredArgsConstructor
: Automatically generate constructors for fields that contain final or @NonNull annotations. -
@AllArgsConstructor
: Automatically generate a constructor that includes all fields -
@Data
: Including the combined annotations of@ToString
,@EqualsAndHashCode
,@Getter
and@Setter
, excluding the two annotations of no-argument construction and full-argument construction -
@Builder
: Provides a chain call method to create objects -
@Slf4j
: auto-generated logger -
@Cleanup
: Automatically handle resource closing operations, such as releasing files or database connections -
@Value
: Create an immutable class, all fields are final -
@NonNull
: The tag field cannot be empty, and the corresponding null check code will be generated- The specific usage code of the annotation is as follows
-
package com.example.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; // 创建实体类User用于封装查询的数据 @Data @NoArgsConstructor @AllArgsConstructor public class User { private Integer id; private String name; private short age; private short gender; private String phone; }
-