转载

SpringBoot使用LomBok

Lombok是什么?它是一个能帮我们消除那些必须要写但是重复的代码,比如setter,getter,构造函数之类的方法。

首先先简单说一下idea安装lombok,有2种方法: 1.直接从 plugins.jetbrains.com/ 下载,然后放到idea安装文件下面的plugins,然后重启idea

2.在idea的settings(windows)或者Preferences(mac),下找到plugins菜单,点击Browse repositories,如图

SpringBoot使用LomBok

然后搜索lombok,点击右侧下载,下载完成重启即可,如图

SpringBoot使用LomBok

新建项目,在pom文件中加入lombok依赖,完整pom如下:

<?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>

    <groupId>com.dalaoyang</groupId>
    <artifactId>springboot_lombok</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot_lombok</name>
    <description>springboot_lombok</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
    </dependencies>

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


</project>

新建一个User类,这个是使用lombok的类,代码如下:

package com.dalaoyang.entity;

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

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang
 * @email yangyang@dalaoyang.cn
 * @date 2018/5/7
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private Integer id;
    private String name;
    private String password;
}

新建一个UserNoLombok类,这个和原来一样,目的为了对比使用lombok的好处,代码如下:

package com.dalaoyang.entity;

import java.util.Objects;

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.entity
 * @email yangyang@dalaoyang.cn
 * @date 2018/5/7
 */
public class UserNoLombok {
    private Integer id;
    private String name;
    private String password;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserNoLombok(Integer id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }


    public UserNoLombok() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserNoLombok that = (UserNoLombok) o;
        return Objects.equals(id, that.id) &&
                Objects.equals(name, that.name) &&
                Objects.equals(password, that.password);
    }

    @Override
    public int hashCode() {

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

    @Override
    public String toString() {
        return "UserNoLombok{" +
                "id=" + id +
                ", name='" + name + '/'' +
                ", password='" + password + '/'' +
                '}';
    }
}

然后新建一个UserController测试,代码如下:

package com.dalaoyang.controller;

import com.dalaoyang.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.controller
 * @email yangyang@dalaoyang.cn
 * @date 2018/5/7
 */
@RestController
public class UserController {

    @GetMapping("/testUser")
    public User getUser(){
        User user = new User();
        user.setId(1);
        user.setName("dalaoyang");
        user.setPassword("123");
        System.out.println(user.toString());
        return user;
    }
}

访问 http://localhost:8080/testUser 如图

SpringBoot使用LomBok

lombok注解简介:

@Data 标签,生成getter/setter toString()等方法 
@NonNull : 让你不在担忧并且爱上NullPointerException 
@CleanUp : 自动资源管理:不用再在finally中添加资源的close方法 
@Setter/@Getter : 自动生成set和get方法 
@ToString : 自动生成toString方法 
@EqualsAndHashcode : 从对象的字段中生成hashCode和equals的实现 
@NoArgsConstructor/@RequiredArgsConstructor/@AllArgsConstructor 
自动生成构造方法 
@Data : 自动生成set/get方法,toString方法,equals方法,hashCode方法,不带参数的构造方法 
@Value : 用于注解final类 
@Builder : 产生复杂的构建器api类 
@SneakyThrows : 异常处理(谨慎使用) 
@Synchronized : 同步方法安全的转化 
@Getter(lazy=true) : 
@Log : 支持各种logger对象,使用时用对应的注解,如:@Log4j

源码下载 :大老杨码云

个人网站:www.dalaoyang.cn

原文  https://juejin.im/post/5af0247ff265da0b8070a9c9
正文到此结束
Loading...