SpringBoot2.0 (Lombok, SpringBoot unified return package)

1. Introduction to Lombok

In a java project, we need to create a lot of java beans. The template codes for getter, setter, equals, hashCode and toString are written in these javaBeans. These codes have no technical content.
​ Then we use Lombok to automatically generate these codes through annotations. Improve our work efficiency.
​The principle of Lombok: JSR 269 plug-in annotation processing. Before compiling and generating bytecode, Lombok dynamically modifies the AST to add new nodes (that is, the code that Lombok needs to generate custom annotations) based on the annotation processor written by itself, and finally generates a JVM executable bytecode Class file. .
What is JSR 269?

JSR 269:  Pluggable Annotation Processing API
实现在Javac编译阶段利用“Annotation Processor”对自定义的注解进行预处理后生成真正在JVM上面执行的“Class文件

java反射技术的对比:

  1. Custom annotations using Annotation Processing are modified during the compilation phase.
  2. JDK's reflection technology is dynamically modified at runtime
结论:反射更加灵活一些但是带来的性能损耗更加大

2. Add dependencies

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

3. Springboot uniformly returns encapsulation

This format mainly contains 3 parts:
code:状态码, 由后端统一定义各种返回结果的状态码
message:描述信息
data:返回的数据,例如列表数据

3.1, create a toolkit util and a JsonData class

Insert image description here
JsonData class

package com.demo.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor //会生成一个包含所有变量
@NoArgsConstructor //生成一个无参数的构造方法
public class JsonData {
    
    

    /**
     * 状态码 0 表示成功,1表示处理中,-1表示失败
     */
    private Integer code;
    /**
     * 数据
     */
    private Object data;
    /**
     * 描述
     */
    private String msg;

    // 成功,传入数据
    public static JsonData buildSuccess() {
    
    
        return new JsonData(0, null, null);
    }

    // 成功,传入数据
    public static JsonData buildSuccess(Object data) {
    
    
        return new JsonData(0, data, null);
    }

    // 失败,传入描述信息
    public static JsonData buildError(String msg) {
    
    
        return new JsonData(-1, null, msg);
    }

    // 失败,传入描述信息,状态码
    public static JsonData buildError(String msg, Integer code) {
    
    
        return new JsonData(code, null, msg);
    }
}

After writing this class, there is no need to write get and set methods in the entity class.

4. Example entity class

Adding the @Data annotation to the entity class eliminates the need to write get and set methods.
Adding the @Data annotation has the same effect as writing the get and set methods.

package com.demo.bean;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.Version;
import lombok.Data;

@Data
public class Passer {
    
    

    /**  */

    private Integer id ;
    /**  */

    private String passername ;
    /**  */

    private String passerage ;
    /**  */

    private String passersex ;

    @Version
    private Integer version;

    @TableLogic(value = "0",delval = "1")
    private Integer deleted;
}

5. @Data annotation

@Data, define a clean class, add this annotation, mvn compile to view the bytecode, it
acts on the class, it is a collection of the following annotations
@ToString
@EqualsAndHashCode
@Getter
@Setter
@RequiredArgsConstructor

Guess you like

Origin blog.csdn.net/H20031011/article/details/132857705