Lombok - Accessors详细说明

分享知识 传递快乐

Accessor 的中文含义是存取器,@Accessors 用于配置 get/set 方法的生成结果,同时也支持链式操作,减少多余对象的创建,另外 builder 类元信息又可以减少。

@Accessors 注解有三个参数,下面分别简单说明一下:

  • fluent:中文含义是流畅的,设置为 true,则 get/set 方法的方法名都是基础属性名,且 set 方法返回当前对象
  • chain:中文含义是链式的,设置为 true,则 set 方法返回当前对象
  • prefix:中文含义是前缀,用于生成 get/set 方法的字段名会忽视指定前缀(遵守驼峰命名)

示例代码:

import lombok.Data;
import lombok.experimental.Accessors;

@Data
public 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;

}


编译后的代码:

public class UserEntity
{
    private Long userId;
    private String userName;
    private int userAge;
    
    public Long getId() {
        return this.userId;
    }
    
    public String getName() {
        return this.userName;
    }
    
    public int getAge() {
        return this.userAge;
    }
    
    public void setId(final Long userId) {
        this.userId = userId;
    }
    
    public void setName(final String userName) {
        this.userName = userName;
    }
    
    public void setAge(final int userAge) {
        this.userAge = userAge;
    }
    
    @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$userId = this.getId();
        final Object other$userId = other.getId();
        Label_0065: {
            if (this$userId == null) {
                if (other$userId == null) {
                    break Label_0065;
                }
            }
            else if (this$userId.equals(other$userId)) {
                break Label_0065;
            }
            return false;
        }
        final Object this$userName = this.getName();
        final Object other$userName = other.getName();
        if (this$userName == null) {
            if (other$userName == null) {
                return this.getAge() == other.getAge();
            }
        }
        else if (this$userName.equals(other$userName)) {
            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 $userId = this.getId();
        result = result * 59 + (($userId == null) ? 43 : $userId.hashCode());
        final Object $userName = this.getName();
        result = result * 59 + (($userName == null) ? 43 : $userName.hashCode());
        result = result * 59 + this.getAge();
        return result;
    }
    
    @Override
    public String toString() {
        return "UserEntity(userId=" + this.getId() + ", userName=" + this.getName() + ", userAge=" + this.getAge() + ")";
    }
}


 

—————————
如有不足请留言指正
相互学习,共同进步

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

旷野历程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值