lombok common annotation @ Data @ AllArgsConstructor @ NoArgsConstructor @ Builder @ Accessors

Original Post: https://blog.csdn.net/ChenXvYuan_001/article/details/84961992

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

@Data
use this annotation, you do not go hand Getter, Setter, equals, canEqual, hasCode, toString methods, and the notes will be automatically added to the list at compile time.

@AllArgsConstructor
adding a constructor is used, which contains the constructor parameter field properties of all declared

@NoArgsConstructor
created after using a no-argument constructor

@Builder
About Builder is more complex, one of the role Builder is to address in a class constructor has a lot of cases, it eliminates the need to write a lot of trouble constructor, in design mode of thinking is: to use an inner class instantiate an object, a class to avoid an excessive number constructor.

Then, by a simple code examples:

1) First, the establishment of a simple class, and notes with Lombok: Note that this is the code before annotations, annotation can be posted back with the generated code is compared:

@Data // generating getter, setter other functions 
@AllArgsConstructor // generate a full-argument constructor 
@NoArgsConstructor // form non-argument constructor 
@Builder
 public  class test1 { 
    String name; 
    String Age; 
    String Sex; 
}

2) Test inlet:

 public  static  void main (String [] args) {
  // after use @Builder annotations directly through parameter setting field Builder 
        test1 T1 = new new test1.test1Builder () 
                .name ( "Wang" ) 
                .age ( "12 is" ) 
                . Sex ( "man" ) 
                .build (); 

        System.out.println ( "IS name" t1.getName + () + '\ n-' + "Age IS:" + t1.getAge ()); 

    }

3) After viewing compiled by the class, the amount of code before and after comparing notes, found to save a lot of code to write:

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
the Accessor in Chinese meaning accessor, @ Accessors for generating configuring getter and setter methods, three properties described below

FLUENT
FLUENT the Chinese meaning is smooth, set to true, then the method name getter and setter methods are the basis for property names and setter method returns the current object.

@Data 
@Accessors (FLUENT = to true )
 public  class the User {
     Private Long ID;
     Private String name; 
    
    // generate getter and setter methods below, the method body slightly 
    public Long ID () {}
     public the User ID (Long ID) {}
     public String name () {}
     public the User name (String name) {} 
}  

catena alberghiera
catena alberghiera the Chinese meaning of the chain, is set to true, the setter method returns the current object.

 public  static  void main (String [] args) {
  // after use @Builder annotations directly through parameter setting field Builder 
        test1 T1 = new new test1.test1Builder () 
                .name ( "Wang" ) 
                .age ( "12 is" ) 
                . Sex ( "man" ) 
                .build (); 

        System.out.println ( "IS name" t1.getName + () + '\ n-' + "Age IS:" + t1.getAge ()); 

    }

prefix
prefix is a prefix meaning in Chinese, the field name is used to generate getter and setter methods will ignore the specified prefix (named after comply hump).

@Data 
@Accessors (prefix = "P" )
 class the User {
     Private Long pId;
     Private String pName; 
    
    // generate getter and setter methods below, the method body slightly 
    public Long getId () {}
     public  void the setId (Long ID) { }
     public String getName () {}
     public  void the setName (String name) {} 
}

 

Guess you like

Origin www.cnblogs.com/penghq/p/12055483.html