目录
1. lombok简介
官网简介:
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
Project Lombok 是一个 Java 库,可自动插入您的编辑器并构建工具,为您的 Java 增添趣味。
永远不要再编写另一个 getter 或 equals 方法,通过一个注释,您的类就有一个功能齐全的构建器,自动化您的日志变量等等。
官网地址:Project Lombok
2.lombok在idea中的使用
1.在idea的插件市场里搜索lombok,安装图片中的这个。
2.新建一个maven项目,选择quickstar即可,在pom文件中加入lombok的依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
3.新建一个实体类,这里我以Student为例
public class Student {
private int id;
private String name;
private String address;
private int age;
private String email;
}
4.加入lombok的注解
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
private String address;
private int age;
private String email;
}
说明:
@Data:作用相当于写了set,get,toString方法
/**
* Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check
* all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.
* <p>
* Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}.
* <p>
* Complete documentation is found at <a href="https://projectlombok.org/features/Data">the project lombok features page for @Data</a>.
*
* @see Getter
* @see Setter
* @see RequiredArgsConstructor
* @see ToString
* @see EqualsAndHashCode
* @see lombok.Value
*/
@AllArgsConstructor:全参构造方法
/**
* Generates an all-args constructor.
* An all-args constructor requires one argument for every field in the class.
* <p>
* Complete documentation is found at <a href="https://projectlombok.org/features/Constructor">the project lombok features page for @Constructor</a>.
* <p>
* Even though it is not listed, this annotation also has the {@code onConstructor} parameter. See the full documentation for more details.
*
* @see NoArgsConstructor
* @see RequiredArgsConstructor
*/
@NoArgsConstructor:无参构造方法
/**
* Generates a no-args constructor.
* Will generate an error message if such a constructor cannot be written due to the existence of final fields.
* <p>
* Complete documentation is found at <a href="https://projectlombok.org/features/Constructor">the project lombok features page for @Constructor</a>.
* <p>
* Even though it is not listed, this annotation also has the {@code onConstructor} parameter. See the full documentation for more details.
* <p>
* NB: Fields with constraints such as {@code @NonNull} will <em>NOT</em> be checked in a {@code @NoArgsConstructor} constructor, of course!
*
* @see RequiredArgsConstructor
* @see AllArgsConstructor
*/
@ToString:相当于toString方法,一般来讲,写了@Data就不用写这个了
/**
* Generates an implementation for the {@code toString} method inherited by all objects, consisting of printing the values of relevant fields.
* <p>
* Complete documentation is found at <a href="https://projectlombok.org/features/ToString">the project lombok features page for @ToString</a>.
*/
其他的注解可以自己去看
5.创建一个测试方法
import org.example.domain.Student;
import org.junit.Test;
public class MyTest {
@Test
public void test01(){
// 测试无参构造
Student student = new Student();
System.out.println(student);
// 测试全参构造
Student student1 = new Student(1001, "zhangsan", "china", 18, "123@qq.com");
System.out.println(student1);
// 测试set, get方法
Student student2 = new Student();
System.out.println(student2);
student2.setId(1002);
student2.setName("lisi");
System.out.println(student2);
}
}
输出结果
Student(id=0, name=null, address=null, age=0, email=null)
Student(id=1001, name=zhangsan, address=china, age=18, email=123@qq.com)
Student(id=0, name=null, address=null, age=0, email=null)
Student(id=1002, name=lisi, address=null, age=0, email=null)
3. lombok在eclipse中的使用
1.下载lombok的插件,网上有教程
Eclipse安装lombok插件 - boonya - 博客园
2.进行测试,大致步骤和idea类似,还是先创建一个maven的java项目,然后添加lombok的依赖进行测试。
4.总结
Lombok项目是一个Java库,它会自动插入编辑器和构建工具中,Lombok提供了一组有用的注释,用来消除Java类中的大量样板代码。仅五个字符(@Data)就可以替换数百行代码从而产生干净,简洁且易于维护的Java类。