How to use the @ Builder annotation in Lombok
This article mainly introduces how to use the @ Builder annotation in Lombok. It is very detailed and has a certain reference value. Friends who are interested must finish it!
The use of @ Builder annotations in Lombok
The @ Builder annotation is mainly used to generate objects and can assign values to object chains.
Introduce dependency
Because the @ Builder annotation is something in lombok, the first step is to introduce lombok dependencies, as shown in the following figure:
The second step is to annotate the entity class with @ Builder
In the second step, we need to add an @ Builder annotation to our entity class, as shown in the following figure:
The third step uses tests to generate objects using @ Builder annotations
Compilation result of entity class with @ Builder annotation
After the entity class is annotated with @ Builder, a builder () method and a CardBuilder static inner class are added after compilation, as shown in the following figure:
The code is as follows:
Public class Card {private int id; private String name; private boolean sex; public static Card.CardBuilder builder () {return new Card.CardBuilder ();} public Card (int id, String name, boolean sex) {this.id = id; this.name = name; this.sex = sex;} public Card () {} public int getId () {return this.id } public String getName () {return this.name;} public boolean isSex () {return this.sex;} public void setId (int id) {this.id = id;} public void setName (String name) {this.name = name;} public void setSex (boolean sex) {this.sex = sex } public boolean equals (Object o) {if (o = = this) {return true;} else if (! (o instanceof Card)) {return false;} else {Card other = (Card) o; if (! other.canEqual (this)) {return false } else if (this.getId ()! = other.getId ()) {return false;} else {Object this$name = this.getName (); Object other$name = other.getName () If (this$name = = null) {if (other$name = = null) {return this.isSex () = = other.isSex ();}} else if (this$name.equals (other$name)) {return this.isSex () = = other.isSex () } return false;}} protected boolean canEqual (Object other) {return other instanceof Card;} public int hashCode () {int PRIME = true; int result = 1; int result = result * 59 + this.getId (); Object $name = this.getName () Result = result * 59 + ($name= = null? 43: $name.hashCode ()); result = result * 59 + (this.isSex ()? 79: 97); return result;} public String toString () {return "Card (id=" + this.getId () + ", name=" + this.getName () + ", sex=" + this.isSex () + ")";} public static class CardBuilder {private int id Private String name; private boolean sex; CardBuilder () {} public Card.CardBuilder id (int id) {this.id = id; return this;} public Card.CardBuilder name (String name) {this.name = name; return this } public Card.CardBuilder sex (boolean sex) {this.sex = sex; return this;} public Card build () {return new Card (this.id, this.name, this.sex) } public String toString () {return "Card.CardBuilder (id=" + this.id + ", name=" + this.name + ", sex=" + this.sex + ")";} these are all the contents of the article "how to use @ Builder annotations in Lombok". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!