Lombok注解知识点总结(@data)

1 Lombok简介

Lombok的官网:https://www.projectlombok.org/

官方介绍为:

  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方法,同时可以自动化日志变量。
即以简单的注解形式来简化java代码,提高开发人员的开发效率。

2 Lombok的使用

2.1 引入maven依赖或jar包

如果是maven管理项目需在pom.xml中引入依赖

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.8</version>
    <scope>provided</scope>
</dependency>

如果想引入jar包需要去官网下载  https://projectlombok.org/download

2.2 安装Lombok插件

我用的IntelliJ IDEA 2019.1.3,打开File的Setting,点击Plugins,点击Browse repositories,在弹出的窗口中搜索lombok,然后安装即可
在这里插入图片描述
在这里插入图片描述

2.3 Lombok的注解

官方注解链接
在这里插入图片描述在这里插入图片描述

常用的一些注解:
  • @Setter 注解在类或字段上,注解在类时为所有字段生成setter方法,注解在字段上时只为该字段生成setter方法。
  • @Getter 注解在类或字段上,注解在类时为所有字段生成getter方法,注解在字段上时只为该字段生成getter方法。
  • @ToString 注解在类上,生成toString方法。
  • @EqualsAndHashCode 注解在类上,生成hashCode()和equals()方法。
  • @NoArgsConstructor 注解在类上,生成无参的构造方法。
  • @AllArgsConstructor 注解在类上,生成包含类中所有字段参数的构造方法。
  • @RequiredArgsConstructor 注解在类上,为类中需要特殊处理的字段生成构造方法。
  • @Data 注解在类上,捆绑特征的快捷方式去注释@ToString,@EqualsAndHashCode,@Getter/@Setter和@RequiredArgsConstructor在一起,假如是final属性,则不会为该属性生成setter()方法。
  • @Slf4j 注解在类上,将拥有一个静态的final log字段,该字段按照使用的日志记录框架通常规定的方式进行初始化,然后可以使用该方法编写日志语句。
@Log注解具体有许多种选择:不同的注解创建不同类型的日志记录器
@CommonsLog
//Creates 
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@Flogger
//Creates
private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();
@JBossLog
//Creates
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
//Creates
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j
//Creates
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2
//Creates
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j
//Creates
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j
//Creates
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
2.4 注解的使用

@Data:注解在类上,捆绑特征的去注解@ToString,@EqualsAndHashCode,@Getter/@Setter和@RequiredArgsConstructor在一起,假如是final属性,则不会为该属性生成setter()方法。
不使用注解:

public class LombokTest {
    private Long id;
    private String userName;
    private String password;

    public Long getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

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

    @Override
    public String toString() {
        return "LombokTest{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

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

    @Override
    public int hashCode() {
        return Objects.hash(id, userName, password);
    }
}

使用@Data注解:

@Data
public class LombokTest {
    private Long id;
    private String userName;
    private String password;
}

在这里插入图片描述
@toString
1、如果需要可以通过注释参数includeFieldNames来控制输出中是否包含的属性名称。
2、可以通过exclude参数中包含字段名称,可以从生成的方法中排除特定字段。
3、可以通过callSuper参数控制父类的输出。
               @ToString(callSuper=true, includeFieldNames=true)

@EqualsAndHashCode
使用在类上,该注解在类级别注释会同时生成equals和hashCode方法。
使用注解:

@EqualsAndHashCode
public class LombokTest {
    private Long id;
    private String userName;
    private String password;
}

在这里插入图片描述
@AllArgsConstructor
注解使用在类上,该注解提供一个全参数的构造方法,默认不提供无参构造方法。
不使用注解:

public class LombokTest {
    private Long id;
    private String userName;
    private String password;

    public LombokTest(Long id, String userName, String password) {
        this.id = id;
        this.userName = userName;
        this.password = password;
    }
}

使用注解:

@AllArgsConstructor
public class LombokTest {
    private Long id;
    private String userName;
    private String password;
}

不使用注解和使用注解Structure结构都为:
在这里插入图片描述
@NoArgsConstructor
注解使用在类上,该注解提供一个无参构造方法。
不使用注解:

public class LombokTest {
    private Long id;
    private String userName;
    private String password;

    public LombokTest() {
    }
}

使用注解:

@NoArgsConstructor
public class LombokTest {
    private Long id;
    private String userName;
    private String password;
}

不使用注解和使用注解Structure结构都为:
在这里插入图片描述

3 遇到的问题

@EqualsAndHashCode
  假如有多个类有相同的部分属性,一般会把它们定义到父类中,恰好id(数据库主键)也在父类中,那么就会存在部分对象在比较时,它们并不相等,却因为lombok自动生成的equals(Object other) 和 hashCode()方法判定为相等,从而导致出错。

因为这个注解它默认仅使用该类中定义的属性且不调用父类的方法 。
可通过callSuper=true解决上一点问题。让其生成的方法中调用父类的方法。

class BaseLombok{
    private Long id;
}
@Data                                  //开发一般都会同时加上这两个注解
@EqualsAndHashCode(callSuper = true)   //
public class LombokTest extends BaseLombok{
    private String userName;
    private String password;
}

4 总结

    (优缺点转自https://blog.csdn.net/ThinkWon/article/details/101392808

优点:
1. 能通过注解的形式自动生成构造器、getter/setter、equals、hashcode、toString等方法,提高了一定的开发效率
2. 让代码变得简洁,不用过多的去关注相应的方法
3. 属性做修改时,也简化了维护为这些属性所生成的getter/setter方法等

缺点:
1. 不支持多种参数构造器的重载
2. 虽然省去了手动创建getter/setter方法的麻烦,但大大降低了源代码的可读性和完整性,降低了阅读源代码的舒适度

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值