zoukankan      html  css  js  c++  java
  • IDEA安装使用Lombok插件

      项目中经常使用bean,entity等类,绝大部分数据类类中都需要get、set、toString、equals和hashCode方法,虽然IDEA开发环境下都有自动生成的快捷方式,但自动生成这些代码后,如果bean中的属性一旦有修改、删除或增加时,需要重新生成或删除get/set等方法,给代码维护增加负担。而使用了lombok则不一样,使用了lombok的注解(@Setter,@Getter,@ToString,@@RequiredArgsConstructor,@EqualsAndHashCode或@Data)之后,就不需要编写或生成get/set等方法,很大程度上减少了代码量,而且减少了代码维护的负担。故强烈建议项目中使用lombok,去掉bean中get、set、toString、equals和hashCode等方法的代码。

    安装

      

    使用

    引入pom

    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.18</version>
        <scope>provided</scope>
    </dependency>

    代码测试

    @Setter
    @Getter
    @ToString
    @EqualsAndHashCode
    public class Student {
        private String studentNo;
        private String name;
        private int age;
        private String male;
        
    }
    @Log
    public class LombokTest {
        public static void main(String[] args) {
            Student student = new Student();
            student.setAge(27);
            student.setMale("man");
            student.setName("lance");
            student.setStudentNo("2017");
            System.out.println(student.toString());
            Student student2 = new Student();
            student2.setAge(27);
            student2.setMale("man");
            student2.setName("lance");
            student2.setStudentNo("2017");
            System.out.println(student.equals(student2));
            student2.setStudentNo("2018");
            System.out.println(student.equals(student2));
            log.info("lombok test");
        }
    }
     输出
    Student(name=lance, age=27, male=man, studentNo=2017)
    true
    false
    lombok test
    可以看出,如果没有添加@Setter注解,则LombokTest中的student示例无法使用setAge()等方法。使用lombok之后,省去了许多没必要的get,set,toString,equals,hashCode代码,简化了代码编写,减少了代码量。
          另外@Data注解的作用相当于 @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode的合集。
          另外@Log 省去了在LombokTest中添加 getLogger的如下代码: 
    private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());


  • 相关阅读:
    软件工程
    python 浮点数四舍六入五成双
    python 比较内嵌字典的值
    python 之多继承顺序及supper()方法执行顺序
    python之装饰器进化
    Centos7 安装Postgres10
    hive常用操作
    MySQL中case when else end 用法
    python写入日志文件时日志内容重复写入
    python向config、ini文件读取写入
  • 原文地址:https://www.cnblogs.com/JackpotHan/p/9772705.html
Copyright © 2011-2022 走看看