Lombok使用方法
Lombok能以简单的注解形式来简化java代码,提高开发人员的开发效率。例如开发中经常需要写的javabean,都需要花时间去添加相应的getter/setter,也许还要去写构造器、equals等方法,而且需要维护,当属性多时会出现大量的getter/setter方法,这些显得很冗长也没有太多技术含量,一旦修改属性,就容易出现忘记修改对应方法的失误。
Lombok能通过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法。出现的神奇就是在源码中没有getter和setter方法,但是在编译生成的字节码文件中有getter和setter方法。这样就省去了手动重建这些代码的麻烦,使代码看起来更简洁些。
Lombok的使用跟引用jar包一样,可以在官网(Download)下载jar包,也可以使用maven添加依赖
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
</dependency>
开启注解处理器
在 Settings 里面选择 Build,Execution,Deployment -> Compiler -> Annotation Processor 勾选 Enable annotation processing,如图:
导入依赖和加上注释后,lombok getter&setter识别不到
lombok使用
lombok使用过程中主要是靠注解起作用的,官网上的文档里面有所有的注解,这里不一一罗列,只说明其中几个比较常用的。
- @Data:注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
- @Setter:注解在属性上;为属性提供 setting 方法
- @Getter:注解在属性上;为属性提供 getting 方法
- @SneakyThrows:无需在签名处显式抛出异常
- @Log4j:注解在类上;为类提供一个 属性名为log 的 log4j 日志对像
- @Slf4j: 同上
- @NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
- @AllArgsConstructor:注解在类上;为类提供一个全参的构造方法
1、@Getter/@Setter
放在字段上
为该字段生成 Getter/Setter 方法
AccessLevel.PUBLIC
指定该 Getter/Setter 方法为 public(默认为 public)
@Setter
@Getter
public class User {
@Getter(AccessLevel.PUBLIC)
private Integer id;
private String name;
}
放在类上
为该类所有字段生成 Getter/Setter 方法
@Getter
public class User {
private Integer id;
private String name;
}
放在类上时,对指定字段取消该字段的 Getter/Setter 方法
AccessLevel.NONE
@Getter
public class User {
private Integer id;
@Getter(AccessLevel.NONE) //单独指定该字段不生成 Getter 方法
private String name;
}
注意
@Getter/@Setter 注解只对该类中的成员变量生效
,static 不产生效果
final 仅生成 Getter 方法(
final 类型字段不可更改
)
2、@ToString
该注解只能放在类上
为该类生成 toString 方法
@ToString
public class User {
private Integer id;
private String name;
}
排除对某些字段的输出
exclude = {"XX","XX"}
@ToString(exclude = {"name"})
public class User {
private Integer id;
private String name;
}
必须包含哪些字段(与 exclude 相反),只输出该字段
of = {"XX",XX}
@ToString(of = {"id"})
public class User {
private Integer id;
private String name;
}
3、@EqualsAndHashCode
用于对象之间的判等和比较
该注解会生成三个方法
该方法通过一系列判断逻辑来判断两个对象是否相等
public boolean equals(final Object o) {
//判断两个对象是否为同一对象
if (o == this) {
return true;
//判断两个对象是否为同一类的实例
} else if (!(o instanceof User)) {
return false;
} else {
User other = (User)o;
//判断两个对象是否为同一类的实例(canEqual())
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(final Object other) {
return other instanceof User;
}
通过各属性的值组成该对象的 hashCode
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;
}
默认情况下,equal() 方法的判等规则为判断两个对象的全部字段值是否相等
排除指定字段的判等:
exclude = {"XX","XX"}
指定哪些字段进行判等:
of = {"XX","XX"}
@EqualsAndHashCode(exclude = {"name"})
public class User {
private Integer id;
private String name;
}
@EqualsAndHashCode(of = {"name"})
public class User {
private Integer id;
private String name;
}
4. @Data
: 注在类上,提供类的get、set、equals、hashCode、canEqual、toString方法
EasyExcel导入时invoke方法中data对象取不到值问题
在一次需求开发中发现,使用EasyExcel导入时,在invoke方法中data对象的属性值一直取不到,全是null值,非常的疑惑。经过排查,发现其实是EasyExcel和Lombok结合使用导致的问题,或者说是cglib中BeanMap和Lombok结合的问题。问题原因定位到了,那么怎么解决呢?有以下几种方法:
- 1、升级EasyExcel版本号(未验证过,记得以前其他项目好像没这个问题,应该新版本解决了)
- 2、导入的对象上不使用@Data注解,手写get和set方法
- 3、导入的对象上继续使用@Data注解,但是需要增加一个@Accessors(chain = false),指定生成的set方法不使用链式结构,也就是返回值为void
- @ExcelProperty(value = "业务号码") Excel中表头不一致(如包含空格,换行或特殊字符肉眼看不到)换成 @ExcelProperty(value = "业务号码", index = 0) 通过index命中