C# has properties when writing an entity class, eliminating the need to write getters and setters.

In Java programming, after writing fields, you need to write getter and setter methods one by one. When programming with Idea, you can hold down ALT+INSERT and have the IDE automatically generate getters, setters, toString, etc., as shown below:

However, when a class has a lot of fields, the entire file can look very messy. Fortunately, IDEA provides a Lombok plugin that can add annotations at the code stage, eliminating the need to write getters and setters by hand.

Installation and use steps

1. Install the plug-in

To open Idea, choose “File –> Settings” in the upper left corner of the menu. In the pop-up dialog box, select Plugins on the left, enter” Lombok “in the search bar, and click Install, as shown below:

The button in the picture is uninstall, because I’ve already installed it here. After the installation, the IDE will prompt you to restart. In this case, restart IDEA to start using it.

2. Use of plug-ins

Add lombok dependencies to the project’s POM file. Note that scope is provided, indicating that the package does not need to be packaged, is only needed for compilation, and already has a provider.

<! -- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> < version > 1.18.6 < / version > < scope > provided < / scope > < / dependency >Copy the code

pom.xml

Add the @data annotation to the model class to save the trouble of handwritten getters, setters, and toStrings, as shown below:

With this simple annotation, you can call sysAdmin’s getUsername() method elsewhere.

Lombok has many other optional uses beyond annotating classes with @data. But this one annotation covers 80% of the requirements, so we won’t go into any more details. As for the detailed usage, there are many online examples, interested friends can search for their own.

It is important to note that once the Lombok plug-in is enabled, the rest of the team must install it as well, or it will compile an error.