Accessor 的中文含义是存取器,@Accessors 用于配置 get/set 方法的生成结果,同时也支持链式操作,减少多余对象的创建,另外 builder 类元信息又可以减少。
@Accessors 注解有三个参数,下面分别简单说明一下:
fluent:中文含义是流畅的,设置为 true,则 get/set 方法的方法名都是基础属性名,且 set 方法返回当前对象chain:中文含义是链式的,设置为 true,则 set 方法返回当前对象prefix:中文含义是前缀,用于生成 get/set 方法的字段名会忽视指定前缀(遵守驼峰命名)
示例代码:
import lombok.Data;import lombok.experimental.Accessors;@Datapublic class UserEntity { private Long id; private String name; private int age;}1. fluent
@Accessors(fluent = true) 后编译后的代码:
public class UserEntity{ private Long id; private String name; private int age; public Long id() { return this.id; } public String name() { return this.name; } public int age() { return this.age; } public UserEntity id(final Long id) { this.id = id; return this; } public UserEntity name(final String name) { this.name = name; return this; } public UserEntity age(final int age) { this.age = age; return this; } @Override public boolean equals(final Object o) { if (o == this) { return true; } if (!(o instanceof UserEntity)) { return false; } final UserEntity other = (UserEntity)o; if (!other.canEqual(this)) { return false; } final Object this$id = this.id(); final Object other$id = other.id(); Label_0065: { if (this$id == null) { if (other$id == null) { break Label_0065; } } else if (this$id.equals(other$id)) { break Label_0065; } return false; } final Object this$name = this.name(); final Object other$name = other.name(); if (this$name == null) { if (other$name == null) { return this.age() == other.age(); } } else if (this$name.equals(other$name)) { return this.age() == other.age(); } return false; } protected boolean canEqual(final Object other) { return other instanceof UserEntity; } @Override public int hashCode() { final int PRIME = 59; int result = 1; final Object $id = this.id(); result = result * 59 + (($id == null) ? 43 : $id.hashCode()); final Object $name = this.name(); result = result * 59 + (($name == null) ? 43 : $name.hashCode()); result = result * 59 + this.age(); return result; } @Override public String toString() { return "UserEntity(id=" + this.id() + ", name=" + this.name() + ", age=" + this.age() + ")"; }}2. chain
@Accessors(chain = true) 编译后的代码:
public class UserEntity{ private Long id; private String name; private int age; public Long getId() { return this.id; } public String getName() { return this.name; } public int getAge() { return this.age; } public UserEntity setId(final Long id) { this.id = id; return this; } public UserEntity setName(final String name) { this.name = name; return this; } public UserEntity setAge(final int age) { this.age = age; return this; } @Override public boolean equals(final Object o) { if (o == this) { return true; } if (!(o instanceof UserEntity)) { return false; } final UserEntity other = (UserEntity)o; if (!other.canEqual(this)) { return false; } final Object this$id = this.getId(); final Object other$id = other.getId(); Label_0065: { if (this$id == null) { if (other$id == null) { break Label_0065; } } else if (this$id.equals(other$id)) { break Label_0065; } return false; } final Object this$name = this.getName(); final Object other$name = other.getName(); if (this$name == null) { if (other$name == null) { return this.getAge() == other.getAge(); } } else if (this$name.equals(other$name)) { return this.getAge() == other.getAge(); } return false; } protected boolean canEqual(final Object other) { return other instanceof UserEntity; } @Override public int hashCode() { final int PRIME = 59; int result = 1; final Object $id = this.getId(); result = result * 59 + (($id == null) ? 43 : $id.hashCode()); final Object $name = this.getName(); result = result * 59 + (($name == null) ? 43 : $name.hashCode()); result = result * 59 + this.getAge(); return result; } @Override public String toString() { return "UserEntity(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ")"; }}3. prefix
@Accessors(prefix = "user") 设置后缀示例
import lombok.Data;import lombok.experimental.Accessors;@Data@Accessors(prefix = "user")public class UserEntity { private Long userId; private String userName; private int userAge;}
编译后的代码:
—————————
如有不足请留言指正
相互学习,共同进步