Lombok是什麼?它是一個能幫我們消除那些必須要寫但是重複的程式碼,比如setter,getter,建構函式之類的方法。
首先先簡單說一下idea安裝lombok,有2種方法:
1.直接從plugins.jetbrains.com/下載,然後放到idea安裝檔案下面的plugins,然後重啟idea
2.在idea的settings(windows)或者Preferences(mac),下找到plugins選單,點選Browse repositories,如圖
然後搜尋lombok,點選右側下載,下載完成重啟即可,如圖
新建專案,在pom檔案中加入lombok依賴,完整pom如下:
<?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>
複製程式碼
新建一個User類,這個是使用lombok的類,程式碼如下:
package com.dalaoyang.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang
* @email yangyang@dalaoyang.cn
* @date 2018/5/7
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private String password;
}
複製程式碼
新建一個UserNoLombok類,這個和原來一樣,目的為了對比使用lombok的好處,程式碼如下:
package com.dalaoyang.entity;
import java.util.Objects;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.entity
* @email yangyang@dalaoyang.cn
* @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 + '\'' +
'}';
}
}
複製程式碼
然後新建一個UserController測試,程式碼如下:
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 yangyang@dalaoyang.cn
* @date 2018/5/7
*/
@RestController
public 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;
}
}
複製程式碼
訪問http://localhost:8080/testUser如圖
lombok註解簡介:
@Data 標籤,生成getter/setter toString()等方法
@NonNull : 讓你不在擔憂並且愛上NullPointerException
@CleanUp : 自動資源管理:不用再在finally中新增資源的close方法
@Setter/@Getter : 自動生成set和get方法
@ToString : 自動生成toString方法
@EqualsAndHashcode : 從物件的欄位中生成hashCode和equals的實現
@NoArgsConstructor/@RequiredArgsConstructor/@AllArgsConstructor
自動生成構造方法
@Data : 自動生成set/get方法,toString方法,equals方法,hashCode方法,不帶引數的構造方法
@Value : 用於註解final類
@Builder : 產生複雜的構建器api類
@SneakyThrows : 異常處理(謹慎使用)
@Synchronized : 同步方法安全的轉化
@Getter(lazy=true) :
@Log : 支援各種logger物件,使用時用對應的註解,如:@Log4j
複製程式碼
原始碼下載 :大老楊碼雲
個人網站:www.dalaoyang.cn