以前的java实体类,代码中充斥着各种的get,set,构造等方法,这些代码既浪费时间又影响代码的美观,于是Lombok工具就问世了,使用Lombok可以用注解的方式取代这些方法。
1、引入Lombok的Maven包
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2、安装IDEA支持组件,不然编译不通过
3、编写代码
package com.mhi.cdr.base.commons.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data // 实现了:1、所有属性的get和set方法;2、toString 方法;3、hashCode方法;4、equals方法
@Builder // 建造者模式
@NoArgsConstructor // 无参构造函数
@AllArgsConstructor // 有参构造函数
public class Student {
// 主键ID
private Long id;
// 学号
private String code;
// 姓名
private String name;
}
4、这样,一个完整的实体了创建好了,我们可以去找到这个实体编译后的class文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.mhi.cdr.base.commons.dto;
public class Student {
private Long id;
private String code;
private String name;
public static Student.StudentBuilder builder() {
return new Student.StudentBuilder();
}
public Long getId() {
return this.id;
}
public String getCode() {
return this.code;
}
public String getName() {
return this.name;
}
public void setId(final Long id) {
this.id = id;
}
public void setCode(final String code) {
this.code = code;
}
public void setName(final String name) {
this.name = name;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Student)) {
return false;
} else {
Student other = (Student)o;
if (!other.canEqual(this)) {
return false;
} else {
label47: {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id == null) {
break label47;
}
} else if (this$id.equals(other$id)) {
break label47;
}
return false;
}
Object this$code = this.getCode();
Object other$code = other.getCode();
if (this$code == null) {
if (other$code != null) {
return false;
}
} else if (!this$code.equals(other$code)) {
return false;
}
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof Student;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.getId();
int result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $code = this.getCode();
result = result * 59 + ($code == null ? 43 : $code.hashCode());
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
public String toString() {
return "Student(id=" + this.getId() + ", code=" + this.getCode() + ", name=" + this.getName() + ")";
}
public Student() {
}
public Student(final Long id, final String code, final String name) {
this.id = id;
this.code = code;
this.name = name;
}
public static class StudentBuilder {
private Long id;
private String code;
private String name;
StudentBuilder() {
}
public Student.StudentBuilder id(final Long id) {
this.id = id;
return this;
}
public Student.StudentBuilder code(final String code) {
this.code = code;
return this;
}
public Student.StudentBuilder name(final String name) {
this.name = name;
return this;
}
public Student build() {
return new Student(this.id, this.code, this.name);
}
public String toString() {
return "Student.StudentBuilder(id=" + this.id + ", code=" + this.code + ", name=" + this.name + ")";
}
}
}
反编译后不难发现,注解帮我们实现了get,set,equals,hasCode,toString,无/有参构造函数,还有StudentBuilder内部类。
5、使用建造者模式编写代码
public static void main(String[] args) {
Student student = Student.builder().name("张三").code("100001").build();
}
这样,代码是不是简洁了很多
6、不能使用Lombok的情况
可能有些公司的规范不给用Lombok插件,怎么办,是不是就只能手写了?其实这个插件还提供了一个将注解替换回代码的功能,以上面的实体为例子,只要在IDEA中右键这个实体选择Delombok,就能将注解替换掉,这样你也不用写重复的代码了。
点了后就变成下图这样了