今天 1024,为了不 996,Lombok 用起来以及避坑指南

Lombok是一个Java库,通过注解自动消除样板代码,如getter、setter、equals、hashCode等。它的工作原理是利用编译时处理,通过JSR269 API在编译阶段修改抽象语法树(AST)。Lombok的优点在于提高开发效率和代码整洁性,但可能带来团队协作和代码可读性的挑战。使用时需注意合理选择注解,避免过度使用@Data。

Lombok简介、使用、工作原理、优缺点

Lombok 项目是一个 Java 库,它会自动插入编辑器和构建工具中,Lombok 提供了一组有用的注解,用来消除 Java 类中的大量样板代码。



简介

官方介绍

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、setter、toString 或 equals 方法,带有一个注释的您的类有一个功能全面的生成器,可以自动化您的日志记录变量,以及更多其他功能

官网链接

使用

添加maven依赖

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

注意: 在这里 scope 要设置为 provided, 防止依赖传递给其他项目

安装插件(可选)

在开发过程中,一般还需要配合插件使用,在 IDEA 中需要安装 Lombok 插件即可

为什么要安装插件?

首先在不安装插件的情况下,代码是可以正常的编译和运行的。如果不安装插件,IDEA 不会自动提示 Lombok 在编译时才会生成的一些样板方法,同样 IDEA 在校验语法正确性的时候也会提示有问题,会有大面积报红的代码

示例

下面举两个栗子,看看使用 lombok 和不使用的区别

创建一个用户类

不使用 Lombok

public class User {
  private Integer id;
  private Integer age;
  private String realName;

  public User() {
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    User user = (User) o;

    if (!Objects.equals(id, user.id)) {
      return false;
    }
    if (!Objects.equals(age, user.age)) {
      return false;
    }
    return Objects.equals(realName, user.realName);
  }

  @Override
  public int hashCode() {
    int result = id != null ? id.hashCode() : 0;
    result = 31 * result + (age != null ? age.hashCode() : 0);
    result = 31 * result + (realName != null ? realName.hashCode() : 0);
    return result;
  }

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

  public Integer getId() {
    return id;
  }

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

  public Integer getAge() {
    return age;
  }

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

  public String getRealName() {
    return realName;
  }

  public void setRealName(String realName) {
    this.realName = realName;
  }
}

使用Lombok

@Data
public class User {
  private Integer id;
  private String username;
  private Integer age;
}

使用 @Data 注解会在编译的时候自动生成以下模板代码:

  • toString
  • equals
  • hashCode
  • getter 不会对 final 属性生成
  • setter 不会对 final 属性生成
  • 必要参数的构造器

关于什么是必要参数下面会举例说明

全部注解

上面已经简单看了一下 @Data 注解,下面看下所有的可以用的注解

  • @NonNull 注解在字段和构造器的参数上。注解在字段上,则在 setter, constructor 方法中加入判空,注意这里需要配合 @Setter、@RequiredArgsConstructor、@AllArgsConstructor 使用;注解在构造器方法参数上,则在构造的时候加入判空
  • @Cleanup 注解在本地变量上。负责清理资源,当方法直接结束时,会调用 close 方法
  • @Setter 注解在类或字段。注解在类时为所有字段生成setter方法,注解在字段上时只为该字段生成setter方法,同时可以指定生成的 setter 方法的访问级别
  • @Getter 使用方法同 @Setter,区别在于生成的是 getter 方法
  • @ToString 注解在类上。添加toString方法
  • @EqualsAndHashCode 注解在类。生成hashCode和equals方法
  • @NoArgsConstructor 注解在类。生成无参的构造方法。
  • @RequiredArgsConstructor 注解在类。为类中需要特殊处理的字段生成构造方法,比如 final 和被 @NonNull 注解的字段。
  • @AllArgsConstructor 注解在类,生成包含类中所有字段的构造方法。
  • @Data 注解在类,生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。
  • @Value 注解在类和属性上。如果注解在类上在类实例创建后不可修改,即不会生成 setter 方法,这个会导致 @Setter 不起作用
  • @Builder 注解在类上,生成构造器
  • @SneakyThrows
  • @Synchronized 注解在方法上,生成同步方法
  • @With
  • 日志相关: 注解在类,生成 log 常量,类似 private static final xxx log
    • @Log java.util.logging.Logger
    • @CommonsLog org.apache.commons.logging.Log
    • @Flogger com.google.common.flogger.FluentLogger
    • @JBossLog org.jboss.logging.Logger
    • @Log4j org.apache.log4j.Logger
    • @Log4j2 org.apache.logging.log4j.Logger
    • @Slf4j org.slf4j.Logger
    • @XSlf4j org.slf4j.ext.XLogger

关于所有的注解可以查看 https://projectlombok.org/features/all

综合实例

综合实例一

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter                     // 生成 getter
@AllArgsConstructor         // 生成所有的参数
@RequiredArgsConstructor    // 生成必要参数的构造器
@ToString           // 生成 toString
@EqualsAndHashCode  // 生成 equals 和 hashCode
@Builder            // 生成一个 builder
public class UserLombok {

  // 创建 setter 并且校验 id 不能为空
  @Setter
  @NonNull
  private Integer id;

  // 创建 setter 且生成方法的访问级别为 PROTECTED
  @Setter(AccessLevel.PROTECTED)
  private Integer age;

  // 创建 setter 不校验是否为空
  @Setter
  private String realName;

  // 构造器,校验 id 不能为空
  public UserLombok(@NonNull Integer id, Integer age) {
    this.id = id;
    this.age = age;
  }

  /**
   * 自定义 realName 的 setter 方法,这个优先高于 Lombok
   * @param realName 真实姓名
   */
  public void setRealName(String realName) {
    this.realName = "realName:" + realName;
  }
}

具体生成的类为

import lombok.NonNull;

public class UserLombok {
  @NonNull
  private Integer id;
  private Integer age;
  private String realName;

  public UserLombok(@NonNull Integer id, Integer age) {
    if (id == null) {
      throw new NullPointerException("id is marked non-null but is null");
    } else {
      this.id = id;
      this.age = age;
    }
  }

  public void setRealName(String realName) {
    this.realName = "realName:" + realName;
  }

  public static UserLombok.UserLombokBuilder builder() {
    return new UserLombok.UserLombokBuilder();
  }

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

  public Integer getAge() {
    return this.age;
  }

  public String getRealName() {
    return this.realName;
  }

  public UserLombok(@NonNull Integer id, Integer age, String realName) {
    if (id == null) {
      throw new NullPointerException("id is marked non-null but is null");
    } else {
      this.id = id;
      this.age = age;
      this.realName = realName;
    }
  }

  public UserLombok(@NonNull Integer id) {
    if (id == null) {
      throw new NullPointerException("id is marked non-null but is null");
    } else {
      this.id = id;
    }
  }

  public String toString() {
    return "UserLombok(id=" + this.getId() + ", age=" + this.getAge() + ", realName=" + this.getRealName() + ")";
  }

  public boolean equals(Object o) {
    if (o == this) {
      return true;
    } else if (!(o instanceof UserLombok)) {
      return false;
    } else {
      UserLombok other = (UserLombok)o;
      if (!other.canEqual(this)) {
        return false;
      } else {
        label47: {
          Object this$id = this.getId();
          Object other$id = other.getId();
          if (this$id == null) {
            if (other$id == null) {
              break label47;
            }
          } else if (this$id.equals(other$id)) {
            break label47;
          }

          return false;
        }

        Object this$age = this.getAge();
        Object other$age = other.getAge();
        if (this$age == null) {
          if (other$age != null) {
            return false;
          }
        } else if (!this$age.equals(other$age)) {
          return false;
        }

        Object this$realName = this.getRealName();
        Object other$realName = other.getRealName();
        if (this$realName == null) {
          if (other$realName != null) {
            return false;
          }
        } else if (!this$realName.equals(other$realName)) {
          return false;
        }

        return true;
      }
    }
  }

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

  public int hashCode() {
    int PRIME = true;
    int result = 1;
    Object $id = this.getId();
    int result = result * 59 + ($id == null ? 43 : $id.hashCode());
    Object $age = this.getAge();
    result = result * 59 + ($age == null ? 43 : $age.hashCode());
    Object $realName = this.getRealName();
    result = result * 59 + ($realName == null ? 43 : $realName.hashCode());
    return result;
  }

  public void setId(@NonNull Integer id) {
    if (id == null) {
      throw new NullPointerException("id is marked non-null but is null");
    } else {
      this.id = id;
    }
  }

  protected void setAge(Integer age) {
    this.age = age;
  }

  public static class UserLombokBuilder {
    private Integer id;
    private Integer age;
    private String realName;

    UserLombokBuilder() {
    }

    public UserLombok.UserLombokBuilder id(@NonNull Integer id) {
      if (id == null) {
        throw new NullPointerException("id is marked non-null but is null");
      } else {
        this.id = id;
        return this;
      }
    }

    public UserLombok.UserLombokBuilder age(Integer age) {
      this.age = age;
      return this;
    }

    public UserLombok.UserLombokBuilder realName(String realName) {
      this.realName = realName;
      return this;
    }

    public UserLombok build() {
      return new UserLombok(this.id, this.age, this.realName);
    }

    public String toString() {
      return "UserLombok.UserLombokBuilder(id=" + this.id + ", age=" + this.age + ", realName=" + this.realName + ")";
    }
  }
}

综合实例二

@Value
public class UserLombok {

  @NonNull
  private Integer id;

  // 这里的 setter 不会生成,所有没用,这里反面示例
  @Setter(AccessLevel.PROTECTED)
  private Integer age;

  private String realName;

}

@ValueToString、EqualsAndHashCode、AllArgsConstructor、Getter 的组合注解

生成的代码

import lombok.NonNull;

public final class UserLombok {
  @NonNull
  private final Integer id;
  private final Integer age;
  private final String realName;

  public UserLombok(@NonNull Integer id, Integer age, String realName) {
    if (id == null) {
      throw new NullPointerException("id is marked non-null but is null");
    } else {
      this.id = id;
      this.age = age;
      this.realName = realName;
    }
  }

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

  public Integer getAge() {
    return this.age;
  }

  public String getRealName() {
    return this.realName;
  }

  public boolean equals(Object o) {
    if (o == this) {
      return true;
    } else if (!(o instanceof UserLombok)) {
      return false;
    } else {
      UserLombok other;
      label44: {
        other = (UserLombok)o;
        Object this$id = this.getId();
        Object other$id = other.getId();
        if (this$id == null) {
          if (other$id == null) {
            break label44;
          }
        } else if (this$id.equals(other$id)) {
          break label44;
        }

        return false;
      }

      Object this$age = this.getAge();
      Object other$age = other.getAge();
      if (this$age == null) {
        if (other$age != null) {
          return false;
        }
      } else if (!this$age.equals(other$age)) {
        return false;
      }

      Object this$realName = this.getRealName();
      Object other$realName = other.getRealName();
      if (this$realName == null) {
        if (other$realName != null) {
          return false;
        }
      } else if (!this$realName.equals(other$realName)) {
        return false;
      }

      return true;
    }
  }

  public int hashCode() {
    int PRIME = true;
    int result = 1;
    Object $id = this.getId();
    int result = result * 59 + ($id == null ? 43 : $id.hashCode());
    Object $age = this.getAge();
    result = result * 59 + ($age == null ? 43 : $age.hashCode());
    Object $realName = this.getRealName();
    result = result * 59 + ($realName == null ? 43 : $realName.hashCode());
    return result;
  }

  public String toString() {
    return "UserLombok(id=" + this.getId() + ", age=" + this.getAge() + ", realName=" + this.getRealName() + ")";
  }
}

综合实例三

日志使用

import lombok.extern.java.Log;

@Log
public class LogLombok {

  public void log() {
    log.info("打个日志");
  }
}

生成后代码

import java.util.logging.Logger;

public class LogLombok {
  private static final Logger log = Logger.getLogger(LogLombok.class.getName());

  public LogLombok() {
  }

  public void log() {
    log.info("打个日志");
  }
}

通过上面的示例,我们可以看出 Lombok 可以大大简化我们的代码

Lombok的优缺点

优点:

  1. 提高开发效率,自动生成getter/setter、toString、builder 等,尤其是类不断改变过程中,如果使用 IDEA 自动生成的代码,我们则需要不停的删除、重新生成,使用 Lombok 则自动帮助我们完成
  2. 让代码变得简洁,不用过多的去关注相应的模板方法,其中 getter/setter、toString、builder 均为模板代码,写着难受,不写还不行,而且在 java 14 已经开始计划支持 record, 也在帮我们从原生方面解决这种模板代码
  3. 属性做修改时,也简化了维护为这些属性所生成的getter/setter方法等

缺点:

  1. 不同开发人员同时开发同一个使用 Lombok 项目、需要安装 Lombok 插件
  2. 不利于重构属性名称,对应的 setter、getter、builder, IDEA 无法帮助自动重构
  3. 有可能降低了源代码的可读性和完整性,降低了阅读源代码的舒适度,谁会去阅读模板代码呢

解决编译时出错问题

编译时出错,可能是没有启用注解处理器。Build, Execution, Deployment > Annotation Processors > Enable annotation processing。设置完成之后程序正常运行。

避坑指南

  • 尽量不要使用 @Data 注解, 这个注解太全了,不利于维护,除非你知道你在干什么
  • Java 默认机制如果有其他构造器,则不会生成无参构造器,在使用 @AllArgsConstructor 注解时,记得加上 @NoArgsConstructor
  • 如果类定义还在变化阶段,不建议使用 @AllArgsConstructor 注解
  • @Setter@Getter 注解如果需要可以缩小使用范围
  • @ToString 注解默认不会生成父类的信息,如果需要生成需要 @ToString(callSuper = true)
  • @RequiredArgsConstructor@NoArgsConstructor 尽量不要一起使用,无参构造器无法处理 @NonNull,但在序列化/反序列化的还是需要提供无参的
  • 当团队决定不再使用 Lombok 的时候,可以使用 Lombok 插件的 Delombok 一键去除,在 Refactor > Delombok

再次注意- @AllArgsConstructor 尽量不要使用

参考

  • https://projectlombok.org
  • https://github.com/rzwitserloot/lombok

Lombok工作原理

工作原理来自网上资料

在Lombok使用的过程中,只需要添加相应的注解,无需再为此写任何代码。自动生成的代码到底是如何产生的呢?

核心之处就是对于注解的解析上。JDK5引入了注解的同时,也提供了两种解析方式。

  • 运行时解析

运行时能够解析的注解,必须将@Retention设置为RUNTIME,这样就可以通过反射拿到该注解。java.lang.reflect反射包中提供了一个接口AnnotatedElement,该接口定义了获取注解信息的几个方法,Class、Constructor、Field、Method、Package等都实现了该接口,对反射熟悉的朋友应该都会很熟悉这种解析方式。

  • 编译时解析

编译时解析有两种机制,分别简单描述下:

1)Annotation Processing Tool

apt自JDK5产生,JDK7已标记为过期,不推荐使用,JDK8中已彻底删除,自JDK6开始,可以使用Pluggable Annotation Processing API来替换它,apt被替换主要有2点原因:

  • api都在com.sun.mirror非标准包下
  • 没有集成到javac中,需要额外运行

2)Pluggable Annotation Processing API

JSR 269自JDK6加入,作为apt的替代方案,它解决了apt的两个问题,javac在执行的时候会调用实现了该API的程序,这样我们就可以对编译器做一些增强,javac执行的过程如下:

Lombok本质上就是一个实现了“JSR 269 API”的程序。在使用javac的过程中,它产生作用的具体流程如下:

  1. javac对源代码进行分析,生成了一棵抽象语法树(AST)
  2. 运行过程中调用实现了“JSR 269 API”的Lombok程序
  3. 此时Lombok就对第一步骤得到的AST进行处理,找到@Data注解所在类对应的语法树(AST),然后修改该语法树(AST),增加getter和setter方法定义的相应树节点
  4. javac使用修改后的抽象语法树(AST)生成字节码文件,即给class增加新的节点(代码块)

通过读Lombok源码,发现对应注解的实现都在HandleXXX中,比如@Getter注解的实现在HandleGetter.handle()。还有一些其它类库使用这种方式实现,比如Google AutoDagger等等。

红色图片

<think>嗯,用户现在的问题是关于如何在Java实体类中屏蔽某个字段被映射,以及解决Unknown column错误的问题。他们提到了MyBatis-Plus的@TableField注解,所以需要结合之前的回答和引用的资料来详细解答。 首先,回顾之前的回答,已经解释了MyBatis Plus和JPA中字段映射的问题,特别是未使用注解导致列名匹配的错误。用户现在需要更具体的屏蔽字段的方法,以及如何配置以免Unknown column错误。 根据引用[1],@TableField(exist = false)的作用是标记字段在数据库中存在,这样MyBatis-Plus生成SQL时会忽略这个字段。而引用[3]中也提到@TableField(select = false)可以在查询时返回该字段的值。这些应该就是解决屏蔽字段的关键点。 用户可能的需求是,实体类中有一些字段需要映射到数据库,比如临时变量或计算字段。这时候需要使用exist=false属性。另外,如果某个字段在数据库中存在,但查询时想返回,可以用select=false。 接下来需要分步骤说明如何配置,包括基本用法、常用属性、全局配置以及解决Unknown column错误的方法。需要强调正确的注解使用,以及如何结合全局命名策略来免字段名和列名一致的问题。 此外,用户之前遇到Unknown column错误,可能是因为字段名符合默认的驼峰转下划线规则,或者字段名包含特殊字符,需要显式指定列名。所以解决方案中应该包括检查命名策略,使用注解明确指定列名,以及处理特殊字段的情况。 可能还需要提到其他注意事项,比如静态字段的屏蔽,使用静态变量或@TableField(exist=false)来免被ORM框架处理。同时,结合引用中的信息,给出示例代码和配置方法,帮助用户更好地理解和应用。 最后,确保回答结构清晰,步骤明确,涵盖用户的问题点,并引用提供的参考资料来支持解答,比如引用[1]、[2]、[3]中的相关内容。</think>在MyBatis-Plus中实现字段屏蔽与解决`Unknown column`错误,需要结合`@TableField`注解和框架配置策略。以下是具体实现方法和原理分析: --- ### 一、字段屏蔽的3种核心方法 #### 1. 基础屏蔽(数据库无对应列) ```java @TableField(exist = false) // 完全排除字段映射 private String tempData; // 用于临时计算的字段(如DTO扩展字段) ``` - **作用**:该字段**会参与任何SQL语句生成**,包括SELECT/INSERT/UPDATE[^1] - **典型场景**:前端传递的验证码字段、动态计算的业务字段 #### 2. 查询屏蔽(数据库有列但返回) ```java @TableField(select = false) // 仅屏蔽SELECT查询映射 private String password; // 数据库有password列但查询时返回 ``` - **特点**:INSERT/UPDATE仍会包含该字段,适用于敏感字段保护[^3] - **对比实验**: ```sql -- 未加注解时生成的SQL: SELECT id, username, password FROM user -- 添加注解后: SELECT id, username FROM user ``` #### 3. 条件屏蔽(动态控制映射) ```java @TableField(condition = SqlCondition.LIKE) // 仅WHERE条件中生效 private String searchKey; @TableField(insertStrategy = FieldStrategy.NOT_EMPTY) // 非空时插入 private String remark; ``` - **策略枚举**: | 策略类型 | 作用范围 | 常用值示例 | |-------------------|--------------------|------------------------------| | `FieldStrategy` | CRUD操作字段策略 | NOT_NULL, IGNORED, NEVER | | `SqlCondition` | WHERE条件生成规则 | LIKE, NOT_EQUAL, IS_NOT_NULL | --- ### 二、解决Unknown column错误的4步方案 #### 步骤1:验证命名策略配置 ```yaml # application.yml 关键配置 mybatis-plus: configuration: map-underscore-to-camel-case: true # 开启驼峰转换 global-config: db-config: column-format: "%s" # 列名格式化规则 table-prefix: t_ # 表前缀自动识别 ``` #### 步骤2:检查字段特殊字符 ```java // 错误案例:字段名含连续大写字母 private String userID; // 默认映射为user_i_d // 解决方案①:强制指定列名 @TableField("user_id") private String userID; // 解决方案②:修改全局命名策略(需自定义命名器) ``` #### 步骤3:静态字段处理 ```java private static final String VERSION = "v1"; // 默认会被扫描 @TableField(exist = false) private static String cacheData; // 必须显式添加注解 ``` #### 步骤4:日志验证SQL生成 ```yaml # 开启SQL日志分析 logging: level: com.baomidou.mybatisplus: DEBUG ``` - **典型错误日志分析**: ``` ==> Preparing: SELECT id,for_md_date FROM document # 错误点:for_md_date未映射 ``` --- ### 三、高级屏蔽场景解决方案 #### 1. 动态表名字段屏蔽 ```java @TableField(exist = false) private String tableNameSuffix; // 动态表名参与字段映射 public String getActualTableName() { return "log_" + tableNameSuffix; } ``` #### 2. 继承关系的字段控制 ```java public class BaseEntity { @TableField(exist = false) private String commonField; // 子类自动继承屏蔽状态 } public class User extends BaseEntity { // 其他字段... } ``` #### 3. 混合注解使用 ```java @TableField(value = "is_deleted", select = false, updateStrategy = FieldStrategy.NEVER) private Boolean deleted; // 多重控制:映射列名、查询屏蔽、禁止更新 ``` --- ### 四、验证工具推荐 1. **MyBatis-Plus元数据检查**: ```java TableInfo tableInfo = TableInfoHelper.getTableInfo(User.class); tableInfo.getFieldList().forEach(field -> System.out.println(field.getProperty() + " -> " + field.getColumn())); ``` 2. **H2内存数据库单元测试**: ```java @SpringBootTest public class FieldMappingTest { @Autowired private UserMapper userMapper; @Test void testSelectExclusion() { userMapper.selectById(1L); // 观察生成SQL是否包含屏蔽字段 } } ``` --- ### 五、指南 1. **Lombok兼容性问题**: - 使用`@Data`注解时,确保编译器支持字段注解继承 - 建议显式生成`getter/setter`方法免注解失效 2. **JSON序列化影响**: - `@TableField(select = false)`**影响Jackson序列化**,需额外使用`@JsonIgnore` 3. **TypeHandler冲突**: ```java // 错误示例:注解优先级问题 @TableField(typeHandler = JsonTypeHandler.class) @TableField(exist = false) // 两个注解冲突 private Map<String, Object> extraInfo; ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值