天天看點

帶你快速入門 Java開發神器 lombok

在寫JavaBean對象時候 每次都要重複Alt+insert寫Getter/Setter toString 有參構造 和 無參構造 特别麻煩 浪費時間 今天給大家推薦神器lombok 官方支援的神器 可以幫您省去大量時間 讓代碼簡潔易讀

什麼是lombok

Project Lombok 是一個 java 庫,可自動插入您的編輯器和建構工具,為您的 java 增添趣味。

永遠不要再編寫另一個 getter 或 equals 方法,使用一個注釋,您的類就有一個功能齊全的建構器、自動化您的日志記錄變量等等。

安裝lombok

lombok安裝有兩種方法 一種是idea插件安裝 一種是導入lombok坐标

第一種:idea插件安裝

  • 去​

    ​File > Settings > Plugins​

  • 點選​

    ​Browse repositories...​

  • 搜尋​

    ​Lombok Plugin​

  • 點選​

    ​Install plugin​

  • 重新開機 IntelliJ IDEA
帶你快速入門 Java開發神器 lombok

第二種:導入lombok坐标(最新版本1.18.22)

<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>      

lombok的使用

以下是常用的lombok注解 其他注解參考文檔

@Data

最常用的注解 使用該注解 @Getter/@Setter @toString @NoArgsContructor @AllArgisConstructor 等

隻需要一個@Data 一共标準的JavaBean就生成了

例如

@Data
public class Demo {
    private String id;
    private String name;
}      

編譯後

public class Demo {
    private String id;
    private String name;

    public Demo() {
    }

    public String getId() {
        return this.id;
    }

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

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Demo)) {
            return false;
        } else {
            Demo other = (Demo)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$id = this.getId();
                Object other$id = other.getId();
                if (this$id == null) {
                    if (other$id != null) {
                        return false;
                    }
                } else if (!this$id.equals(other$id)) {
                    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;
                }

                return true;
            }
        }
    }

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

    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());
        return result;
    }

    public String toString() {
        return "Demo(id=" + this.getId() + ", name=" + this.getName() + ")";
    }
}      

@Setter

作用于該屬性上 自動預設生成Getter方法

@Setter
public class Demo {
    private String id;
    private String name;
}      
public class Demo {
    private String id;
    private String name;

    public Demo() {
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }
}      

@Getter

@Getter方法與@Setter方法一樣 這裡不描述了

@NonNull

作用于屬性或方法的參數上 用于做非空檢查 如果是空 抛出空指針異常

使用方法如下:

public class Demo {
    @NonNull
    private String id;
    private String name;
}      

編譯後效果:

public class Demo {
    @NonNull
    private String id;
    private String name;

    public Demo() {
    }
}      

@ToString

無需通過調試來檢視字段 隻需要添加@ToString 讓lombok自動生成

使用方法:

@ToString
public class Demo {
    private String id;
    private String name;
}      

編譯後效果:

public class Demo {
    private String id;
    private String name;

    public Demo() {
    }

    public String toString() {
        return "Demo(id=" + this.id + ", name=" + this.name + ")";
    }
}      

@AllArgsConstructor

作用于類上 自動生成一個全參構造方法

使用方法:

@AllArgsConstructor
public class Demo {
    private String id;
    private String name;
}      

編譯後效果:

public class Demo {
    private String id;
    private String name;

    public Demo(String id, String name) {
        this.id = id;
        this.name = name;
    }
}      

@NoArgsConstructor

跟@AllArgsConstructor效果一樣生成構造方法 隻不過是無參構造

@EqualsAndHashCode

作用于類上,生成equals、canEqual、hashCode方法。

使用方法:

@EqualsAndHashCode
public class Demo {
    private String id;
    private String name;
}      

編譯後效果:

public class Demo {
    private String id;
    private String name;

    public Demo() {
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Demo)) {
            return false;
        } else {
            Demo other = (Demo)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$id = this.id;
                Object other$id = other.id;
                if (this$id == null) {
                    if (other$id != null) {
                        return false;
                    }
                } else if (!this$id.equals(other$id)) {
                    return false;
                }

                Object this$name = this.name;
                Object other$name = other.name;
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                return true;
            }
        }
    }

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

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.id;
        int result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $name = this.name;
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        return result;
    }
}      

@RequiredArgsConstructor

作用于類上 生成被@NonNull 或 被final修飾的屬性的構造方法

使用方法:

@RequiredArgsConstructor
public class Demo {
    @NonNull
    private String id;
    private final String name;
}      

編譯後效果:

public class Demo {
    @NonNull
    private String id;
    private final String name;

    public Demo(@NonNull String id, String name) {
        if (id == null) {
            throw new NullPointerException("id is marked non-null but is null");
        } else {
            this.id = id;
            this.name = name;
        }
    }
}      

@SneakyThrows

作用于方法上 在方法内部添加try-catch

使用方法:

@SneakyThrows
    public String getValue(){
        return "a";
    }      

編譯後效果:

public String getValue() {
        try {
            return "a";
        } catch (Throwable var2) {
            throw var2;
        }
    }      

@Value

作用于類上,會生成全參數的構造方法、getter方法、equals、hashCode、toString方法。

小結

參考文檔