[SpringBoot] Eleven, use Lombok in SpringBoot

When lombok is not used, we are still writing redundant code of get, set, toString methods. These codes have no technical content at all, which greatly affects the appearance, so we started to use lombok

1. Introduce lombok dependency

<!-- lombok插件 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

2. Install the lombok plugin (take IDEA as an example)

File-> Settings-> Plugins are
Install lombok plugin
installed, restart IDEA to take effect

Eclipse, STS and other development tools need to download lombok.jar, will not be introduced here

3. Create the User.java class

@Data
public class User {

    /**
     * 主键id
     */
    private long id;
    /**
     * 登录账号
     */
    private String name;
    /**
     * 登录密码
     */
    private String password;
    /**
     * 性别
     */
    private int sex;
    /**
     * 年龄
     */
    private int age;
}

Use @Data here to provide get, set, equals, hashCode, canEqual, toString methods

4. Common notes

@Data: Note on the class, provide the get, set, equals, hashCode, canEqual, toString methods of
the class @AllArgsConstructor: Note on the class, provide the full parameter structure of
the class @NoArgsConstructor: Note on the class, provide the class without parameters Construct
@Setter: Note on property, provide set method
@Getter: Note on property, provide get method
@EqualsAndHashCode: Note on class, provide corresponding equals and hashCode methods
@ Log4j / @ Slf4j: Note on class, provide Corresponding Logger object, the variable name is log

If you find deficiencies in reading, please leave a message! ! !

Published 100 original articles · praised 321 · 20,000+ views

Guess you like

Origin blog.csdn.net/qq_40065776/article/details/105643530