lombok常用注解@Data@AllArgsConstructor@NoArgsConstructor@Builder@Accessors


原貼:https://blog.csdn.net/ChenXvYuan_001/article/details/84961992

   https://blog.csdn.net/weixin_38229356/article/details/82937420

@Data
使用這個注解,就不用再去手寫Getter,Setter,equals,canEqual,hasCode,toString等方法了,注解后在編譯時會自動加進去。

@AllArgsConstructor
使用后添加一個構造函數,該構造函數含有所有已聲明字段屬性參數

@NoArgsConstructor
使用后創建一個無參構造函數

@Builder
關於Builder較為復雜一些,Builder的作用之一是為了解決在某個類有很多構造函數的情況,也省去寫很多構造函數的麻煩,在設計模式中的思想是:用一個內部類去實例化一個對象,避免一個類出現過多構造函數。

然后,通過一個簡單的代碼例子說明:

1)首先,建立一個簡單的類,並用lombok進行注解:注意這是注解前的代碼,可以與后面貼出的注解生成的代碼進行比較:

@Data //生成getter,setter等函數
@AllArgsConstructor //生成全參數構造函數
@NoArgsConstructor//生成無參構造函數
@Builder
public class test1 {
    String name;
    String age;
    String sex;
}

2)測試入口:

 public static void main(String[] args) {
 //使用@Builder注解后,可以直接通過Builder設置字段參數
        test1 t1=new test1.test1Builder()
                .name("wang")
                .age("12")
                .sex("man")
                .build();

        System.out.println("name is"+t1.getName()+'\n'+"age is :"+t1.getAge());

    }

3)通過查看編譯后的類,比較注解前后的代碼量,發現會省去了很多代碼的書寫:

public class test1 {
    String name;
    String age;
    String sex;

    public static test1.test1Builder builder() {
        return new test1.test1Builder();
    }

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

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

    public String getSex() {
        return this.sex;
    }

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

    public void setAge(String age) {
        this.age = age;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof test1)) {
            return false;
        } else {
            test1 other = (test1)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$name = this.getName();
                    Object other$name = other.getName();
                    if (this$name == null) {
                        if (other$name == null) {
                            break label47;
                        }
                    } else if (this$name.equals(other$name)) {
                        break label47;
                    }

                    return false;
                }

                Object this$age = this.getAge();
                Object other$age = other.getAge();
                if (this$age == null) {
                    if (other$age != null) {
                        return false;
                    }
                } else if (!this$age.equals(other$age)) {
                    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;
                }

                return true;
            }
        }
    }

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

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $name = this.getName();
        int 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());
        return result;
    }

    public String toString() {
        return "test1(name=" + this.getName() + ", age=" + this.getAge() + ", sex=" + this.getSex() + ")";
    }

    @ConstructorProperties({"name", "age", "sex"})
    public test1(String name, String age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public test1() {
    }

    public static class test1Builder {
        private String name;
        private String age;
        private String sex;

        test1Builder() {
        }

        public test1.test1Builder name(String name) {
            this.name = name;
            return this;
        }

        public test1.test1Builder age(String age) {
            this.age = age;
            return this;
        }

        public test1.test1Builder sex(String sex) {
            this.sex = sex;
            return this;
        }

        public test1 build() {
            return new test1(this.name, this.age, this.sex);
        }

        public String toString() {
            return "test1.test1Builder(name=" + this.name + ", age=" + this.age + ", sex=" + this.sex + ")";
        }
    }
}

 

@Accessors
Accessor的中文含義是存取器,@Accessors用於配置getter和setter方法的生成結果,下面介紹三個屬性

fluent
fluent的中文含義是流暢的,設置為true,則getter和setter方法的方法名都是基礎屬性名,且setter方法返回當前對象。

@Data
@Accessors(fluent = true)
public class User {
    private Long id;
    private String name;
    
    // 生成的getter和setter方法如下,方法體略
    public Long id() {}
    public User id(Long id) {}
    public String name() {}
    public User name(String name) {}
}  

chain
chain的中文含義是鏈式的,設置為true,則setter方法返回當前對象。

 public static void main(String[] args) {
 //使用@Builder注解后,可以直接通過Builder設置字段參數
        test1 t1=new test1.test1Builder()
                .name("wang")
                .age("12")
                .sex("man")
                .build();

        System.out.println("name is"+t1.getName()+'\n'+"age is :"+t1.getAge());

    }

prefix
prefix的中文含義是前綴,用於生成getter和setter方法的字段名會忽視指定前綴(遵守駝峰命名)。

@Data
@Accessors(prefix = "p")
class User {
    private Long pId;
    private String pName;
    
    // 生成的getter和setter方法如下,方法體略
    public Long getId() {}
    public void setId(Long id) {}
    public String getName() {}
    public void setName(String name) {}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM