引入lombok依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
实体类添加注解
@Data//生成get、set、equals、hashCode和toString
@Builder//生成builder内部类
public class User {
private int id;
private String username;
private String passwd;
private boolean sex;
private Date birthday;
}
测试用例
public class BuildTest {
public static void main(String[] args) {
User user = User.builder().id(1)
.username("张三")
.passwd("123456")
.birthday(new Date())
.sex(false).build();
System.out.println(user);
}
}
打印效果