IDEA must install plug-ins: Lombok, GenerateAllSetter

Lombok eliminates redundant code of entity classes through annotations

GenerateAllSetter quickly generates the set method of the entity

Lombok

In previous Java projects, there were too many unfriendly codes: POJO getter/setter/toString/construction methods;
Printing logs; closing operations of I/O streams, etc., these codes have no technical content, but also affect the beauty of the code, Lombok came into being.
LomBok can help developers eliminate redundant code in JAVA, especially in POJO classes, through annotations.

Before using the Lombok plugin

Use Lombok after plugin

We only need to add an annotation, LomBok can automatically generate get and set methods for us

Using Lombok, if the IDEA version is above 2020.3, there is no need to install the Lombok plugin. If the IDEA version is below 2020.3, you need to install the Lombok plugin

Open the settings, find Plugins, just search and install

Download lombok.jar to add dependencies to the project

Download (projectlombok.org)https://projectlombok.org/downloadDownload lombok.jar

Open the project structure and add the jar package just downloaded as a dependency to the project

Set Enable annotation processing to tick to enable annotation processing

Next, write the entity class, which will automatically use lombok

Write annotations on the attributes, and lombok will automatically provide methods such as setter/getter for the attributes in the class.

In IDEA, hold down Ctrl + 7 to view the code generated by Lombok.

@Setter, @Getter

Role: Provide setter/getter methods for properties in the class.
Position: above the class or above the attribute, above the attribute (ie member variable), generate a setter/getter method for the attribute (ie member variable), above the class means to generate a setter/getter method for all attributes under the class.
Properties: Set setter and getter access.

● Static variables do not generate getter and setter methods
● Final modified variables only generate getter methods

@toString

Function: Generate the toString method, which will print the class name and each field in order by default.
Location: Above Class
Attributes:
exclude: Cancel the display of one or more variables in the toString method

If you just write a @ToString above the class, then the toString method is to output all member variables. In addition, if you use the exclude attribute, if you want not to output multiple member variables, write it like a list

@ToString(exclude = {"password","username"})

@EqualsAndHashCod

In Java, call equals() to determine whether two objects are equal. If the class does not override this method, it is determined whether the two references point to the same object. In the actual development process, if the attribute values (member variable values) of two objects are equal, then the two objects are considered equal.

How to override equals():

  1. Determine whether two bows point to the same object
  2. Determine whether the reference is Null
  3. To determine whether the actual types of two objects are equal, you need to call canEqual()
  4. Determines whether the properties of two objects are equal

To determine whether an object is repeated in Set, you need to call hashCode() to calculate the hash value before calling equals().
Therefore, judging that objects are equal requires rewriting the three methods equals(), canEqual(), and hashCode().

@EqualsAndHashCode
Function: generate equals and hashCode, canEqual method. Used to compare whether two class objects are the same.
Location: Above Class
Attributes:
exclude: Exclude some attributes when comparing
of: Only use some attributes when comparing

Notice:
● Only use non-static properties when comparing
● By default, only the attributes defined by this class are used and the attributes defined by the parent class are not compared

@NonNull

Function: Used before the method parameter, indicating that the parameter cannot be null when calling the method; used above the attribute, indicating the attribute
When assigning a value (note that when it is used above the attribute, it cannot be null when assigning a value, so the initial value can be null) the value cannot be null.
Position: before method parameters or above attributes.

(1) @NoArgsConstructor
Function: generate a parameterless construction method
Location: Above Class

(2) @RequiredArgsConstructor
Function: Generate a constructor that contains final and @NonNull modified properties

The attribute modified by final must be assigned in the construction object, and the attribute assigned by @NonNull cannot be null
Location: Above Class

@NoArgsConstructor and @RequiredArgsConstructor cannot be used at the same time

(3) @AllArgsConstructor
Function: Generate a construction method with full parameters
Location: Above Class

@Data

Function: Equivalent to adding @Setter. @Getter. @ToString, @EqualsAndHashCode.
@RequiredArgsConstructor five annotations
Location: Above Class

@Builder

Role: Provide chained style to create objects
Location: Above Class

@Log

Function: Generate a log object in the class, which can be used directly in the method
Location: Above Class

Note: For different log implementation products, there are different log annotations. Using @Log means using the log function that comes with Java. In addition to @Log, you can also use @Log4j, @Log4j2, @SIf4j and other annotations to use different logs product.

@Cleanup

Function: Automatically close resources, such as I0 stream objects.
Position: in front of the code

@SneakyThrows

Function: catch and throw exceptions in the method
Position: above the method

GenerateAllSetter

In the usual development process, it is inevitable that some entity beans will be converted to each other. If there are few entity class attribute fields, it is okay. We can type it manually, such as user.setName(u.getName()), but when the entity bean When there are more than 10 fields, manual typing will inevitably reduce the development efficiency. At this time, GenerateAllSetter has to come into play. GenerateAllSetter can quickly generate the set method of the entity, greatly reducing the development time.

1. GenerateAllSetter plug-in installation

Open the setting window of idea, find Plugins, search for generateallsetter, as shown in the figure, I have already installed it here, if it is not installed, click the install button to install it, after the installation is complete, it will prompt to restart idea

2. Use GenerateAllSetter

After installing and restarting the idea, you can use the shortcut key to quickly generate code. The shortcut key is alt + enter (enter key), or you can go to the idea to manually change the shortcut key. You can see that a series of set methods have been automatically generated , and the same properties of the two entity beans will be automatically set on

It can be generated directly by clicking, saving us a lot of time