Category:JavaWeb
Article From:https://www.cnblogs.com/PreachChen/p/9122723.html

Lombok

LombokIt is a tool that can help us to simplify the elimination of some of the necessary but overstaffed Java code by using a simple annotation. By using the corresponding annotations, the corresponding method can be generated when the source code is compiled. Usually, the objects and bean we define need to produce get and se.T method, when it is necessary to create many objects, it is very cumbersome and unsightly. Lombok can save these operations for us through a series of annotations.

Install plug-in

Search and install in the IDEA plug-in Plugins.

Mavenrely on

  1. <!–lombok–>  
  2. <dependency>  
  3.     <groupId>org.projectlombok</groupId>  
  4.     <artifactId>lombok</artifactId>  
  5.     <version>1.16.20</version>  
  6.     <scope>provided</scope>  
  7. </dependency>  

Simple use

  1. package com.preach.model;  
  2.   
  3. import lombok.Data;  
  4.   
  5. /** 
  6.  * @author chen 
  7.  * @date 2018-05-25 
  8.  */  
  9. @Data  
  10. public class Student {  
  11.     public String name;  
  12.     public String address;  
  13.     public int age;  
  14.   
  15.     public static void main(String[] args) {  
  16.         Student student = new Student();  
  17.         student.setAge(24);  
  18.         student.setAddress(“wuhu”);  
  19.         student.setName(“chen”);  
  20.         System.out.printf(student.getName());  
  21.     }  
  22. }  

structure

Look at the structure, the methods such as get and set have been OK.

Common annotation

annotation Effect Explain
@Data Provides getting and setting methods for all attributes of the class, and also provides equals, canEqual, hashCode and toString methods. Annotation on the class
@Getter/@Setter Provide the get/set method for the property Annotation on attributes
@NonNull If this parameter is null, it will throw new NullPointerException (parameter name). Annotation on parameters
@NoArgsConstructor Provide a method of constructing a class without reference Annotation on the class
@AllArgsConstructor Provide a full parameter construction method for class Annotation on the class
@Cleanup Auto recovery resource default call close method Annotations are before the referenced variable
@Value It is an immutable form of @Data, which is equivalent to adding final declarations to attributes, providing getter methods instead of setter methods. On the class

Appendix: IDEA commonly used plug-in collation

Original address: https://blog.csdn.net/cx243698/article/details/80453726

Link of this Article: IDEA utility plug-in Lombok

Leave a Reply

Your email address will not be published. Required fields are marked *