简介

大部分项目中都必不可少的包含数据库实体(Entity)、数据载体(dto,dataObject),而这两部分都包含着大量的没有业务逻辑的setter、getter、空参构造,同时我们一般要复写类的toString(),equals(),hashCode()方法(贫血模型)。这些工作都是重复性的工作,作为程序员,懒是必备素质之一,这些工作肯定已经有大牛封装好了处理方法,这就是lombok。

idea 安装插件,支持lombok

lombok是在编译阶段才生成相应的代码体,所以在项目中直接调用setter,getter,constructor会报错,这时候可以在IDE安装相应的插件支持lombok。这里介绍idea插件安装,eclipse请自行百度。

安装方法

  1. 进入设置页面(windows:setting,Mac:Preferences)
  2. 点击Plugin
  3. Browse repositories
  4. 搜索lombok
  5. 点击Install
  6. 安装完毕后开启注解权限才能正常使用: 
    • –>setting
    • –>Build,Execution,Deployment
    • –>Compiler
    • –>Annontation Processors
    • –>勾选Enable annotation processing
    • –> Apply
  7. 重启Idea

引入方法

gradle

  1. // https://mvnrepository.com/artifact/org.projectlombok/lombok
  2. compile group: 'org.projectlombok', name: 'lombok', version: '1.16.16'

maven

  1. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  2. <dependency>
  3. <groupId>org.projectlombok</groupId>
  4. <artifactId>lombok</artifactId>
  5. <version>1.16.16</version>
  6. </dependency>

常用方法

@Setter

生成setter方法,final变量不包含

  1. //原始类
  2. @Setter
  3. public class TestEntity {
  4.  
  5. private String name;
  6.  
  7. private Integer age;
  8.  
  9. private final String type = "type";
  10. }
  11. //反编译的类
  12. public class TestEntity {
  13. private String name;
  14. private Integer age;
  15. private final String type = "person";
  16.  
  17. public TestEntity() {
  18. }
  19.  
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23.  
  24. public void setAge(Integer age) {
  25. this.age = age;
  26. }
  27. }

@Getter

生成getter方法,final变量不包含

  1. //原始类
  2. @Getter
  3. public class TestEntity {
  4.  
  5. private String name;
  6.  
  7. private Integer age;
  8.  
  9. private final String type = "person";
  10. }
  11. //反编译的类
  12. public class TestEntity {
  13. private String name;
  14. private Integer age;
  15. private final String type = "person";
  16.  
  17. public TestEntity() {
  18. }
  19.  
  20. public String getName() {
  21. return this.name;
  22. }
  23.  
  24. public Integer getAge() {
  25. return this.age;
  26. }
  27.  
  28. public String getType() {
  29. this.getClass();
  30. return "person";
  31. }
  32. }

@NoArgsConstructor

生成空参构造

  1. //原始类
  2. @NoArgsConstructor
  3. public class TestEntity {
  4.  
  5. private String name;
  6.  
  7. private Integer age;
  8.  
  9. private final String type = "person";
  10. }
  11. //反编译的类
  12. public class TestEntity {
  13. private String name;
  14. private Integer age;
  15. private final String type = "person";
  16.  
  17. public TestEntity() {
  18. }
  19. }

@AllArgsConstructor

生成全部参数构造

  1. //原始类
  2. @AllArgsConstructor
  3. public class TestEntity {
  4.  
  5. private String name;
  6.  
  7. private Integer age;
  8.  
  9. private final String type = "person";
  10. }
  11. //反编译的类
  12. public class TestEntity {
  13. private String name;
  14. private Integer age;
  15. private final String type = "person";
  16.  
  17. @ConstructorProperties({"name", "age"})
  18. public TestEntity(String name, Integer age) {
  19. this.name = name;
  20. this.age = age;
  21. }
  22. }

@RequiredArgsConstructor

将标记为@NoNull的属性生成一个构造器

如果运行中标记为@NoNull的属性为null,会抛出空指针异常。

  1. //原始类
  2. @RequiredArgsConstructor
  3. public class TestEntity {
  4.  
  5. private String name;
  6. @NonNull
  7. private Integer age;
  8.  
  9. private final String type = "person";
  10. }
  11. //反编译的类
  12. public class TestEntity {
  13. private String name;
  14. @NonNull
  15. private Integer age;
  16. private final String type = "person";
  17.  
  18. @ConstructorProperties({"age"})
  19. public TestEntity(@NonNull Integer age) {
  20. if(age == null) {
  21. throw new NullPointerException("age");
  22. } else {
  23. this.age = age;
  24. }
  25. }
  26. }

@ToString

生成所有属性的toString()方法

  1. //原始类
  2. @ToString
  3. public class TestEntity {
  4.  
  5. private String name;
  6.  
  7. private Integer age;
  8.  
  9. private final String type = "person";
  10. }
  11. //反编译的类
  12. public class TestEntity {
  13. private String name;
  14. private Integer age;
  15. private final String type = "person";
  16.  
  17. public TestEntity() {
  18. }
  19.  
  20. public String toString() {
  21. StringBuilder var10000 = (new StringBuilder()).append("TestEntity(name=").append(this.name).append(", age=").append(this.age).append(", type=");
  22. this.getClass();
  23. return var10000.append("person").append(")").toString();
  24. }
  25. }

@EqualsAndHashCode

生成equals()方法和hashCode方法

  1. //原始类
  2. @EqualsAndHashCode
  3. public class TestEntity {
  4.  
  5. private String name;
  6.  
  7. private Integer age;
  8.  
  9. private final String type = "person";
  10. }
  11. //反编译的类
  12. public class TestEntity {
  13. private String name;
  14. private Integer age;
  15. private final String type = "person";
  16.  
  17. public TestEntity() {
  18. }
  19.  
  20. public boolean equals(Object o) {
  21. if(o == this) {
  22. return true;
  23. } else if(!(o instanceof TestEntity)) {
  24. return false;
  25. } else {
  26. TestEntity other = (TestEntity)o;
  27. if(!other.canEqual(this)) {
  28. return false;
  29. } else {
  30. label47: {
  31. String this$name = this.name;
  32. String other$name = other.name;
  33. if(this$name == null) {
  34. if(other$name == null) {
  35. break label47;
  36. }
  37. } else if(this$name.equals(other$name)) {
  38. break label47;
  39. }
  40.  
  41. return false;
  42. }
  43.  
  44. Integer this$age = this.age;
  45. Integer other$age = other.age;
  46. if(this$age == null) {
  47. if(other$age != null) {
  48. return false;
  49. }
  50. } else if(!this$age.equals(other$age)) {
  51. return false;
  52. }
  53.  
  54. this.getClass();
  55. String this$type = "person";
  56. other.getClass();
  57. String other$type = "person";
  58. if(this$type == null) {
  59. if(other$type != null) {
  60. return false;
  61. }
  62. } else if(!this$type.equals(other$type)) {
  63. return false;
  64. }
  65.  
  66. return true;
  67. }
  68. }
  69. }
  70.  
  71. protected boolean canEqual(Object other) {
  72. return other instanceof TestEntity;
  73. }
  74.  
  75. public int hashCode() {
  76. boolean PRIME = true;
  77. byte result = 1;
  78. String $name = this.name;
  79. int result1 = result * 59 + ($name == null?43:$name.hashCode());
  80. Integer $age = this.age;
  81. result1 = result1 * 59 + ($age == null?43:$age.hashCode());
  82. this.getClass();
  83. String $type = "person";
  84. result1 = result1 * 59 + ($type == null?43:$type.hashCode());
  85. return result1;
  86. }
  87. }

@Data(常用)

@Data直接修饰POJO or beans, getter所有的变量,setter所有不为final的变量。如果你不需要默认的生成方式,直接填写你需要的annotation的就可以了。默认生成的所有的annotation都是public的,如果需要不同权限修饰符可以使用AccessLevel.NONE选项。当然@Data 也可以使用staticConstructor选项生成一个静态方法。

=@Setter+@Getter+@EqualsAndHashCode+@NoArgsConstructor

  1. //原始类
  2. @Data
  3. public class TestEntity {
  4. @Setter(AccessLevel.PRIVATE)
  5. private String name;
  6.  
  7. private Integer age;
  8.  
  9. private final String type = "person";
  10. }
  11. //反编译的类
  12.  
  13. public class TestEntity {
  14. private String name;
  15. private Integer age;
  16. private final String type = "person";
  17.  
  18. public TestEntity() {
  19. }
  20.  
  21. public String getName() {
  22. return this.name;
  23. }
  24.  
  25. public Integer getAge() {
  26. return this.age;
  27. }
  28.  
  29. public String getType() {
  30. this.getClass();
  31. return "person";
  32. }
  33.  
  34. public void setAge(Integer age) {
  35. this.age = age;
  36. }
  37.  
  38. public boolean equals(Object o) {
  39. if(o == this) {
  40. return true;
  41. } else if(!(o instanceof TestEntity)) {
  42. return false;
  43. } else {
  44. TestEntity other = (TestEntity)o;
  45. if(!other.canEqual(this)) {
  46. return false;
  47. } else {
  48. label47: {
  49. String this$name = this.getName();
  50. String other$name = other.getName();
  51. if(this$name == null) {
  52. if(other$name == null) {
  53. break label47;
  54. }
  55. } else if(this$name.equals(other$name)) {
  56. break label47;
  57. }
  58.  
  59. return false;
  60. }
  61.  
  62. Integer this$age = this.getAge();
  63. Integer other$age = other.getAge();
  64. if(this$age == null) {
  65. if(other$age != null) {
  66. return false;
  67. }
  68. } else if(!this$age.equals(other$age)) {
  69. return false;
  70. }
  71.  
  72. String this$type = this.getType();
  73. String other$type = other.getType();
  74. if(this$type == null) {
  75. if(other$type != null) {
  76. return false;
  77. }
  78. } else if(!this$type.equals(other$type)) {
  79. return false;
  80. }
  81.  
  82. return true;
  83. }
  84. }
  85. }
  86.  
  87. protected boolean canEqual(Object other) {
  88. return other instanceof TestEntity;
  89. }
  90.  
  91. public int hashCode() {
  92. boolean PRIME = true;
  93. byte result = 1;
  94. String $name = this.getName();
  95. int result1 = result * 59 + ($name == null?43:$name.hashCode());
  96. Integer $age = this.getAge();
  97. result1 = result1 * 59 + ($age == null?43:$age.hashCode());
  98. String $type = this.getType();
  99. result1 = result1 * 59 + ($type == null?43:$type.hashCode());
  100. return result1;
  101. }
  102.  
  103. public String toString() {
  104. return "TestEntity(name=" + this.getName() + ", age=" + this.getAge() + ", type=" + this.getType() + ")";
  105. }
  106.  
  107. private void setName(String name) {
  108. this.name = name;
  109. }
  110. }

@Builder

构造Builder模式的结构。通过内部类Builder()进行构建对象。

  1. //原始类
  2. @Builder
  3. public class TestEntity {
  4.  
  5. private String name;
  6.  
  7. private Integer age;
  8.  
  9. private final String type = "person";
  10. }
  11. //反编译的类
  12. public class TestEntity {
  13. private String name;
  14. private Integer age;
  15. private final String type = "person";
  16.  
  17. @ConstructorProperties({"name", "age"})
  18. TestEntity(String name, Integer age) {
  19. this.name = name;
  20. this.age = age;
  21. }
  22.  
  23. public static TestEntity.TestEntityBuilder builder() {
  24. return new TestEntity.TestEntityBuilder();
  25. }
  26.  
  27. public static class TestEntityBuilder {
  28. private String name;
  29. private Integer age;
  30.  
  31. TestEntityBuilder() {
  32. }
  33.  
  34. public TestEntity.TestEntityBuilder name(String name) {
  35. this.name = name;
  36. return this;
  37. }
  38.  
  39. public TestEntity.TestEntityBuilder age(Integer age) {
  40. this.age = age;
  41. return this;
  42. }
  43.  
  44. public TestEntity build() {
  45. return new TestEntity(this.name, this.age);
  46. }
  47.  
  48. public String toString() {
  49. return "TestEntity.TestEntityBuilder(name=" + this.name + ", age=" + this.age + ")";
  50. }
  51. }
  52. }
  53.  
  54. //Builder模式使用方法
  55. @Test
  56. public void test(){
  57. TestEntity testEntity = TestEntity.builder()
  58. .name("java")
  59. .age(18)
  60. .build();
  61. }

@Value

与@Data相对应的@Value, 两个annotation的主要区别就是如果变量不加@NonFinal ,@Value会给所有的弄成final的。当然如果是final的话,就没有set方法了。

  1. //原始类
  2. @Value
  3. public class TestEntity {
  4. @Setter(AccessLevel.PRIVATE)
  5. private String name;
  6.  
  7. private Integer age;
  8.  
  9. private final String type = "person";
  10. }
  11. //反编译的类
  12. public final class TestEntity {
  13. private final String name;
  14. private final Integer age;
  15. private final String type = "person";
  16.  
  17. @ConstructorProperties({"name", "age"})
  18. public TestEntity(String name, Integer age) {
  19. this.name = name;
  20. this.age = age;
  21. }
  22.  
  23. public String getName() {
  24. return this.name;
  25. }
  26.  
  27. public Integer getAge() {
  28. return this.age;
  29. }
  30.  
  31. public String getType() {
  32. this.getClass();
  33. return "person";
  34. }
  35.  
  36. public boolean equals(Object o) {
  37. if(o == this) {
  38. return true;
  39. } else if(!(o instanceof TestEntity)) {
  40. return false;
  41. } else {
  42. TestEntity other;
  43. label44: {
  44. other = (TestEntity)o;
  45. String this$name = this.getName();
  46. String other$name = other.getName();
  47. if(this$name == null) {
  48. if(other$name == null) {
  49. break label44;
  50. }
  51. } else if(this$name.equals(other$name)) {
  52. break label44;
  53. }
  54.  
  55. return false;
  56. }
  57.  
  58. Integer this$age = this.getAge();
  59. Integer other$age = other.getAge();
  60. if(this$age == null) {
  61. if(other$age != null) {
  62. return false;
  63. }
  64. } else if(!this$age.equals(other$age)) {
  65. return false;
  66. }
  67.  
  68. String this$type = this.getType();
  69. String other$type = other.getType();
  70. if(this$type == null) {
  71. if(other$type != null) {
  72. return false;
  73. }
  74. } else if(!this$type.equals(other$type)) {
  75. return false;
  76. }
  77.  
  78. return true;
  79. }
  80. }
  81.  
  82. public int hashCode() {
  83. boolean PRIME = true;
  84. byte result = 1;
  85. String $name = this.getName();
  86. int result1 = result * 59 + ($name == null?43:$name.hashCode());
  87. Integer $age = this.getAge();
  88. result1 = result1 * 59 + ($age == null?43:$age.hashCode());
  89. String $type = this.getType();
  90. result1 = result1 * 59 + ($type == null?43:$type.hashCode());
  91. return result1;
  92. }
  93.  
  94. public String toString() {
  95. return "TestEntity(name=" + this.getName() + ", age=" + this.getAge() + ", type=" + this.getType() + ")";
  96. }
  97. }

@Synchronized

同步方法

  1. //原始类
  2. public class TestEntity {
  3. private String name;
  4.  
  5. private Integer age;
  6.  
  7. private final String type = "person";
  8. @Synchronized
  9. public void write(){
  10. //do something
  11. }
  12. }
  13. //反编译的类
  14. public class TestEntity {
  15. private final Object $lock = new Object[0];
  16. private String name;
  17. private Integer age;
  18. private final String type = "person";
  19.  
  20. public TestEntity() {
  21. }
  22.  
  23. public void write() {
  24. Object var1 = this.$lock;
  25. synchronized(this.$lock) {
  26. ;
  27. }
  28. }
  29. }

@Cleanup @@SneakyThrows

自动调用close方法关闭资源。

  1. //原始类
  2. public class TestEntity {
  3. private String name;
  4.  
  5. private Integer age;
  6.  
  7. private final String type = "person";
  8.  
  9. @SneakyThrows
  10. public void outputStream(){
  11. @Cleanup OutputStream outputStream = new FileOutputStream(new File("/Users/hello"));
  12. }
  13. }
  14. //反编译的类
  15. public class TestEntity {
  16. private String name;
  17. private Integer age;
  18. private final String type = "person";
  19.  
  20. public TestEntity() {
  21. }
  22.  
  23. public void outputStream() {
  24. try {
  25. FileOutputStream $ex = new FileOutputStream(new File("/Users/hello"));
  26. if(Collections.singletonList($ex).get(0) != null) {
  27. $ex.close();
  28. }
  29.  
  30. } catch (Throwable var2) {
  31. throw var2;
  32. }
  33. }
  34. }

原文链接:https://blog.csdn.net/u013225178/article/details/80721799

lombok使用及常用注解的更多相关文章

  1. Eclipse安装lombok及常用注解

    转自:https://blog.csdn.net/ZJDWHD/article/details/77795023 lombok的官方网址:http://projectlombok.org/ https ...

  2. eclipse安装lombok和常用注解使用

    1.下载lombok.jar lombok 的官方网址:http://projectlombok.org/   2.运行lombok.jar: java -jar  D:\eclipse-luna\l ...

  3. 20190905 Lombok常用注解

    Lombok常用注解 val 用于声明类型,将从初始化表达式推断出类型,仅适用于局部变量和foreach循环,而不适用于字段.声明的局部变量为final变量. Java自带类型推断随着JDK版本提升越 ...

  4. Lombok 常用注解

    Lombok Lombok 能以简单的注解形式来简化 java 代码,提高开发人员的开发效率.例如开发中经常需要写的 javaBean,都需要花时间去添加相应的 getter/setter,也许还要去 ...

  5. lombok 简化java代码注解

    lombok 简化java代码注解 安装lombok插件 以intellij ide为例 File-->Setting-->Plugins-->搜索"lombok plug ...

  6. Spring Boot常用注解

    SpringBoot注解大全   一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@Enable ...

  7. lombok 中的@Data注解

    今天看到有代码中的Dao包中的类文件,写的极其简洁,甚至引起了开发工具InteliJ的报错,然后程序还能稳健地跑起来. import lombok.Data; @Data public class V ...

  8. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  9. SpringMVC常用注解實例詳解3:@ResponseBody

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

随机推荐

  1. sql 循环转移备份数据

    --创建表结构 select top 1 * into ATable_20190710 from ATable --转移表数据 insert into ATable_20190710 select t ...

  2. centos7 追加python3 + 使用pip + virtualenv

    一.安装Python3的方法: 首先安装依赖包: yum -y groupinstall "Development tools" yum -y install zlib-devel ...

  3. Java 概述和编程基础

    First of all,Java概述: 类是Java程序设计的基石和基本单元: main()方法是程序的入口,它是共有的.静态的,参数String[] args表示一个字符串数组可以传入该程序,用来 ...

  4. L1-025. 正整数A+B 简单复习一下,。

    本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B,其间以空格分开.问题是A和B不一定是满 ...

  5. C#求1-100的质数,100-1000的水仙花数,1-100所有的平方和平方平方根

    //你们的鼓励是我最大的动力 大家可以多留言评论  在接下来很长一段时间我会从初级到高级每天更新 大家有想学习的内容也可以留言哦 //现在是我做C#老师的第28天,希望和大家一起努力 加油 using ...

  6. Left4Dead2 LAN Online

    Left4Dead2 LAN Online Franklin vs Wolverine 求生之路 局域网联机说明 ============================ 局域网联机方法: 1.先找到 ...

  7. ubuntu 网卡名称重命名

    ubuntu 网卡名称重命名 参考:https://blog.csdn.net/hzj_001/article/details/81587824 biosdevname 和 net.ifnames 两 ...

  8. Ubuntu14.04更新硬件实现堆栈(HWE)

    Ubuntu14.04更新硬件实现堆栈(HWE) 来源: https://github.com/gatieme/AderXCoding/tree/master/system/tools/ubuntu_ ...

  9. 【其他】BootCDN

    BootCDN 稳定.快速.免费的前端开源项目 CDN 加速服务 是 Bootstrap 中文网支持并维护的前端开源项目免费 CDN 服务,致力于为 Bootstrap.jQuery.Angular. ...

  10. loj 3014「JOI 2019 Final」独特的城市

    loj 我本来是直接口胡了一个意思一样的做法的,但是因为觉得有点假+实现要用并查集(?)就卡了好一会儿... 对于一个点\(x\)来说,独特的点一定在它的最长链上,如果有独特的点不在最长链上,那么最长 ...