问题起源
我们每次使用Java实体类的时候,总是要写那些万变不离其宗的满参啊,空参啊,set/get方法等等,虽然可以自动生成,但是就是一个体力活,Lombok插件就可以使我们脱离这些烦恼,并且当你加属性的时候,也不需要在重新写set/get方法
在maven项目中的pom文件中添加依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
然后刷新让maven自己将需要的jar包导入,下图是在IDEA中的操作
进行上面俩步即可设置完成
IDEA实践
然后直接写完属性变量,加一个注解在上面,这个类就可以直接使用了,跟正常使用无区别,我以为万事大吉,谁知道出了问题
import lombok.Data;
@Data
publicclassUser{
private int id;
private String username;
private String password;
private String gender;
private String addr;
}
测试类
classtest{
public static void main(String [] args){
User user = new User();
user.setAddr("中国北京");
System.out.println(user);
}
}
新的问题并解决
但是我发现,没有带参的构造方法 只有Java自己默认给的空参构造方法,也就是传参是就是只能使用set方法,那样的话好苦逼啊,然后我去光了一圈博客,解决了问题
加上下面的一些注解就可以实现只写属性,全参,空参,set方法/get方法这些方法
@Data:注解在类上,将类提供的所有属性都添加get、set方法,并添加、equals、canEquals、hashCode、toString方法
@NoArgsConstructor:创建一个无参构造函数
@AllArgsConstructor:创建一个全参构造函数
测试代码
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@Data
@NoArgsConstructor
public class User {
private int id;
private String username;
private String password;
private String gender;
private String addr;
}
class test{
public static void main(String[] args) {
//可使用满参
User user = new User(1,"张无忌","12345","男","光明顶");
//可使用空参
User ss = new User();
//也可以使用set/get方法
ss.setAddr("北极");
System.out.println(user);
}
}
这个类跟其他正常写的实体类无差别,生成的class文件也是预期结果
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.itheima.pojo;
public class User {
private int id;
private String username;
private String password;
private String gender;
private String addr;
public User(int id, String username, String password, String gender, String addr) {
this.id = id;
this.username = username;
this.password = password;
this.gender = gender;
this.addr = addr;
}
public int getId() {
return this.id;
}
public String getUsername() {
return this.username;
}
public String getPassword() {
return this.password;
}
public String getGender() {
return this.gender;
}
public String getAddr() {
return this.addr;
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setAddr(String addr) {
this.addr = addr;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof User)) {
return false;
} else {
User other = (User)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getId() != other.getId()) {
return false;
} else {
label61: {
Object this$username = this.getUsername();
Object other$username = other.getUsername();
if (this$username == null) {
if (other$username == null) {
break label61;
}
} else if (this$username.equals(other$username)) {
break label61;
}
return false;
}
label54: {
Object this$password = this.getPassword();
Object other$password = other.getPassword();
if (this$password == null) {
if (other$password == null) {
break label54;
}
} else if (this$password.equals(other$password)) {
break label54;
}
return false;
}
Object this$gender = this.getGender();
Object other$gender = other.getGender();
if (this$gender == null) {
if (other$gender != null) {
return false;
}
} else if (!this$gender.equals(other$gender)) {
return false;
}
Object this$addr = this.getAddr();
Object other$addr = other.getAddr();
if (this$addr == null) {
if (other$addr != null) {
return false;
}
} else if (!this$addr.equals(other$addr)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof User;
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 + this.getId();
Object $username = this.getUsername();
result = result * 59 + ($username == null ? 43 : $username.hashCode());
Object $password = this.getPassword();
result = result * 59 + ($password == null ? 43 : $password.hashCode());
Object $gender = this.getGender();
result = result * 59 + ($gender == null ? 43 : $gender.hashCode());
Object $addr = this.getAddr();
result = result * 59 + ($addr == null ? 43 : $addr.hashCode());
return result;
}
public String toString() {
return "User(id=" + this.getId() + ", username=" + this.getUsername() + ", password=" + this.getPassword() + ", gender=" + this.getGender() + ", addr=" + this.getAddr() + ")";
}
public User() {
}
}
Lombok的优缺点
优点:
能通过注解的形式自动生成构造器、getter/setter、equals、hashcode、toString等方法,提高了一定的开发效率
让代码变得简洁,不用过多的去关注相应的方法
属性做修改时,也简化了维护为这些属性所生成的getter/setter方法等
缺点:
不支持多种参数构造器的重载
虽然省去了手动创建getter/setter方法的麻烦,但大大降低了源代码的可读性和完整性,降低了阅读源代码的舒适度