0
点赞
收藏
分享

微信扫一扫

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

在写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

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

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

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

lombok的使用

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

@Data

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

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

例如

@Data
public class Demo {
private String id;
private String name;
}

编译后

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方法

@Setter
public class Demo {
private String id;
private String name;
}
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

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

使用方法如下:

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

编译后效果:

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

public Demo() {
}
}

@ToString

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

使用方法:

@ToString
public class Demo {
private String id;
private String name;
}

编译后效果:

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

public Demo() {
}

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

@AllArgsConstructor

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

使用方法:

@AllArgsConstructor
public class Demo {
private String id;
private String name;
}

编译后效果:

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方法。

使用方法:

@EqualsAndHashCode
public class Demo {
private String id;
private String name;
}

编译后效果:

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修饰的属性的构造方法

使用方法:

@RequiredArgsConstructor
public class Demo {
@NonNull
private String id;
private final String name;
}

编译后效果:

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

使用方法:

@SneakyThrows
public String getValue(){
return "a";
}

编译后效果:

public String getValue() {
try {
return "a";
} catch (Throwable var2) {
throw var2;
}
}

@Value

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

小结

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

参考文档

lombok官网:​​Project Lombokhttps://projectlombok.org/​​


举报

相关推荐

0 条评论