How to use @Accessors correctly in lombok

This article will explain in detail how to use @Accessors correctly in lombok. The content of the article is of high quality, so I will share it with you as a reference. I hope you have a certain understanding of related knowledge after reading this article.

@Accessors

The Chinese meaning of Accessor is an accessor, @Accessors is used to configure the result of the getter and setter methods, the following three properties are introduced

fluent

The Chinese meaning of fluent is fluent. When set to true, the method names of the getter and setter methods are both basic property names, and the setter method returns the current object. as follows

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

chain

The Chinese meaning of chain is chained. When set to true, the setter method returns the current object. as follows

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

prefix

The Chinese meaning of prefix is ​​prefix, and the field names used to generate getter and setter methods will ignore the specified prefix (according to the camel case naming). as follows

@Data
@Accessors(prefix = "p")
class User {
    
    
 private Long pId;
 private String pName;
 
 // 生成的getter和setter方法如下,方法体略
 public Long getId() {
    
    }
 public void setId(Long id) {
    
    }
 public String getName() {
    
    }
 public void setName(String name) {
    
    }
}

About how to use @Accessors correctly in lombok, I share it here. I hope that the above content can be helpful to everyone and learn more. If you think the article is good, you can share it for more people to see.

This article is reproduced from: https://www.yisu.com/zixun/318467.html

Guess you like

Origin blog.csdn.net/wuxiaolongah/article/details/110951088