Installation of IDEA-lombok plugin @Data annotation

Preface

A few days ago, in the project that came down from github, I accidentally discovered the use of @Data annotations by the big guys. After some reading and learning, I now put the materials I found at the time here for future learning

Lombok is an open source code generation library. It is a very practical small tool. When editing entity classes, we can use lombok annotations to reducethe writing of getter, setter and other methods. When changing entity classes, we only need to modify the properties, which reduces A lot of repetitive code writing work. The editor of this article only introduces the installation and configuration of the lombok plug-in in IntelliJ IDEA and the simple method of use.

Installation of lombok plugin

First, we need to install the lombok plug-in in IntelliJ IDEA. After opening IntelliJ IDEA, click File–>Settings in the menu bar, or use the shortcut key Ctrl+Alt+S to enter the settings page.
Insert picture description here
We click Plugins in the settings to install the plug-in, select Browse repositories... on the right, and then enter lombok on the search page to find the Lombok Plugin below. Click on the Lombok Plugin to see the Install button on the right, click the button You can install it.
Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here
We can see all the annotations specifically supported by lombok on the installation page. During the installation process, there is a prompt for Downloading Plugins, and the progress bar will change during the installation process. What needs to be reminded is that the network connection must be available and good during the installation process, otherwise the installation may fail. After the installation is successful, we can see the Restart button on the right side. You can leave it alone at this time because we still have subsequent configuration work. After the installation is complete, we will return to Plugins. At this time, you can search for lombok on the right side, but it is not possible before the installation.

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

Configure annotation processor

Similarly, we are on the Settings page, we click Build, Execution, Deployment -> select Compiler -> select Annotation Processors, and then check Enable annotation processing on the right.
Insert picture description here
Insert picture description here

Use of lombok plugin

Before use, we need to explain that the installed plugin is just a call, just like we use the maven plugin, the machine needs to install maven. We also need to add the dependency of lombok before using lombok. The version of lombok has been updated. You can enter lombok maven in the Baidu search box to find the latest dependency version.

<dependency>    
	<groupId>org.projectlombok</groupId>    
	<artifactId>lombok</artifactId>    
	<version>1.16.10</version>
</dependency>

    org.projectlombok    lombok    1.16.10
Next, we edit an entity class Student, add three attributes, and finally add the @Data attribute to the class. This annotation can help us generate get/set methods, equals, canEqual, hashCode, and all attributes of the class in the .class file. toString method, etc.
Insert picture description here
So how do we view the generated method after editing the code? Click View–>Tool Windows–>Structure in the menu bar, you can see all the methods in the class, which are automatically generated by lombok for me.
Insert picture description here
Insert picture description here

expand:

There are some more commonly used annotations, such as:

@AllArgsConstructor is added to the class to generate a construction method with all parameters of the entity class.

@NoArgsConstructor is added to the class to generate a no-argument constructor.

@RequiredArgsConstructor is added to the class and used with the @NonNull annotation to generate a construction method for the specified parameters. For example, add the @NonNull annotation in front of the age attribute, then the User generates a construction method that requires the age parameter.

@Getter is added to the class to generate getter methods for all properties of the entity class.

@Setter is added to the class to generate setter methods for all attributes of the entity class.

@ToString is added to the class and the toString() method is called to output the values ​​of all the attributes in the entity class.


Continuously updating...

Article reference learning address: https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html

Guess you like

Origin blog.csdn.net/weixin_44325444/article/details/106323023