技术干货

Lombok简介与使用

2020-03-08  本文已影响0人  小草莓子桑

最近项目中有人用到了Lombok,一些注解搞得我头晕晕的,借着学习来简单的给大家总结一下。

官网介绍

搜索Lombok发现,还有一个官网,链接贴出来了https://projectlombok.org/

英文介绍

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

中文介绍

Project Lombok是一个java库,它自动插入到编辑器和构建工具中,增强java的性能。不要再编写另一个getter或equals方法,使用一个注释,您的类有一个功能齐全的生成器,自动记录变量,等等。
简单说,他们作用就是让你在写java代码的时候,不用自己编写诸多的get set等方法,使用注解就可以搞定。

使用方式

我们就直接来说说使用方式

maven依赖
<dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
        <optional>true</optional>
        <version>1.18.10</version>
 </dependency>

说下<scope>provided</scope>,lombok只在编译阶段生效,不需要打入包中。
说下<optional>true</optional>,这个设置为true表示,依赖不传递,举个栗子,A项目里面引用了lombok,设置了optional为true,B项目里面引用A,那么A项目依赖的lombok在B项目不会出现。

常用注解

从包里面看了下,注解很多,今天先说下常用注解吧,既然是编译阶段生效,我们就直接看下加上注解类编译出来的class吧


lombok下面的注解
@Data

直接上代码。一个实体类里面两个字段,id、name

@Data
public class TestDto {
    private int id;
    private String name;
}

来来编译后的代码

public class TestDto {
    private int id;
    private String name;

    public TestDto() {
    }

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

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

    public void setId(int 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 TestDto)) {
            return false;
        } else {
            TestDto other = (TestDto)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getId() != other.getId()) {
                return false;
            } else {
                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 TestDto;
    }

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

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

大致可以看到加上@Data注解,编译后,生成所有字段的set、get方法,重写了toString、hashCode、equals几个方法。

@Getter与@Setter
@Setter
@Getter
public class TestDto {
    private int id;
    private String name;
}

来来编译后的代码

public class TestDto {
    private int id;
    private String name;

    public TestDto() {
    }

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

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

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

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

很明显就是生成各个字段的get、set方法

@ToString
@ToString
public class TestDto {
    private int id;
    private String name;
}

来看看编译后的代码

public class TestDto {
    private int id;
    private String name;

    public TestDto() {
    }

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

@ToString作用就是重写ToString方法

@NoArgsConstructor、@AllArgsConstructor

构造器相关的主要有这么几个

@NoArgsConstructor
@AllArgsConstructor
public class TestDto {
    private int id;
    private String name;
}

看看编译后的代码

public class TestDto {
    private int id;
    private String name;

    public TestDto() {
    }

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

@NoArgsConstructor、@AllArgsConstructor两个注解分别代表无参构造函数、与全部参数的构造函数

Lombok简介与使用,就先为大家说到这里,欢迎大家来交流,指出文中一些说错的地方,让我加深认识,愿大家没有bug,谢谢!

上一篇 下一篇

猜你喜欢

热点阅读