Intellij IDEA 安装 lombok (本地安装/在线安装)及使用详解

时间:2024-04-01 14:52:34

一 本地安装lombok
 

1.下载安装lombok zip包

下载地址:https://github.com/mplushnikov/lombok-intellij-plugin/releases
Intellij IDEA 安装 lombok (本地安装/在线安装)及使用详解

2.下载好后放在任意盘下

3.进入 file->settings -> idea Annotation processors

勾选

Intellij IDEA 安装 lombok (本地安装/在线安装)及使用详解

4.进入 Plugins - >Install plugin from disk…

选择下载的zip包

Intellij IDEA 安装 lombok (本地安装/在线安装)及使用详解

5.选择后点击ok,然后重启idea即可

二 在线安装lombok

1.进入 file->settings -> Plugins

Intellij IDEA 安装 lombok (本地安装/在线安装)及使用详解

5.结束后重启idea即可

三 lombok的使用

1.pom文件加入

 
  1. <dependency>
  2. <groupId>org.projectlombok</groupId>
  3. <artifactId>lombok</artifactId>
  4. <version>1.18.4</version>
  5. </dependency>

2.@Data 注解

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

官方实例如下:

 
  1. import lombok.AccessLevel;
  2. import lombok.Setter;
  3. import lombok.Data;
  4. import lombok.ToString;
  5. @Data
  6. public class DataExample {
  7. private final String name;
  8. @Setter(AccessLevel.PACKAGE) private int age;
  9. private double score;
  10. private String[] tags;
  11. @ToString(includeFieldNames=true)
  12. @Data(staticConstructor="of")
  13. public static class Exercise<T> {
  14. private final String name;
  15. private final T value;
  16. }
  17. }

如不使用Lombok,则实现如下:

 
  1. import java.util.Arrays;
  2.  
  3. public class DataExample {
  4. private final String name;
  5. private int age;
  6. private double score;
  7. private String[] tags;
  8.  
  9. public DataExample(String name) {
  10. this.name = name;
  11. }
  12.  
  13. public String getName() {
  14. return this.name;
  15. }
  16.  
  17. void setAge(int age) {
  18. this.age = age;
  19. }
  20.  
  21. public int getAge() {
  22. return this.age;
  23. }
  24.  
  25. public void setScore(double score) {
  26. this.score = score;
  27. }
  28.  
  29. public double getScore() {
  30. return this.score;
  31. }
  32.  
  33. public String[] getTags() {
  34. return this.tags;
  35. }
  36.  
  37. public void setTags(String[] tags) {
  38. this.tags = tags;
  39. }
  40.  
  41. @Override public String toString() {
  42. return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")";
  43. }
  44.  
  45. protected boolean canEqual(Object other) {
  46. return other instanceof DataExample;
  47. }
  48.  
  49. @Override public boolean equals(Object o) {
  50. if (o == this) return true;
  51. if (!(o instanceof DataExample)) return false;
  52. DataExample other = (DataExample) o;
  53. if (!other.canEqual((Object)this)) return false;
  54. if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
  55. if (this.getAge() != other.getAge()) return false;
  56. if (Double.compare(this.getScore(), other.getScore()) != 0) return false;
  57. if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
  58. return true;
  59. }
  60.  
  61. @Override public int hashCode() {
  62. final int PRIME = 59;
  63. int result = 1;
  64. final long temp1 = Double.doubleToLongBits(this.getScore());
  65. result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
  66. result = (result*PRIME) + this.getAge();
  67. result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
  68. result = (result*PRIME) + Arrays.deepHashCode(this.getTags());
  69. return result;
  70. }
  71.  
  72. public static class Exercise<T> {
  73. private final String name;
  74. private final T value;
  75.  
  76. private Exercise(String name, T value) {
  77. this.name = name;
  78. this.value = value;
  79. }
  80.  
  81. public static <T> Exercise<T> of(String name, T value) {
  82. return new Exercise<T>(name, value);
  83. }
  84.  
  85. public String getName() {
  86. return this.name;
  87. }
  88.  
  89. public T getValue() {
  90. return this.value;
  91. }
  92.  
  93. @Override public String toString() {
  94. return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")";
  95. }
  96.  
  97. protected boolean canEqual(Object other) {
  98. return other instanceof Exercise;
  99. }
  100.  
  101. @Override public boolean equals(Object o) {
  102. if (o == this) return true;
  103. if (!(o instanceof Exercise)) return false;
  104. Exercise<?> other = (Exercise<?>) o;
  105. if (!other.canEqual((Object)this)) return false;
  106. if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;
  107. if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;
  108. return true;
  109. }
  110.  
  111. @Override public int hashCode() {
  112. final int PRIME = 59;
  113. int result = 1;
  114. result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
  115. result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode());
  116. return result;
  117. }
  118. }