Intellij IDEA中lombok插件的使用

本文介绍了在Idea中开启对Lombok插件的支持,包括下载、安装和重启。还说明了Lombok的使用,如在pom.xml添加依赖,以及常用注解@Setter、@Getter等在实体类中的使用,最后举例在User实体类添加@Data注解的效果。
  • idea中开启对lombok插件的支持

下载lombok插件,在settings中
在这里插入图片描述
搜索lombok,然后再install,install好了之后再restart
在这里插入图片描述

  • lombok的使用

在pom.xml文件中添加lombok的依赖

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

在实体类中常用注解的使用,只需要在实体类中添加下列响应注解,lombok插件就能自动生成相应的方法

@Setter:注解在类上,该类所有的字段都生成set方法,注解在字段上,只为该字段生成set方法
@Getter:注解在类上,该类所有的字段都生成get方法,注解在字段上,只为该字段生成get方法
@Data:注解在类上;提供类所有属性的get和set方法,此外还提供了equals、canEqual、hashCode、toString方法
@ToString:注解在类上,生成toString方法
@EqualAndHashCode:注解在类上,生成equals、canEqual、hashCode方法
@NoArgsConstructor:注解在类上;生成一个无参的构造方法
@AllArgsConstructor:注解在类上;生成一个全参的构造方法

  • 举例使用

在User实体类中添加@Data注解,相当于提供类所有属性的get和set方法,此外还提供了equals、canEqual、hashCode、toString方法

import lombok.*;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
@Data
public class User {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private Integer age;
    private String sex;
    private String address;
}

在编译后的class文件中

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private Integer age;
    private String sex;
    private String address;

    public User() {}
    public Long getId() {return this.id;}
    public String getName() {return this.name;}
    public Integer getAge() {return this.age;}
    public String getSex() {return this.sex;}
    public String getAddress() {return this.address;}
    public void setId(final Long id) {this.id = id;}
    public void setName(final String name) {this.name = name;}
    public void setAge(final Integer age) {this.age = age;}
    public void setSex(final String sex) {this.sex = sex;}
    public void setAddress(final String address) {this.address = address;}
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof User)) {
            return false;
        } else {
            User other = (User)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label71: {
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if (this$id == null) {
                        if (other$id == null) {
                            break label71;
                        }
                    } else if (this$id.equals(other$id)) {
                        break label71;
                    }
                    return false;
                }
                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }
                label57: {
                    Object this$age = this.getAge();
                    Object other$age = other.getAge();
                    if (this$age == null) {
                        if (other$age == null) {
                            break label57;
                        }
                    } else if (this$age.equals(other$age)) {
                        break label57;
                    }
                    return false;
                }
                Object this$sex = this.getSex();
                Object other$sex = other.getSex();
                if (this$sex == null) {
                    if (other$sex != null) {
                        return false;
                    }
                } else if (!this$sex.equals(other$sex)) {
                    return false;
                }

                Object this$address = this.getAddress();
                Object other$address = other.getAddress();
                if (this$address == null) {
                    if (other$address == null) {
                        return true;
                    }
                } else if (this$address.equals(other$address)) {
                    return true;
                }

                return false;
            }
        }
    }
    protected boolean canEqual(final Object other) {
        return other instanceof User;
    }
    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.getId();
        int result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $age = this.getAge();
        result = result * 59 + ($age == null ? 43 : $age.hashCode());
        Object $sex = this.getSex();
        result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
        Object $address = this.getAddress();
        result = result * 59 + ($address == null ? 43 : $address.hashCode());
        return result;
    }
    public String toString() {
        return "User(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ", sex=" + this.getSex() + ", address=" + this.getAddress() + ")";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值