前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >带你快速入门 Java开发神器 lombok

带你快速入门 Java开发神器 lombok

作者头像
陶然同学
发布2023-02-27 09:53:42
2390
发布2023-02-27 09:53:42
举报
文章被收录于专栏:陶然同学博客

在写JavaBean对象时候 每次都要重复Alt+insert写Getter/Setter toString 有参构造 和 无参构造 特别麻烦 浪费时间 今天给大家推荐神器lombok 官方支持的神器 可以帮您省去大量时间 让代码简洁易读

什么是lombok

Project Lombok 是一个 java 库,可自动插入您的编辑器和构建工具,为您的 java 增添趣味。 永远不要再编写另一个 getter 或 equals 方法,使用一个注释,您的类就有一个功能齐全的构建器、自动化您的日志记录变量等等。

安装lombok

lombok安装有两种方法 一种是idea插件安装 一种是导入lombok坐标

第一种:idea插件安装

  • File > Settings > Plugins
  • 点击Browse repositories...
  • 搜索Lombok Plugin
  • 点击Install plugin
  • 重启 IntelliJ IDEA

第二种:导入lombok坐标(最新版本1.18.22)

代码语言:javascript
复制
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>

lombok的使用

以下是常用的lombok注解 其他注解参考文档

@Data

最常用的注解 使用该注解 @Getter/@Setter @toString @NoArgsContructor @AllArgisConstructor 等

只需要一个@Data 一共标准的JavaBean就生成了

例如

代码语言:javascript
复制
@Data
public class Demo {
    private String id;
    private String name;
}

编译后

代码语言:javascript
复制
public class Demo {
    private String id;
    private String name;

    public Demo() {
    }

    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() + ")";
    }
}

@Setter

作用于该属性上 自动默认生成Getter方法

代码语言:javascript
复制
@Setter
public class Demo {
    private String id;
    private String name;
}
代码语言:javascript
复制
public class Demo {
    private String id;
    private String name;

    public Demo() {
    }

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

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

@Getter

@Getter方法与@Setter方法一样 这里不描述了

@NonNull

作用于属性或方法的参数上 用于做非空检查 如果是空 抛出空指针异常

使用方法如下:

代码语言:javascript
复制
public class Demo {
    @NonNull
    private String id;
    private String name;
}

编译后效果:

代码语言:javascript
复制
public class Demo {
    @NonNull
    private String id;
    private String name;

    public Demo() {
    }
}

@ToString

无需通过调试来查看字段 只需要添加@ToString 让lombok自动生成

使用方法:

代码语言:javascript
复制
@ToString
public class Demo {
    private String id;
    private String name;
}

编译后效果:

代码语言:javascript
复制
public class Demo {
    private String id;
    private String name;

    public Demo() {
    }

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

@AllArgsConstructor

作用于类上 自动生成一个全参构造方法

使用方法:

代码语言:javascript
复制
@AllArgsConstructor
public class Demo {
    private String id;
    private String name;
}

编译后效果:

代码语言:javascript
复制
public class Demo {
    private String id;
    private String name;

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

@NoArgsConstructor

跟@AllArgsConstructor效果一样生成构造方法 只不过是无参构造

@EqualsAndHashCode

作用于类上,生成equals、canEqual、hashCode方法。

使用方法:

代码语言:javascript
复制
@EqualsAndHashCode
public class Demo {
    private String id;
    private String name;
}

编译后效果:

代码语言:javascript
复制
public class Demo {
    private String id;
    private String name;

    public Demo() {
    }

    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;
                }

                Object this$name = this.name;
                Object other$name = other.name;
                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.id;
        int result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $name = this.name;
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        return result;
    }
}

@RequiredArgsConstructor

作用于类上 生成被@NonNull 或 被final修饰的属性的构造方法

使用方法:

代码语言:javascript
复制
@RequiredArgsConstructor
public class Demo {
    @NonNull
    private String id;
    private final String name;
}

编译后效果:

代码语言:javascript
复制
public class Demo {
    @NonNull
    private String id;
    private final String name;

    public Demo(@NonNull String id, String name) {
        if (id == null) {
            throw new NullPointerException("id is marked non-null but is null");
        } else {
            this.id = id;
            this.name = name;
        }
    }
}

@SneakyThrows

作用于方法上 在方法内部添加try-catch

使用方法:

代码语言:javascript
复制
    @SneakyThrows
    public String getValue(){
        return "a";
    }

编译后效果:

代码语言:javascript
复制
    public String getValue() {
        try {
            return "a";
        } catch (Throwable var2) {
            throw var2;
        }
    }

@Value

作用于类上,会生成全参数的构造方法、getter方法、equals、hashCode、toString方法。

小结

对于刚学习Java的小白 lombok是不推荐的 等熟悉构造之后 在尝试使用lombok 提高效率

参考文档

lombok官网:Project Lombok

https://projectlombok.org/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 什么是lombok
  • 安装lombok
  • lombok的使用
  • 小结
  • 参考文档
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档