lombok deployment and simple use

lombok deployment

1.lombok overview

Project Lombok is a Java library that automatically plugs into editors and build tools to enhance Java performance. No need to write getters, setters or equals methods anymore, just have an annotation and your class has a full-featured builder, automatic logging of variables, and more.

2. Analysis of common annotations

@Setter is used to generate setter methods for the described classes and does not contain final modified attributes.
@Getter is used to generate a getter method for the described class.
@ToString is used to add a toString method to the described class.
@EqualsAndHashCode is used to generate hashCode and equals methods for the described class.
@NoArgsConstructor is used to generate a parameterless constructor for the described class.
@AllArgsConstructor is used to generate a constructor for the described class that contains all fields in the class.
@Data is used to generate setter/getter, equals, canEqual, hashCode, and toString methods for the described class. If it is a final attribute, the setter method will not be generated for the attribute.
@Slf4J is used to add a log attribute object to the described class.

3.lombok plug-in
  • 1. Find plugins, search for lombok and install it, as shown in the picture:
    I have already installed it here. After searching, just click on the first one.
    Insert image description here

  • 2. Add relevant dependencies in the pom.xml file

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

Insert image description here

4.Using lombok annotations

Entity classes generally have setter, getter, tostring and other methods. Lombok uses brief annotations to replace these methods.

# 这里对应的看上面的2.常用注解分析
@Data
public class Student {
    
    

    private Integer id;
    private String name;
    private Integer age;
}

After commenting, you can view the built code in the target -> classes directory, which is also the source code that does not use Lombok.
as follows:

public class Student {
    
    
    private Integer id;
    private String name;
    private Integer age;

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

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

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

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

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

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

    public boolean equals(final 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 {
    
    
                label47: {
    
    
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if (this$id == null) {
    
    
                        if (other$id == null) {
    
    
                            break label47;
                        }
                    } else if (this$id.equals(other$id)) {
    
    
                        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$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(final Object other) {
    
    
        return other instanceof Student;
    }

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

    public String toString() {
    
    
        return "Student(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ")";
    }
  • Personal opinion:
    Lombok greatly reduces the amount of entity class code, but it also reduces the readability of the code. Therefore, it is not recommended for novices to use Lombok annotations directly. They should start from the basics and lay a solid foundation.

Guess you like

Origin blog.csdn.net/weixin_52173254/article/details/127160574