The use of @Data annotation in Lombok

Table of contents

1. Introduction

2. Use

1. Introduce dependencies into the pom file

2. Add in the definition of JavaBean class (entity class)


1. Introduction

The @Data annotation is an annotation tag provided by the Lombok framework, which can automatically generate the getter and setter methods, toString method, equals method and hashCode method in the JavaBean class (entity class). Using the @Data annotation can simplify the writing of JavaBeans, making the code more concise, and developers can write more readable JavaBean classes faster.

Using the @Data annotation is very simple, just introduce the dependency and add it in the definition of the JavaBean class.

2. Use

1. Introduce dependencies into the pom file

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

2. Add in the definition of JavaBean class (entity class)

For example:

@Data
public class User {
    private Long id;
    private String username;
    private String password;
    private Integer age;
}

In the above code, use the @Data annotation to annotate the User class, which will automatically generate the getter and setter methods, toString method, equals method and hashCode method of the User class. Using @Data annotation can make the code more concise and clear, reduce the writing of redundant getter and setter methods, and improve development efficiency.

In addition to @Data annotations, the Lombok framework also provides other commonly used annotation tags, such as @Getter, @Setter, @ToString, @EqualsAndHashCode, etc., which can help developers write high-quality JavaBean classes more quickly.

Guess you like

Origin blog.csdn.net/weixin_51451545/article/details/131199216