lombok的@Accessors注解3个属性说明

Accessors翻译是存取器。通过该注解可以控制getter和setter方法的形式。

@Accessors(fluent = true)

使用fluent属性,getter和setter方法的方法名都是属性名,且setter方法返回当前对象

复制代码
@Data
@Accessors(fluent = true)
class User {
    private Integer id;
    private String name;
    
    // 生成的getter和setter方法如下,方法体略
    public Integer id(){}
    public User id(Integer id){}
    public String name(){}
    public User name(String name){}
}
复制代码

@Accessors(chain = true)

使用chain属性,setter方法返回当前对象

复制代码
@Data
@Accessors(chain = true)
class User {
    private Integer id;
    private String name;
    
    // 生成的setter方法如下,方法体略
    public User setId(Integer id){}
    public User setName(String name){}
}
复制代码

@Accessors(prefix = “f”)

使用prefix属性,getter和setter方法会忽视属性名的指定前缀(遵守驼峰命名)

复制代码
@Data
@Accessors(prefix = "f")
class User {
    private Integer fId;
    private String fName;
    
    // 生成的getter和setter方法如下,方法体略
    public Integer id(){}
    public void id(Integer id){}
    public String name(){}
    public void name(String name){}
}
复制代码
原文地址:https://www.cnblogs.com/exmyth/p/13709404.html