簡介
大部分項目中都必不可少的包含數據庫實體(Entity)、數據載體(dto,dataObject),而這兩部分都包含着大量的沒有業務邏輯的setter、getter、空參構造,同時我們一般要復寫類的toString(),equals(),hashCode()方法(貧血模型)。這些工作都是重復性的工作,作為程序員,懶是必備素質之一,這些工作肯定已經有大牛封裝好了處理方法,這就是lombok。
idea 安裝插件,支持lombok
lombok是在編譯階段才生成相應的代碼體,所以在項目中直接調用setter,getter,constructor會報錯,這時候可以在IDE安裝相應的插件支持lombok。這里介紹idea插件安裝,eclipse請自行百度。
安裝方法
- 進入設置頁面(windows:setting,Mac:Preferences)
- 點擊Plugin
- Browse repositories
- 搜索lombok
- 點擊Install
- 安裝完畢后開啟注解權限才能正常使用:
- –>setting
- –>Build,Execution,Deployment
- –>Compiler
- –>Annontation Processors
- –>勾選
Enable annotation processing
- –> Apply
- 重啟Idea
引入方法
gradle
// https://mvnrepository.com/artifact/org.projectlombok/lombok compile group: 'org.projectlombok', name: 'lombok', version: '1.16.16'
maven
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.16</version> </dependency>常用方法
@Setter
生成setter方法,final變量不包含
//原始類 @Setter public class TestEntity { private String name; private Integer age; private final String type = "type"; } //反編譯的類 public class TestEntity { private String name; private Integer age; private final String type = "person"; public TestEntity() { } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } }@Getter
生成getter方法,final變量不包含
//原始類 @Getter public class TestEntity { private String name; private Integer age; private final String type = "person"; } //反編譯的類 public class TestEntity { private String name; private Integer age; private final String type = "person"; public TestEntity() { } public String getName() { return this.name; } public Integer getAge() { return this.age; } public String getType() { this.getClass(); return "person"; } }@NoArgsConstructor
生成空參構造
//原始類 @NoArgsConstructor public class TestEntity { private String name; private Integer age; private final String type = "person"; } //反編譯的類 public class TestEntity { private String name; private Integer age; private final String type = "person"; public TestEntity() { } }@AllArgsConstructor
生成全部參數構造
//原始類 @AllArgsConstructor public class TestEntity { private String name; private Integer age; private final String type = "person"; } //反編譯的類 public class TestEntity { private String name; private Integer age; private final String type = "person"; @ConstructorProperties({"name", "age"}) public TestEntity(String name, Integer age) { this.name = name; this.age = age; } }@RequiredArgsConstructor
將標記為@NoNull的屬性生成一個構造器
如果運行中標記為@NoNull的屬性為null,會拋出空指針異常。
//原始類 @RequiredArgsConstructor public class TestEntity { private String name; @NonNull private Integer age; private final String type = "person"; } //反編譯的類 public class TestEntity { private String name; @NonNull private Integer age; private final String type = "person"; @ConstructorProperties({"age"}) public TestEntity(@NonNull Integer age) { if(age == null) { throw new NullPointerException("age"); } else { this.age = age; } } }@ToString
生成所有屬性的toString()方法
//原始類 @ToString public class TestEntity { private String name; private Integer age; private final String type = "person"; } //反編譯的類 public class TestEntity { private String name; private Integer age; private final String type = "person"; public TestEntity() { } public String toString() { StringBuilder var10000 = (new StringBuilder()).append("TestEntity(name=").append(this.name).append(", age=").append(this.age).append(", type="); this.getClass(); return var10000.append("person").append(")").toString(); } }@EqualsAndHashCode
生成equals()方法和hashCode方法
//原始類 @EqualsAndHashCode public class TestEntity { private String name; private Integer age; private final String type = "person"; } //反編譯的類 public class TestEntity { private String name; private Integer age; private final String type = "person"; public TestEntity() { } public boolean equals(Object o) { if(o == this) { return true; } else if(!(o instanceof TestEntity)) { return false; } else { TestEntity other = (TestEntity)o; if(!other.canEqual(this)) { return false; } else { label47: { String this$name = this.name; String other$name = other.name; if(this$name == null) { if(other$name == null) { break label47; } } else if(this$name.equals(other$name)) { break label47; } return false; } Integer this$age = this.age; Integer other$age = other.age; if(this$age == null) { if(other$age != null) { return false; } } else if(!this$age.equals(other$age)) { return false; } this.getClass(); String this$type = "person"; other.getClass(); String other$type = "person"; if(this$type == null) { if(other$type != null) { return false; } } else if(!this$type.equals(other$type)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof TestEntity; } public int hashCode() { boolean PRIME = true; byte result = 1; String $name = this.name; int result1 = result * 59 + ($name == null?43:$name.hashCode()); Integer $age = this.age; result1 = result1 * 59 + ($age == null?43:$age.hashCode()); this.getClass(); String $type = "person"; result1 = result1 * 59 + ($type == null?43:$type.hashCode()); return result1; } }@Data(常用)
@Data直接修飾POJO or beans, getter所有的變量,setter所有不為final的變量。如果你不需要默認的生成方式,直接填寫你需要的annotation的就可以了。默認生成的所有的annotation都是public的,如果需要不同權限修飾符可以使用AccessLevel.NONE選項。當然@Data 也可以使用staticConstructor選項生成一個靜態方法。
=@Setter+@Getter+@EqualsAndHashCode+@NoArgsConstructor
//原始類 @Data public class TestEntity { @Setter(AccessLevel.PRIVATE) private String name; private Integer age; private final String type = "person"; } //反編譯的類 public class TestEntity { private String name; private Integer age; private final String type = "person"; public TestEntity() { } public String getName() { return this.name; } public Integer getAge() { return this.age; } public String getType() { this.getClass(); return "person"; } public void setAge(Integer age) { this.age = age; } public boolean equals(Object o) { if(o == this) { return true; } else if(!(o instanceof TestEntity)) { return false; } else { TestEntity other = (TestEntity)o; if(!other.canEqual(this)) { return false; } else { label47: { String this$name = this.getName(); String other$name = other.getName(); if(this$name == null) { if(other$name == null) { break label47; } } else if(this$name.equals(other$name)) { break label47; } return false; } Integer this$age = this.getAge(); Integer other$age = other.getAge(); if(this$age == null) { if(other$age != null) { return false; } } else if(!this$age.equals(other$age)) { return false; } String this$type = this.getType(); String other$type = other.getType(); if(this$type == null) { if(other$type != null) { return false; } } else if(!this$type.equals(other$type)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof TestEntity; } public int hashCode() { boolean PRIME = true; byte result = 1; String $name = this.getName(); int result1 = result * 59 + ($name == null?43:$name.hashCode()); Integer $age = this.getAge(); result1 = result1 * 59 + ($age == null?43:$age.hashCode()); String $type = this.getType(); result1 = result1 * 59 + ($type == null?43:$type.hashCode()); return result1; } public String toString() { return "TestEntity(name=" + this.getName() + ", age=" + this.getAge() + ", type=" + this.getType() + ")"; } private void setName(String name) { this.name = name; } }@Builder
構造Builder模式的結構。通過內部類Builder()進行構建對象。
//原始類 @Builder public class TestEntity { private String name; private Integer age; private final String type = "person"; } //反編譯的類 public class TestEntity { private String name; private Integer age; private final String type = "person"; @ConstructorProperties({"name", "age"}) TestEntity(String name, Integer age) { this.name = name; this.age = age; } public static TestEntity.TestEntityBuilder builder() { return new TestEntity.TestEntityBuilder(); } public static class TestEntityBuilder { private String name; private Integer age; TestEntityBuilder() { } public TestEntity.TestEntityBuilder name(String name) { this.name = name; return this; } public TestEntity.TestEntityBuilder age(Integer age) { this.age = age; return this; } public TestEntity build() { return new TestEntity(this.name, this.age); } public String toString() { return "TestEntity.TestEntityBuilder(name=" + this.name + ", age=" + this.age + ")"; } } } //Builder模式使用方法 @Test public void test(){ TestEntity testEntity = TestEntity.builder() .name("java") .age(18) .build(); }@Value
與@Data相對應的@Value, 兩個annotation的主要區別就是如果變量不加@NonFinal ,@Value會給所有的弄成final的。當然如果是final的話,就沒有set方法了。
//原始類 @Value public class TestEntity { @Setter(AccessLevel.PRIVATE) private String name; private Integer age; private final String type = "person"; } //反編譯的類 public final class TestEntity { private final String name; private final Integer age; private final String type = "person"; @ConstructorProperties({"name", "age"}) public TestEntity(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return this.name; } public Integer getAge() { return this.age; } public String getType() { this.getClass(); return "person"; } public boolean equals(Object o) { if(o == this) { return true; } else if(!(o instanceof TestEntity)) { return false; } else { TestEntity other; label44: { other = (TestEntity)o; String this$name = this.getName(); String other$name = other.getName(); if(this$name == null) { if(other$name == null) { break label44; } } else if(this$name.equals(other$name)) { break label44; } return false; } Integer this$age = this.getAge(); Integer other$age = other.getAge(); if(this$age == null) { if(other$age != null) { return false; } } else if(!this$age.equals(other$age)) { return false; } String this$type = this.getType(); String other$type = other.getType(); if(this$type == null) { if(other$type != null) { return false; } } else if(!this$type.equals(other$type)) { return false; } return true; } } public int hashCode() { boolean PRIME = true; byte result = 1; String $name = this.getName(); int result1 = result * 59 + ($name == null?43:$name.hashCode()); Integer $age = this.getAge(); result1 = result1 * 59 + ($age == null?43:$age.hashCode()); String $type = this.getType(); result1 = result1 * 59 + ($type == null?43:$type.hashCode()); return result1; } public String toString() { return "TestEntity(name=" + this.getName() + ", age=" + this.getAge() + ", type=" + this.getType() + ")"; } }@Synchronized
同步方法
//原始類 public class TestEntity { private String name; private Integer age; private final String type = "person"; @Synchronized public void write(){ //do something } } //反編譯的類 public class TestEntity { private final Object $lock = new Object[0]; private String name; private Integer age; private final String type = "person"; public TestEntity() { } public void write() { Object var1 = this.$lock; synchronized(this.$lock) { ; } } }@Cleanup @@SneakyThrows
自動調用close方法關閉資源。
//原始類 public class TestEntity { private String name; private Integer age; private final String type = "person"; @SneakyThrows public void outputStream(){ @Cleanup OutputStream outputStream = new FileOutputStream(new File("/Users/hello")); } } //反編譯的類 public class TestEntity { private String name; private Integer age; private final String type = "person"; public TestEntity() { } public void outputStream() { try { FileOutputStream $ex = new FileOutputStream(new File("/Users/hello")); if(Collections.singletonList($ex).get(0) != null) { $ex.close(); } } catch (Throwable var2) { throw var2; } } }
原文鏈接:https://blog.csdn.net/u013225178/article/details/80721799