What is Lombok? It is a method that can help us eliminate those code that must be written but repeated, such as setters, getters, constructors, etc.
First, let’s briefly talk about the installation of lombok for idea. There are two ways to:
1. Download it directly from http://plugins.jetbrains.com/, then put it in the plugins below the idea installation file, and then restart idea
2. Under the settings (windows) or Preferences (mac) of idea, find the plugins menu and click Browse repositories as shown in the figure
Then search for lombok, click on the right to download, download and restart as shown in the picture
Create a new project, add lombok dependencies to the pom file, and the complete pom is as follows:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dalaoyang</groupId> <artifactId>springboot_lombok</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_lombok</name> <description>springboot_lombok</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
Create a new User class, this is a class that uses lombok, the code is as follows:
package com.dalaoyang.entity;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang * @email [email protected] * @date 2018/5/7 */@Data@AllArgsConstructor@NoArgsConstructorpublic class User { private Integer id; private String name; private String password;}
Create a new UserNoLombok class, the same as before, in order to compare the benefits of using lombok, the code is as follows:
package com.dalaoyang.entity;import java.util.Objects;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.entity * @email [email protected] * @date 2018/5/7 */public class UserNoLombok { private Integer id; private String name; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public UserNoLombok(Integer id, String name, String password) { this.id = id; this.name = name; this.password = password; } public UserNoLombok() { } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UserNoLombok that = (UserNoLombok) o; return Objects.equals(id, that.id) && Objects.equals(name, that.name) && Objects.equals(password, that.password); } @Override public int hashCode() { return Objects.hash(id, name, password); } @Override public String toString() { return "UserNoLombok{" + "id=" + id + ", name='" + name + '/'' + ", password='" + password + '/'' + '}'; }}
Then create a new UserController test, the code is as follows:
package com.dalaoyang.controller;import com.dalaoyang.entity.User;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.controller * @email [email protected] * @date 2018/5/7 */@RestControllerpublic class UserController { @GetMapping("/testUser") public User getUser(){ User user = new User(); user.setId(1); user.setName("dalaoyang"); user.setPassword("123"); System.out.println(user.toString()); return user; }}
Visit http://localhost:8080/testUser as shown in the figure
Lombok annotation introduction:
@Data tag, generate getter/setter toString() and other methods
@NonNull : Make you not worry and fall in love with NullPointerException
@CleanUp: Automatic resource management: no longer need to add resources in finally
@Setter/@Getter: Automatically generate set and get methods
@ToString: Automatically generate toString method
@EqualsAndHashcode: Generate implementations of hashCode and equals from fields of an object
@NoArgsConstructor/@RequiredArgsConstructor/@AllArgsConstructor
Automatically generate construction method
@Data: Automatically generate set/get method, toString method, equals method, hashCode method, constructor method without parameters
@Value: used to annotate final classes
@Builder: Generate complex builder api classes
@SneakyThrows: Exception handling (use with caution)
@Synchronized: Synchronized method safe conversion
@Getter(lazy=true) :
@Log: Supports various logger objects, and use corresponding annotations when using them, such as: @Log4j
Source code download: Big Lao Yang Mayun
Local source code download address: SpringBoot-LomBok_jb51.rar
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.