Lombok安装以及基础应用

Lombok是一个很好的工具类,可以让java代码变得更简洁。使用方式都是通过注解来实现。

软件版本介绍

lombok 版本:1.16.16;jdk要求:1.8

下载jar包

网页下载

http://central.maven.org/maven2/org/projectlombok/lombok/1.16.16/

pom.xml

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.16</version>
    <scope>provided</scope>
</dependency>
Eclipse安装lombok插件

通过上一个步骤下好jar包,右键jar包选择通过java执行,会自动寻找IDE,选择要安装的IDE即可,如果报错,注意看报错信息,提示的很清楚。

基础注解

官网文档路径:https://projectlombok.org/features

@NonNull

If put on a parameter, lombok will insert a null-check at the start of the method / constructor’s body, throwing a NullPointerException with the parameter’s name as message. If put on a field, any generated method assigning a value to this field will also produce these nullchecks. / 如果放入一个参数,lombok将在方法/构造函数的主体开始处插入一个null-check,并抛出一个NullPointerException,参数的名称作为消息。如果放在一个字段上,任何为该字段分配值的生成方法也将生成这些nullcheck。

// java源文件

@Setter
public class Demo {

    @NonNull
    private String id;
    private String name;

}


// 编译后文件

public class Demo {
    @NonNull
    private String id;
    private String name;

    public void setId(@NonNull String id) {
        if (id == null) {
            throw new NullPointerException("id");
        } else {
            this.id = id;
        }
    }

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

Generates an all-args constructor. An all-args constructor requires one argument for every field in the class. / 生成一个所有属性的构造函数。

@RequiredArgsConstructor

Generates a constructor with required arguments. Required arguments are final fields and fields with constraints such as @NonNull. / 生成一个带参数的构造函数,需要结合@NonNull注解使用

@NoArgsConstructor

Generates a no-args constructor. / 无参构造函数

// java 源码

@AllArgsConstructor
@RequiredArgsConstructor
@NoArgsConstructor
public class Demo {

    @NonNull
    private String id;
    private String name;

}

// 编译后的文件

import java.beans.ConstructorProperties;
import lombok.NonNull;

public class Demo {
    @NonNull
    private String id;
    private String name;

    @ConstructorProperties({"id", "name"})
    private Demo(@NonNull String id, String name) {
        if (id == null) {
            throw new NullPointerException("id");
        } else {
            this.id = id;
            this.name = name;
        }
    }

    @ConstructorProperties({"id"})
    public Demo(@NonNull String id) {
        if (id == null) {
            throw new NullPointerException("id");
        } else {
            this.id = id;
        }
    }

    public Demo() {
    }
}
@Getter

Put on any field to make lombok build a standard getter. / 在任何字段上构建标准的getter,也就是添加get方法。

@Sette

Put on any field to make lombok build a standard setter. / 在任何字段上构建标准的setter,也就是添加set方法。

这两个注释也可以使用在类上


// java源码

@Getter
@Setter
public class Demo {

    private String id;
    private String name;
}

// 编译后的文件

public class Demo {
    private String id;
    private String name;

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

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

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

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

Generates an implementation for the toString method inherited by all objects, consisting of printing the values of relevant fields. / 为所有对象继承的toString方法生成实现,包括打印相关字段的值。


// java 源码

@ToString(exclude = { "id" })
public class Demo {

    private String id;
    private String name;
}


// 编译后文件

public class Demo {
    private String id;
    private String name;

    public String toString() {
        return "Demo(name=" + this.name + ")";
    }
}
@EqualsAndHashCode

Generates implementations for the equals and hashCode methods inherited by all objects, based on relevant fields. / 根据相关字段为所有对象继承的equals和hashCode方法生成实现。


// java 源码

@EqualsAndHashCode(exclude = { "name" })
public class Demo {

    private String id;
    private String name;
}

// 编译后文件

public class Demo {
    private String id;
    private String name;

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Demo)) {
            return false;
        } else {
            Demo other = (Demo) o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$id = this.id;
                Object other$id = other.id;
                if (this$id == null) {
                    if (other$id != null) {
                        return false;
                    }
                } else if (!this$id.equals(other$id)) {
                    return false;
                }

                return true;
            }
        }
    }

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

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

Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.

Equivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode. / 等同于使用了@Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode五个注解


// java源码

public class Demo {

    private String id;
    private String name;

}


// 编译后的文件

public class Demo {
    private String id;
    private String name;

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

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

    public void setId(String 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 Demo)) {
            return false;
        } else {
            Demo other = (Demo) o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$id = this.getId();
                Object other$id = other.getId();
                if (this$id == null) {
                    if (other$id != null) {
                        return false;
                    }
                } else if (!this$id.equals(other$id)) {
                    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(Object other) {
        return other instanceof Demo;
    }

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

    public String toString() {
        return "Demo(id=" + this.getId() + ", name=" + this.getName() + ")";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑾析编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值