[工具插件] 简单粗暴节省JavaBean代码插件 Lombok.jar

什么是Lombok?

我称呼它为小辣椒.其实是红色的.

它能干什么?

我给你讲你不要告诉别人,看他们写JavaBean累死累活的生成 getter/setter/toString等等
不过要是同一个项目,还是要统一规范滴

不多xx,步入正题

首先 @Lombok官网
简介:

Project Lombok is a java library that automatically plugs into your editor and build tools, 
spicing up your java.Never write another getter or equals method again, 
with one annotation your class has a fully featured builder, 
Automate your logging variables, and much more.

默默百度翻译一下:

Lombok是一个Java库,能自动插入编辑器并构建工具,简化Java开发.
通过添加注解的方式,不需要为类编写getter或eques方法,同时可以自动化日志变量.

简而言之:Lombok能以简单的注解形式来简化java代码,提高开发人员的开发效率

那么怎么用?

使用Lombok需要的开发环境Java+Maven+IntelliJ IDEA或者Eclipse(安装Lombok Plugin)

现在我们来操作一下Eclipse里面安装

第一步:
下载这个插件,只要有个这个lombok.jar就ok 这个位置可以随便放,安装之后自动就放安装的eclipse根路径下了


[工具插件] 简单粗暴节省JavaBean代码插件 Lombok.jar_第1张图片
Lombok.jar

第二步:
我们来操作命令行,如下:
1.win+R输入cmd进入命令行


[工具插件] 简单粗暴节省JavaBean代码插件 Lombok.jar_第2张图片
win+R输入cmd进入命令行

2.上如图
[工具插件] 简单粗暴节省JavaBean代码插件 Lombok.jar_第3张图片
需要的操作

找到之后,回车,就有了这个界面
[工具插件] 简单粗暴节省JavaBean代码插件 Lombok.jar_第4张图片
安装界面

然后上面的框框就是你的 eclipse.exe 地址 如果有多个eclipse 自己选一下就ok
然后 快速安装,在右下角 然后就搞定了
3.怎么判断是否安装好了


[工具插件] 简单粗暴节省JavaBean代码插件 Lombok.jar_第5张图片
看图安装好了

我这儿可以直接在右边打开,小伙伴也可以选中文件右键,使用记事本打开.也是一样的如图
[工具插件] 简单粗暴节省JavaBean代码插件 Lombok.jar_第6张图片
记事本打开

这就安装好了
现在我们来爽一下
4.如图:
先导jar包
然后 build path
然后贴标签
看一下右边的视图 都有了,是不是很简洁
[工具插件] 简单粗暴节省JavaBean代码插件 Lombok.jar_第7张图片
爽一把

现在来看一下代码的区别:
不使用Lombok

package cn.icanci.test_151;

import lombok.Data;

/**
 * @author icanci
 * @projectName 151
 * @packageName cn.icanci.test_151
 * @version 1.10
 * @time 2020年1月4日 下午6:33:26
 * @classAction test
 */
public class User {
    private Integer id;
    private String username;
    private String password;
    private String address;
    private String phone;
    private String email;
    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }
    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }
    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }
    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }
    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }
    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }
    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }
    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }
    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + ", address=" + address
                + ", phone=" + phone + ", email=" + email + "]";
    }
    
}

使用之后:

package cn.icanci.test_151;

import lombok.Data;

/**
 * @author icanci
 * @projectName 151
 * @packageName cn.icanci.test_151
 * @version 1.10
 * @time 2020年1月4日 下午6:33:26
 * @classAction 
 */
@Data
public class User {
    private Integer id;
    private String username;
    private String password;
    private String address;
    private String phone;
    private String email;
}

一个标签搞定

现在看一下反编译之后的代码:
package cn.icanci.test_151;

public class User {
    private Integer id;
    private String username;
    private String password;
    private String address;
    private String phone;
    private String email;

    public String toString() {
        return "User(id=" + getId() + ", username=" + getUsername() + ", password=" + getPassword() + ", address="
                + getAddress() + ", phone=" + getPhone() + ", email=" + getEmail() + ")";
    }

    public int hashCode() {
        int PRIME = 59;
        int result = 1;
        Object $id = getId();
        result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $username = getUsername();
        result = result * 59 + ($username == null ? 43 : $username.hashCode());
        Object $password = getPassword();
        result = result * 59 + ($password == null ? 43 : $password.hashCode());
        Object $address = getAddress();
        result = result * 59 + ($address == null ? 43 : $address.hashCode());
        Object $phone = getPhone();
        result = result * 59 + ($phone == null ? 43 : $phone.hashCode());
        Object $email = getEmail();
        result = result * 59 + ($email == null ? 43 : $email.hashCode());
        return result;
    }

    protected boolean canEqual(Object other) {
        return other instanceof User;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof User)) {
            return false;
        }
        User other = (User) o;
        if (!other.canEqual(this)) {
            return false;
        }
        Object this$id = getId();
        Object other$id = other.getId();
        if (this$id == null ? other$id != null : !this$id.equals(other$id)) {
            return false;
        }
        Object this$username = getUsername();
        Object other$username = other.getUsername();
        if (this$username == null ? other$username != null : !this$username.equals(other$username)) {
            return false;
        }
        Object this$password = getPassword();
        Object other$password = other.getPassword();
        if (this$password == null ? other$password != null : !this$password.equals(other$password)) {
            return false;
        }
        Object this$address = getAddress();
        Object other$address = other.getAddress();
        if (this$address == null ? other$address != null : !this$address.equals(other$address)) {
            return false;
        }
        Object this$phone = getPhone();
        Object other$phone = other.getPhone();
        if (this$phone == null ? other$phone != null : !this$phone.equals(other$phone)) {
            return false;
        }
        Object this$email = getEmail();
        Object other$email = other.getEmail();
        return this$email == null ? other$email == null : this$email.equals(other$email);
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setAddress(String address) {
        this.address = address;
    }

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

    public void setUsername(String username) {
        this.username = username;
    }

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

    public String getEmail() {
        return this.email;
    }

    public String getPhone() {
        return this.phone;
    }

    public String getAddress() {
        return this.address;
    }

    public String getPassword() {
        return this.password;
    }

    public String getUsername() {
        return this.username;
    }

    public Integer getId() {
        return this.id;
    }
}

说明@Data注解在类上,会为类的所有属性自动生成setter/getter equals canEqual hashCode,toString方法,如为final属性,则不会为该属性生成setter方法.

常用的注解

@Setter 注解在类或字段,注解在类时为所有字段生成setter方法,注解在字段上时只为该字段生成setter方法
@Getter 使用方法同上,区别在于生成的是getter方法
@ToString 注解在类,添加toString方法
@EqualsAndHashCode 注解在类,生成hashCode和equals方法
@NoArgsConstructor 注解在类,生成无参的构造方法
@RequiredArgsConstructor 注解在类,为类中需要特殊处理的字段生成构造方法,比如final和被@NonNull注解的字段
@AllArgsConstructor 注解在类,生成包含类中所有字段的构造方法
@Data 注解在类,生成setter/getter,equals,canEqual,hashCode,toString方法,如为final属性,则不会为该属性生成setter方法,
@Slf4j 注解在类,生成log变量,严格意义来说是常量.private static final Logger log = LoggerFactory.getLogger(UserController.class);

Lombok的优缺点

优点:
能通过注解的形式自动生成构造器、getter/setter、equals、hashcode、toString等方法,提高了一定的开发效率
让代码变得简洁,不用过多的去关注相应的方法
属性做修改时,也简化了维护为这些属性所生成的getter/setter方法等
缺点:
不支持多种参数构造器的重载
虽然省去了手动创建getter/setter方法的麻烦,但大大降低了源代码的可读性和完整性,降低了阅读源代码的舒适度

现在我们来操作一下idea里面安装

1.下载插件 File>Setting>Plugins>搜索Lombok


[工具插件] 简单粗暴节省JavaBean代码插件 Lombok.jar_第8张图片
进入Setting

[工具插件] 简单粗暴节省JavaBean代码插件 Lombok.jar_第9张图片
下载

安装之后重启一下,然后maven项目添加依赖:

 
        
            org.projectlombok
            lombok
            1.18.8
            provided
        

然后就可以和eclipse一起使用了.

注意:

同一个项目组必须同样的环境,要根据实际开发而来.

结尾bb一下

欢迎交流: @小猪童鞋QQ聊天链接 一起学习呀

你可能感兴趣的:([工具插件] 简单粗暴节省JavaBean代码插件 Lombok.jar)