Spring Boot2 series of tutorials (c) | LomBok use to improve development efficiency

Micro-channel public number: a good wreck
any questions or suggestions, please leave a message back, I will try to solve your problem.

File

Foreword

Last week went to the annual meeting, Resort and Spa is the place to go. Honestly, I am no sense, because 90% did not win (the boss too stingy, two hundred people only smoke three awards), 10% is due to the hot springs from childhood grew up feeling nothing.

What LomBok that?

LomBok is a plug-in, but it can eliminate duplication of code must be written to help us through annotations, such as the method setter, getter, constructor of the class.

Imagine a scenario in project development, we often need to define a large number of database entities or certain tools Bean, Bean every need us to write getter, setter method, constructor and toString methods. This is a very tedious process. Programmers guidelines, repeated three times to do more things we must think of ways to make it automated. So today to introduce you a fast hardware plug-in "LomBok."

LomBok common comment Profile

@Data:注解在类上,将类提供的所有属性都添加get、set方法,并添加、equals、canEquals、hashCode、toString方法
@Setter:注解在类上,为所有属性添加set方法、注解在属性上为该属性提供set方法
@Getter:注解在类上,为所有的属性添加get方法、注解在属性上为该属性提供get方法
@NotNull:在参数中使用时,如果调用时传了null值,就会抛出空指针异常
@Synchronized 用于方法,可以锁定指定的对象,如果不指定,则默认创建一个对象锁定
@Log作用于类,创建一个log属性
@Builder:使用builder模式创建对象
@NoArgsConstructor:创建一个无参构造函数
@AllArgsConstructor:创建一个全参构造函数
@ToStirng:创建一个toString方法
@Accessors(chain = true)使用链式设置属性,set方法返回的是this对象。
@RequiredArgsConstructor:创建对象
@UtilityClass:工具类
@ExtensionMethod:设置父类
@FieldDefaults:设置属性的使用范围,如private、public等,也可以设置属性是否被final修饰。
@Cleanup: 关闭流、连接点。
@EqualsAndHashCode:重写equals和hashcode方法。
@toString:创建toString方法。

how to install?

1, directly from http://plugins.jetbrains.com/ download and install the file into IDEA plugins below, and then restart the IDEA.

2, in IDEA's settings (windows) or Preferences (mac), locate the plugins menu, click Browse repositories, as shown below

Browse repositories

The second step search LomBok click Install (I have installed here, but updated so shows the Update button) to restart the IDEA.

Search LomBok

Code demonstrates

New SpringBoot project will not build reading this article using the IDEA project to build Spring Boot , check the web rely build dependency and Lombok, complete pom follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.nasus</groupId>
    <artifactId>lombok</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>lombok</name>
    <description>lombok_demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Student a new category, which is the use lombok class code as follows:

package com.nasus.entity;

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

/**
 * Project Name:springboot_lomnok_demo <br/>
 * Package Name:com.nasus.entity <br/>
 * Date:2019/1/23 0023 14:32 <br/>
 * <b>Description:</b> TODO: 描述该类的作用 <br/>
 *
 * @author <a href="mailto:[email protected]">chenzy</a><br/>
 * Copyright Notice =========================================================
 * This file contains proprietary information of Eastcom Technologies Co. Ltd.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2019 =======================================================
 */
@Data //自动生成set/get方法,toString方法,equals方法,hashCode方法,不带参数的构造方法 
@AllArgsConstructor //自动生成构造方法 
@NoArgsConstructor //自动生成构造方法 
public class Student {

    private String id;
    private String name;
    private int age;

}

StudentNoLombok a new class, which is used without lombok classes (such useless in the project, and the comparison used only for Lombok class, highlighting the use of simple LomBok class code), as follows:

package com.nasus.entity;

import java.util.Objects;

/**
 * Project Name:springboot_lomnok_demo <br/>
 * Package Name:com.nasus.entity <br/>
 * Date:2019/1/23 0023 14:34 <br/>
 * <b>Description:</b> TODO: 描述该类的作用 <br/>
 *
 * @author <a href="mailto:[email protected]">chenzy</a><br/>
 * Copyright Notice =========================================================
 * This file contains proprietary information of Eastcom Technologies Co. Ltd.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2019 =======================================================
 */
public class StudentNoLombok {

    private String id;
    private String name;
    private int age;

    public StudentNoLombok() {
    }

    public StudentNoLombok(String id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof StudentNoLombok)) {
            return false;
        }
        StudentNoLombok that = (StudentNoLombok) o;
        return age == that.age &&
                Objects.equals(id, that.id) &&
                Objects.equals(name, that.name);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, name, age);
    }

    @Override
    public String toString() {
        return "StudentNoLombok{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

As can be seen from the comparison of the above two categories, using analogies LomBok widget using simple appearance much.

StudentController new class, as follows:

package com.nasus.controller;

import com.nasus.entity.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Project Name:springboot_lomnok_demo <br/>
 * Package Name:com.nasus.controller <br/>
 * Date:2019/1/23 0023 14:37 <br/>
 * <b>Description:</b> TODO: 描述该类的作用 <br/>
 *
 * @author <a href="mailto:[email protected]">chenzy</a><br/>
 * Copyright Notice =========================================================
 * This file contains proprietary information of Eastcom Technologies Co. Ltd.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2019 =======================================================
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @GetMapping("/testLombok")
    public Student getStudent(){
        Student student = new Student();
        student.setId("6666666666");
        student.setAge(24);
        student.setName("陈志远");
        System.out.println(student.toString());
        return student;
    }

}

Start the project, access to the address http: // localhost: 8080 / student / testLombok browser output as follows:

Browser Output

Console output is as follows:

Console output

As can be seen from the above two outputs, LomBok plug working. Even if the Student does not override toString method, still can be called, and the results and rewriting the toString method of the same.

When the project is very large, it is often the basis of Bean very much. I recommend this article you can see in the project to LomBok with them.

The last offer project complete code

Afterword

If you see here, that you liked this article, please forward, thumbs up. Micro-channel search " a good basket case ", after concerns reply " 1024 " will give you a complete set of java tutorial.

Tutorial excerpt

Guess you like

Origin www.cnblogs.com/nasus/p/12149632.html