lombok项目官网:https://projectlombok.org/
下面主要是针对Maven项目的使用:
- 在Pom中添加Jar依赖
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
- Pom中添加lombok插件
<!-- Getter/Setter注解插件 -->
<build>
<plugins>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.4.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- Pom中添加如下属性
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
如果没有添加该属性,会报一个类似如下的错误:
The parameters ‘encoding‘ for goal org.projectlombok:lombok-maven-plugin...
- 示例:
实体类:
import lombok.Getter;
import lombok.Setter;
public class User {
@Getter @Setter private String id;
@Getter @Setter private String name;
}
测试类:
import lombok.Getter;
public class Test {
@Getter User u;
public static void main(String[] args) {
User u = new User();
u.setId("123");
u.setName("test");
System.out.println(u.getId());
System.out.println(u.getName());
}
}
运行结果:
123
test