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

Accessors翻译是存取器。经过该注解能够控制getter和setter方法的形式。

@Accessors(fluent = true)

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

@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方法会忽视属性名的指定前缀(遵照驼峰命名)code

@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){}
}
相关文章
相关标签/搜索