Intellij IDEA 安装 lombok (本地安装/在线安装)及使用详解

一 本地安装lombok

1.下载安装lombok zip包

下载地址:https://github.com/mplushnikov/lombok-intellij-plugin/releases

2.下载好后放在任意盘下

3.进入 file->settings 

勾选

4.进入 Plugins - >Install plugin from disk…

选择下载的zip包

5.选择后点击ok,然后重启idea即可

二 在线安装lombok

1.进入 file->settings -> Plugins

5.结束后重启idea即可

三 lombok的使用

1.pom文件加入

  1. <dependency>
  2. <groupId>org.projectlombok</groupId>
  3. <artifactId>lombok</artifactId>
  4. <version>1.18.4</version>
  5. </dependency>

2.@Data 注解

@Data注解在类上,会为类的所有属性自动生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。

官方实例如下:

import lombok.AccessLevel;
import lombok.Setter;
import lombok.Data;
import lombok.ToString;
@Data
public class DataExample {
private final String name;
@Setter(AccessLevel.PACKAGE) private int age;
private double score;
private String[] tags;
@ToString(includeFieldNames=true)
@Data(staticConstructor="of")
public static class Exercise<T> {
private final String name;
private final T value;
}
}

如不使用Lombok,则实现如下:

import java.util.Arrays;

public class DataExample {
private final String name;
private int age;
private double score;
private String[] tags;

public DataExample(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

void setAge(int age) {
this.age = age;
}

public int getAge() {
return this.age;
}

public void setScore(double score) {
this.score = score;
}

public double getScore() {
return this.score;
}

public String[] getTags() {
return this.tags;
}

public void setTags(String[] tags) {
this.tags = tags;
}

@Override public String toString() {
return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")";
}

protected boolean canEqual(Object other) {
return other instanceof DataExample;
}

@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof DataExample)) return false;
DataExample other = (DataExample) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
if (this.getAge() != other.getAge()) return false;
if (Double.compare(this.getScore(), other.getScore()) != 0) return false;
if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
return true;
}

@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
final long temp1 = Double.doubleToLongBits(this.getScore());
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + this.getAge();
result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
result = (result*PRIME) + Arrays.deepHashCode(this.getTags());
return result;
}

public static class Exercise<T> {
private final String name;
private final T value;

private Exercise(String name, T value) {
this.name = name;
this.value = value;
}

public static <T> Exercise<T> of(String name, T value) {
return new Exercise<T>(name, value);
}

public String getName() {
return this.name;
}

public T getValue() {
return this.value;
}

@Override public String toString() {
return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")";
}

protected boolean canEqual(Object other) {
return other instanceof Exercise;
}

@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Exercise)) return false;
Exercise<?> other = (Exercise<?>) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;
if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;
return true;
}

@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode());
return result;
}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值