Intellij IDEA中lombok插件的使用
- idea中开启对lombok插件的支持
下载lombok插件,在settings中
搜索lombok,然后再install,install好了之后再restart
- lombok的使用
在pom.xml文件中添加lombok的依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
在实体类中常用注解的使用,只需要在实体类中添加下列响应注解,lombok插件就能自动生成相应的方法
@Setter:注解在类上,该类所有的字段都生成set方法,注解在字段上,只为该字段生成set方法
@Getter:注解在类上,该类所有的字段都生成get方法,注解在字段上,只为该字段生成get方法
@Data:注解在类上;提供类所有属性的get和set方法,此外还提供了equals、canEqual、hashCode、toString方法
@ToString:注解在类上,生成toString方法
@EqualAndHashCode:注解在类上,生成equals、canEqual、hashCode方法
@NoArgsConstructor:注解在类上;生成一个无参的构造方法
@AllArgsConstructor:注解在类上;生成一个全参的构造方法
- 举例使用
在User实体类中添加@Data注解,相当于提供类所有属性的get和set方法,此外还提供了equals、canEqual、hashCode、toString方法
import lombok.*;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@Data
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
private String sex;
private String address;
}
在编译后的class文件中
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
private String sex;
private String address;
public User() { }
public Long getId() { return this.id;}
public String getName() { return this.name;}
public Integer getAge() { return this.age;}
public String getSex() { return this.sex;}
public String getAddress() { return this.address;}
public void setId(final Long id) { this.id = id;}
public void setName(final String name) { this.name = name;}
public void setAge(final Integer age) { this.age = age;}
public void setSex(final String sex) { this.sex = sex;}
public void setAddress(final String address) { this.address = address;}
public boolean equals(final 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 {
label71: {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id == null) {
break label71;
}
} else if (this$id.equals(other$id)) {
break label71;
}
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;
}
label57: {
Object this$age = this.getAge();
Object other$age = other.getAge();
if (this$age == null) {
if (other$age == null) {
break label57;
}
} else if (this$age.equals(other$age)) {
break label57;
}
return false;
}
Object this$sex = this.getSex();
Object other$sex = other.getSex();
if (this$sex == null) {
if (other$sex != null) {
return false;
}
} else if (!this$sex.equals(other$sex)) {
return false;
}
Object this$address = this.getAddress();
Object other$address = other.getAddress();
if (this$address == null) {
if (other$address == null) {
return true;
}
} else if (this$address.equals(other$address)) {
return true;
}
return false;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof User;
}
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());
Object $age = this.getAge();
result = result * 59 + ($age == null ? 43 : $age.hashCode());
Object $sex = this.getSex();
result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
Object $address = this.getAddress();
result = result * 59 + ($address == null ? 43 : $address.hashCode());
return result;
}
public String toString() {
return "User(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ", sex=" + this.getSex() + ", address=" + this.getAddress() + ")";
}
}
还没有评论,来说两句吧...