Use Lombok to get rid of get() and set()

Preface

In the code, the entity Dao (Domain) is the one you see most. The entities here are mainly divided into two types. One is the entity Domain of the database access layer, which is directly used to define the entity of a database table. Each field defined in it corresponds to each field in the database table. It is also the "medium" used by the system to interact with data to store data. The other is to define multiple parameters or variables as an entity in the code for convenience, which is generally used for: method parameter transfer, interface external object, design pattern, etc.

1. Traditional set and get methods

Since it is an entity, it is indispensable to define variables, and the setting and obtaining of the value of entity variables are implemented by the set() and get() methods. The standard set and get methods are defined as follows:

public class peopleDao {

	private Long id; 

	private Integer age; 

	private String name; 
	
	private String sex;
	
	public void setId(Long id){
	   this.id = id;
	}
	
	public Long getId(){
	   return id;
	}

	public void setAge(Integer age){
	   this.age = age;
	}
	
	public Integer getAge(){
	   return age;
	}
	
	public void setId(String name){
	   this.name = name;
	}
	
	public String getName(){
	   return name;
	}
	
	public void setId(String sex){
	   this.sex = sex;
	}
	
	public String getSex(){
	   return sex;
	}
}

The method setXxx() parameter is the value of the variable, which is set when we define the value for the variable.

The method getXxx() has no parameters and directly returns the value of the variable in the main memory.

You can see the full text of the set and get methods. If an entity has 20 variables or more, then the entire class looks very long and bloated. It has no technical content and affects the beauty of the code. It becomes troublesome and error-prone when adding or modifying. And the set and get methods also have thread synchronization problems...

Two, Lombok's set and get methods

Lombok is a Java library that automatically connects your editor and build tools. Lombok is actually a "tool" to help us write getter or equals methods. Official website: Lombok official website

2.1 Use of lombok

First introduce the maven dependency:

    <dependency>

          <groupId>org.projectlombok</groupId>

          <artifactId>lombok</artifactId>

          <version>1.16.18</version>

          <scope>provided</scope>

    </dependency>

2.2 Entities using lombok

@Getter
@Setter
public class peopleDao {

	private Long id; 

	private Integer age; 

	private String name; 
	
	private String sex;
	
}

Seeing that, the entity using lombok has become very simple and beautiful. And the execution efficiency becomes very fast, this is because the lombok library directly calls the java source code library to achieve, you can see the lombok source code implementation for details.

Just through simple @Getter @Setter annotations, you can implement variable set and get methods. In addition to @ToString, @NoArgsConstructor (constructor), etc., many annotations are available for you to use, saving you a lot of valuable time and improving code development. effectiveness.

Note: When using Lombok, the variable must be in camel case: aaBb, otherwise the annotation cannot find the variable.

Guess you like

Origin blog.csdn.net/follow_24/article/details/108171977