Lombok

Posted lingcheng7777

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lombok相关的知识,希望对你有一定的参考价值。

  • 引入lombok的jar包
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>

 

  • @EqualsAndHashCode

生成hashCode()和equals()方法,默认情况下,它将使用所有非静态,非transient字段。但可以通过在可选的exclude参数中来排除更多字段。或者,通过在parameter参数中命名它们来准确指定希望使用哪些字段。

技术图片
import java.util.Arrays;
public class EqualsAndHashCodeExample 
 private transient int transientVar = 10;
 private String name;
 private double score;
 private Shape shape = new Square(5, 10);
 private String[] tags;
 private transient int id;
 
 public String getName() 
   return this.name;
 
 
 @Override public boolean equals(Object o) 
   if (o == this) return true;
   if (!(o instanceof EqualsAndHashCodeExample)) return false;
   EqualsAndHashCodeExample other = (EqualsAndHashCodeExample) o;
   if (!other.canEqual((Object)this)) return false;
   if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
   if (Double.compare(this.score, other.score) != 0) return false;
   if (!Arrays.deepEquals(this.tags, other.tags)) return false;
   return true;
 
 
 @Override public int hashCode() 
   final int PRIME = 59;
   int result = 1;
   final long temp1 = Double.doubleToLongBits(this.score);
   result = (result*PRIME) + (this.name == null ? 43 : this.name.hashCode());
   result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
   result = (result*PRIME) + Arrays.deepHashCode(this.tags);
   return result;
 
 
 protected boolean canEqual(Object other) 
   return other instanceof EqualsAndHashCodeExample;
 
 
 public static class Square extends Shape 
   private final int width, height;
   
   public Square(int width, int height) 
     this.width = width;
     this.height = height;
   
   
   @Override public boolean equals(Object o) 
     if (o == this) return true;
     if (!(o instanceof Square)) return false;
     Square other = (Square) o;
     if (!other.canEqual((Object)this)) return false;
     if (!super.equals(o)) return false;
     if (this.width != other.width) return false;
     if (this.height != other.height) return false;
     return true;
   
   
   @Override public int hashCode() 
     final int PRIME = 59;
     int result = 1;
     result = (result*PRIME) + super.hashCode();
     result = (result*PRIME) + this.width;
     result = (result*PRIME) + this.height;
     return result;
   
   
   protected boolean canEqual(Object other) 
     return other instanceof Square;
   
 
View Code

 

 

 

  • @Accessors

@Accessors 主要用于控制生成的getter和setter
主要参数介绍

fluent boolean值,默认为false。此字段主要为控制生成的getter和setter方法前面是否带get/set
chain boolean值,默认false。如果设置为true,setter返回的是此对象,方便链式调用方法
prefix 设置前缀 例如:@Accessors(prefix = "abc") private String abcAge 当生成get/set方法时,会把此前缀去掉

  • @Data

@Data 包含了 @ToString、@EqualsAndHashCode、@Getter / @Setter和@RequiredArgsConstructor的功能

关于注解@Data报错问题可以参照https://blog.csdn.net/feinifi/article/details/85275280

 

以上是关于Lombok的主要内容,如果未能解决你的问题,请参考以下文章

lombok的@Data注解

Mybatis持久层框架 | Lombok搭建