一、如何使用
1. 添加依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
2.配置插件
idea配置插件(不然会各种飘红)
二、常用注解
1.@Getter @Setter
使用范围: 1. 类上方 (所有属性自动注入) 2. 属性字段上方
public class LomBokTestEntiy {
@Getter
@Setter
private String id;
@Setter
private String name;
}
查看对应的class文件
public class LomBokTestEntiy {
private String id;
private String name;
public LomBokTestEntiy() {
}
//自动生成
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
- @Data 聚合注解
使用方式类上方,相当于 @ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor。
此注解可以明显看出代码的简洁程度 ~
@Data
public class LomBokTestEntiy {
private String id;
private String name;
}
查看对应的LomBokTestEntiy.class文件
public class LomBokTestEntiy {
private String id;
private String name;
public LomBokTestEntiy() {
}
public String getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof LomBokTestEntiy)) {
return false;
} else {
LomBokTestEntiy other = (LomBokTestEntiy)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id != null) {
return false;
}
} else if (!this$id.equals(other$id)) {
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(Object other) {
return other instanceof LomBokTestEntiy;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.getId();
int result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
public String toString() {
return "LomBokTestEntiy(id=" + this.getId() + ", name=" + this.getName() + ")";
}
}
- @ToString 、 @EqualAndHashCode、 @NoArgsConstructor @RequiredArgsConstructor 、 @AllArgsConstructor
自动生成toString(),equal(),hash(),无参构造,指定参数构造,全部参数构造。
使用方式在class上方。
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class LomBokTestEntiy {
private String id;
private String name;
}
查看对应的class文件,注意一个有意思的事情,自动生成的方法顺序和注解的顺序保持一致。
public class LomBokTestEntiy {
private String id;
private String name;
public String toString() {
return "LomBokTestEntiy(id=" + this.getId() + ", name=" + this.getName() + ")";
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof LomBokTestEntiy)) {
return false;
} else {
LomBokTestEntiy other = (LomBokTestEntiy)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id != null) {
return false;
}
} else if (!this$id.equals(other$id)) {
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(Object other) {
return other instanceof LomBokTestEntiy;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.getId();
int result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
public LomBokTestEntiy() {
}
public LomBokTestEntiy(String id, String name) {
this.id = id;
this.name = name;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return this.id;
}
public String getName() {
return this.name;
}
}
- @RequiredArgsConstructor @NonNull
有参构造注解,在类上方。 非空注解可用于属性上方。
备注: 有参构造函数,指定参数。 需要在对应的属性添加@NonNull注解。
@Setter
@Getter
//staticName 是创建一个静态builder方法,返回类对象.构造函数设置为private。
@RequiredArgsConstructor(staticName = "builder")
public class LomBokTestEntiy {
public LomBokTestEntiy() {
}
@NonNull
private String id;
@NonNull
private String name;
private String age;
}
查看对应的class文件
import lombok.NonNull;
public class LomBokTestEntiy {
@NonNull
private String id;
@NonNull
private String name;
private String age;
public LomBokTestEntiy() {
}
public void setId(@NonNull String id) {
if (id == null) {
throw new NullPointerException("id is marked @NonNull but is null");
} else {
this.id = id;
}
}
public void setName(@NonNull String name) {
if (name == null) {
throw new NullPointerException("name is marked @NonNull but is null");
} else {
this.name = name;
}
}
public void setAge(String age) {
this.age = age;
}
@NonNull
public String getId() {
return this.id;
}
@NonNull
public String getName() {
return this.name;
}
public String getAge() {
return this.age;
}
//注意不同
private LomBokTestEntiy(@NonNull String id, @NonNull String name) {
if (id == null) {
throw new NullPointerException("id is marked @NonNull but is null");
} else if (name == null) {
throw new NullPointerException("name is marked @NonNull but is null");
} else {
this.id = id;
this.name = name;
}
}
// static
public static LomBokTestEntiy builder(@NonNull String id, @NonNull String name) {
return new LomBokTestEntiy(id, name);
}
}
- @Value
使用位置: 类上方
作用: 生成一个final类,类里面的属性以及方法 和@Data类似 。
@Value
public class TestValues {
String age;
String id;
}
对应的class
public final class TestValues {
private final String age;
private final String id;
public TestValues(String age, String id) {
this.age = age;
this.id = id;
}
public String getAge() {
return this.age;
}
public String getId() {
return this.id;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof TestValues)) {
return false;
} else {
TestValues other = (TestValues)o;
Object this$age = this.getAge();
Object other$age = other.getAge();
if (this$age == null) {
if (other$age != null) {
return false;
}
} else if (!this$age.equals(other$age)) {
return false;
}
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id != null) {
return false;
}
} else if (!this$id.equals(other$id)) {
return false;
}
return true;
}
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $age = this.getAge();
int result = result * 59 + ($age == null ? 43 : $age.hashCode());
Object $id = this.getId();
result = result * 59 + ($id == null ? 43 : $id.hashCode());
return result;
}
public String toString() {
return "TestValues(age=" + this.getAge() + ", id=" + this.getId() + ")";
}
}
5、@Builder
生成一个内部builder类。可作用于类上方、方法、属性;
真正使用的场景还是在类上好理解。
@Builder
public class TestBuilder {
private String id;
private String name;
@Builder.Default private long ts = System.currentTimeMillis();
}
对应的class文件
public class TestBuilder {
private String id;
private String name;
private long ts;
private static long $default$ts() {
return System.currentTimeMillis();
}
TestBuilder(String id, String name, long ts) {
this.id = id;
this.name = name;
this.ts = ts;
}
public static TestBuilder.TestBuilderBuilder builder() {
return new TestBuilder.TestBuilderBuilder();
}
public static class TestBuilderBuilder {
private String id;
private String name;
private boolean ts$set;
private long ts;
TestBuilderBuilder() {
}
public TestBuilder.TestBuilderBuilder id(String id) {
this.id = id;
return this;
}
public TestBuilder.TestBuilderBuilder name(String name) {
this.name = name;
return this;
}
public TestBuilder.TestBuilderBuilder ts(long ts) {
this.ts = ts;
this.ts$set = true;
return this;
}
public TestBuilder build() {
long ts = this.ts;
if (!this.ts$set) {
ts = TestBuilder.$default$ts();
}
return new TestBuilder(this.id, this.name, ts);
}
public String toString() {
return "TestBuilder.TestBuilderBuilder(id=" + this.id + ", name=" + this.name + ", ts=" + this.ts + ")";
}
}
}
- @Log
日志组件,可以生成一个log对象,直接使用。
可作用于类&方法
@Slf4j
public class TestLog {
public void log(){
log.info("test");
}
}
对应的class文件
public class TestLog {
private static final Logger log = LoggerFactory.getLogger(TestLog.class);
public TestLog() {
}
public void log() {
log.info("test");
}
}
三、小结
1.lombok主要目的是代码简化,但是需要在易于review和简化之间找平衡点。