IDEA - Lombok 插件

1. Lombok作用

  • Lombok使用注解,简化 javabean/ 实体类/pojo类的编写
  • Lombok能通过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法。

2. Lombok安装

  • 下载lombok-1.18.22.jar
  • 添加到库 Add as Library IDEA - Lombok 插件_第1张图片
  • IDEA添加lombok插件
    IDEA - Lombok 插件_第2张图片

3. Lombok常用注解

package lombok_demo;
import lombok.*;

@Data               //根据字段 编译文件自动生成 get、set、toString
@Getter             //根据字段 编译文件自动生成 get
@Setter             //根据字段 编译文件自动生成 set
@ToString           //根据字段 编译文件自动生成 toString
@NoArgsConstructor  //根据字段 编译文件自动生成 无参构造
@AllArgsConstructor //根据字段 编译文件自动生成 满参构造
@EqualsAndHashCode  //根据字段 编译文件自动生成 equals hashcode
public class Student {
    private int uid;
    private String name;
    private int age;
    private String gender;
}
  • 自动补全的字节码文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package lombok_demo;

public class Student {
    private int uid;
    private String name;
    private int age;
    private String gender;

    public int getUid() {
        return this.uid;
    }

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

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

    public String getGender() {
        return this.gender;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

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

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

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String toString() {
        int var10000 = this.getUid();
        return "Student(uid=" + var10000 + ", name=" + this.getName() + ", age=" + this.getAge() + ", gender=" + this.getGender() + ")";
    }

    public Student() {
    }

    public Student(int uid, String name, int age, String gender) {
        this.uid = uid;
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

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

                    return false;
                }

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

                return true;
            }
        }
    }

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

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

你可能感兴趣的:(JAVA,java,其他,开发语言)