使用Lombok简化java代码

Lombok是一款提高javaer开发效率的插件工具,特别是在繁多是bean类中的getter、setter,使用Lombok可以使用注解的方式省去了添加getter、setter的时间

在使用Lombok之前,需要在IDE上安装插件,eclipse安装步骤稍微多一点,具体请自行百度或者去官方查看https://projectlombok.org/features/configuration,IntelliJ安装就相对来说比较方便,直接在插件中心搜索Lombok,安装Lombok Plugin即可。
接下来,我们在程序中引入jar包:

1
2
3
4
5
6
7
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>

我们平时的POJO类一般都是这样写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class DataFileDownloadAudit {
private Timestamp startTime;
private String userName;
private String operator;
private String source;
private String displayName;
private String filePath;
private long fileSize;
private String creator;
private Timestamp createTime;

public DataFileDownloadAudit() {
}

public String getUserName() {
return this.userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getOperator() {
return this.operator;
}

public void setOperator(String operator) {
this.operator = operator;
}

public String getSource() {
return this.source;
}

public void setSource(String source) {
this.source = source;
}

public String getDisplayName() {
return this.displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getFilePath() {
return this.filePath;
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}

public String getCreator() {
return this.creator;
}

public void setCreator(String creator) {
this.creator = creator;
}

public Timestamp getStartTime() {
return this.startTime;
}

public void setStartTime(Timestamp startTime) {
this.startTime = startTime;
}

public Timestamp getCreateTime() {
return this.createTime;
}

public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}

public long getFileSize() {
return this.fileSize;
}

public void setFileSize(long fileSize) {
this.fileSize = fileSize;
}
}

如果属性更多的话,添加getter、setter就需要花更多的时间。如果使用Lombok插件的话,不仅可以节省时间,而且代码也更加简洁:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class DataFileDownloadAudit {
@Setter(AccessLevel.PUBLIC)
@Getter(AccessLevel.PROTECTED)
private Timestamp startTime;

@Setter(AccessLevel.PUBLIC)
@Getter(AccessLevel.PROTECTED)
private String userName;
private String operator;
private String source;
private String displayName;
private String filePath;
private long fileSize;
private String creator;
private Timestamp createTime;

public DataFileDownloadAudit() {
}

}

这样只是给属性加getter、setter,我们也可以用@Data来继续简化:

1
2
3
4
5
6
7
8
9
10
11
12
@Data
public class DataFileDownloadAudit {
private Timestamp startTime;
private String userName;
private String operator;
private String source;
private String displayName;
private String filePath;
private long fileSize;
private String creator;
private Timestamp createTime;
}

这两种方式有什么差异呢?@Getter和@Setter是作用在bean属性上,可以自动生成getter、setter方法,@Data是作用在类上,他结合了@ToString, @EqualsAndHashCode, @Getter,@Setter,@RequiredArgsConstructor注解的功能。
这仅仅说了Lombok两个常用的注解,总的来说,它会给我们带来更加简洁的代码和快速的开发效率,其他更加强大的功能可以去https://projectlombok.org/features/all自行查看。

You forgot to set the qrcode for Alipay. Please set it in _config.yml.
You forgot to set the qrcode for Wechat. Please set it in _config.yml.
You forgot to set the business and currency_code for Paypal. Please set it in _config.yml.
You forgot to set the url Patreon. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×