Intellij IDEA installs lombok and uses it in detail

Bean, entity and other classes are often used in projects. Most data classes require get, set, toString, equals and hashCode methods. Although there are automatically generated shortcuts in eclipse and idea development environments, these codes are automatically generated. Afterwards, if the properties in the bean are modified, deleted or added, methods such as get/set need to be regenerated or deleted, which increases the burden on code maintenance. However, using lombok is different. After using lombok's annotations (@Setter, @Getter, @ToString, @@RequiredArgsConstructor, @EqualsAndHashCode or @Data), there is no need to write or generate get/set and other methods, to a large extent It reduces the amount of code and reduces the burden of code maintenance. Therefore, it is strongly recommended to use lombok in the project, and remove the code of methods such as get, set, toString, equals and hashCode in the bean.

1. Actual combat

1. Install the lombok plugin:

The specific process is shown in the figure:

1.1

 

1.2

 
 

1.3

 
 

2. Add the pom.xml dependency of lombok's maven:

<dependency>  
          <groupId>org.projectlombok</groupId>  
          <artifactId>lombok</artifactId>  
          <version>1.16.10</version>  
</dependency>

3. Sample code Student.java

package com.lombok.demo;  
  
  
import lombok.EqualsAndHashCode;  
import lombok.Getter;  
import lombok.Setter;  
import lombok.ToString;  
  
/** 
 * Created by zhangzh on 2017/2/8.
 */  
@Setter  
@Getter  
@ToString  
@EqualsAndHashCode  
public class Student {  
  
    private String name;  
    private int age;  
    private String male;  
    private String studentNo;  
}  

 

4. Test class LombokTest.java

package com.lombok.demo;  
  
import lombok.extern.java.Log;  
  
/** 
 * Created by zhangzh on 2017/2/8.
 */  
@Log  
public class LombokTest {  
  
    public static void main(String[] args) {  
  
        Student student = new Student();  
        student.setAge(27);  
        student.setMale("man");  
        student.setName("lance");  
        student.setStudentNo("2017");  
  
        System.out.println(student.toString());  
  
        Student student2 = new Student();  
        student2.setAge(27);  
        student2.setMale("man");  
        student2.setName("lance");  
        student2.setStudentNo("2017");  
  
        System.out.println(student.equals(student2));  
  
        student2.setStudentNo("2018");  
  
        System.out.println(student.equals(student2));  
  
        log.info("lombok test");  
  
    }  
}  

 

5. 输出结果:

Student(name=lance, age=27, male=man, studentNo=2017)  
true  
false  
lombok test

 

结果分析,如果没有添加@Setter注解,则LombokTest中的student示例无法使用setAge()等方法。使用lombok之后,省去了许多没必要的get,set,toString,equals,hashCode代码,简化了代码编写,减少了代码量。

另外@Data注解的作用相当于 @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode的合集。

另外@Log 省去了在LombokTest中添加 getLogger的如下代码:

private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());

看,简单吧!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326409591&siteId=291194637