Lombok features
The @Data annotation saves a lot of get() and set() methods in entity classes.
The @Builder annotation creates a generator.
various other annotations
Effect
Then you can use the entity class normally.
rely
Before using annotations such as @Data, you need to install the Lombok plug-in first. The installation method will be introduced later.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
@Data
Entity class
package com.example.datademo.web;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import lombok.Data;
@Data
public class StudentVO {
@NotBlank
@Size(min = 2, max = 5)
private String name;
@NotNull
@Min(18)
@Max(200)
private Integer age;
@NotEmpty
private List<Integer> courseIds;
@NotNull
@DateTimeFormat(iso = ISO.DATE)
private Date graduationDate;
}
call in interface
package com.example.datademo.web;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("example")
public class ExampleController {
@GetMapping("test")
public String test() {
return "test ok!";
}
@GetMapping("student")
public StudentVO getStudent(@Valid StudentVO vo) {
String name = vo.getName();
vo.setName(name);
return vo;
}
}
The effect of calling in the interface
Separate calls to get() and set() methods
The get() and set() methods of the entity class can be used normally.
Call the toString() method alone
@Builder
the code
package com.example.datademo.web;
import lombok.Builder;
@Builder
public class Dialog {
private String tile;
private String content;
private String positiveButtonText;
private String negativeButtonText;
interface OnClickListenter {
void onClick();
}
private OnClickListenter positiveButtonListener;
public void run() {
positiveButtonListener.onClick();
}
}
use
public static void main(String[] args) {
Dialog dialog = Dialog.builder()
.tile("标题")
.content("内容")
.positiveButtonText("确定")
.negativeButtonText("取消")
.positiveButtonListener(() -> {
System.out.println("回调");
})
.build();
System.out.println("运行");
dialog.run();
}
result
@Singular
Create a method to add an element to the collection. Need to be used together with @Builder.
package com.example.vodemo.employee;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import lombok.Singular;
@Data
@Builder
public class Student1 {
private Long id;
private String name;
@Singular("addHobby")
private List<String> hobby;
public static void main(String[] args) {
Student1 student = Student1.builder().id(300L).name("赵三").addHobby("篮球").addHobby("游泳").build();
System.out.println(student.toString());
}
}
val and var
public void testValAndVar() {
val student = new StudentVO();
// student = new StudentVO(); // val不可变
var student2 = new StudentVO();
student2 = new StudentVO();
}
@FieldNameConstants
package com.example.vodemo.employee;
import lombok.Data;
import lombok.experimental.FieldNameConstants;
@Data
@FieldNameConstants
public class StudentVO {
private Long id;
private String name;
}
public void field() {
System.out.println(StudentVO.Fields.id);
System.out.println(StudentVO.Fields.name);
}
@Accessors(chain = true) - experimental annotation
package com.example.vodemo.employee;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class Student2 {
private Long id;
private String name;
public static void main(String[] args) {
Student2 vo = new Student2();
vo.setId(200L).setName("赵二");
System.out.println(vo.toString());
}
}
@Accessors(fluent = true) - experimental annotation
package com.example.vodemo.employee;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(fluent = true)
public class Student {
private Long id;
private String name;
public static void main(String[] args) {
Student student = new Student();
student.id(100L).name("张三");
System.out.println(student.toString());
System.out.println(student.id());
System.out.println(student.name());
}
}
other notes
Which fields are included in the Equals method and the HashCode method
- @EqualsAndHashCode(of = { "id", "name" })
Constructor access level
- AccessLevel.PUBLIC
package com.example.vodemo.employee;
import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.Singular;
import lombok.experimental.Accessors;
import lombok.experimental.FieldNameConstants;
@Data
@EqualsAndHashCode(of = { "id", "name" })
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class StudentVO {
private Long id;
private String name;
}
package com.example.vodemo.employee;
import java.io.FileInputStream;
import java.io.IOException;
import lombok.Cleanup;
import lombok.SneakyThrows;
import lombok.Synchronized;
public class Test2 {
@SneakyThrows
public void testThrow() {
Thread.sleep(1000);
}
@Synchronized
public void sync() {
}
public void cleanup(String pathname) throws IOException {
@Cleanup
FileInputStream stream = new FileInputStream(pathname);
}
}
Lombok installation and dependencies
@Data annotation, you need to install the Lombok plugin into the IDE and add dependencies to pom.xml.
1. Download the jar package from the official website
2. Install
Windows platform
Double-click the downloaded lombak.jar, the installation steps are as follows:
Close the pop-up warning window, click Specify location...
D:\app\sts
This is the installation directory of my STS
Restart STS, and the installation will take effect.
Mac platform
- Enter the content of the SpringToolSuite4.app package, and copy lombok.jar to the same directory as SpringToolSuite4.ini;
- Start the terminal in the current directory, run java -jar lombok.jar, select SpringToolSuite4.ini, and click install to complete the installation;
Introduce Lombok dependency
Existing projects can directly add dependencies in pom.xml. You may need to clean the project afterward.
When creating a new springboot project, you can directly choose to add lombok dependencies.