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){}
}

 

相关文章:

  • 2021-11-16
  • 2021-11-11
  • 2022-03-03
  • 2021-06-25
  • 2021-09-25
猜你喜欢
  • 2022-02-17
  • 2021-06-16
  • 2021-09-28
  • 2023-03-20
相关资源
相似解决方案