简体   繁体   English

Lombok @Builder 和 setter

[英]Lombok @Builder with setter

I have class having Lombok @Builder annotation as我有 class 有 Lombok @Builder注释作为

@Builder
public class Employee {

  private String empId;
  private String empName;
  private String managerName;
}

After processing or after some business logic I want set managerName in same Employee Object by using setter method like empObj.setManagerName("managerName")在处理之后或在某些业务逻辑之后,我想通过使用像empObj.setManagerName("managerName")这样的setter方法在同一个 Employee Object

How can I use setter method with Builder in Lombok如何在 Lombok 的 Builder 中使用 setter 方法

You have options.你有选择。 Check out the Lombok documentation查看Lombok 文档

If you want just managerName to have a setter:如果您只想managerName有一个二传手:

Use the Lombok @Setter on managerName , ie@Setter上使用 Lombok managerName ,即

@Setter
private String managerName;

If you want all your fields to have a setter:如果您希望所有字段都有一个设置器:

You can also put a @Getter and/or @Setter annotation on a class.您还可以在 class 上放置 @Getter 和/或 @Setter 注释。 In that case, it's as if you annotate all the non-static fields in that class with the annotation.在这种情况下,就好像您使用注释注释了 class 中的所有非静态字段。

You can also add the class-level @Data annotation .您还可以添加类级别的@Data注释 It gives you the combined effect of several other annotations, and is often useful for POJO's.它为您提供了几个其他注释的组合效果,并且通常对 POJO 很有用。

NB that @Data and @Builder play a little rough with one-another, because @Data removes the default no-arg constructor.注意,@ @Data@Builder有些粗糙,因为@Data删除了默认的无参数构造函数。 So, if you want to keep @Builder , you'll need to add an explicit @NoArgsConstructor .所以,如果你想保留@Builder ,你需要添加一个显式的@NoArgsConstructor

Just add @Setter to class/field只需将@Setter添加到类/字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM