在Intellij IDEA中使用lombok插件进行综合项目开发

栏目: IOS · 发布时间: 7年前

内容简介:为什么要使用lombok?

为什么要使用lombok?

应用场景 :在JavaEE项目中经常使用bean,entity等类,绝大部分数据类中都需要生成get /set 方法、重载toString、 equals和hashCode方法。

使用优点

1.虽然在IDEA开发环境下都有自动生成这些代码的快捷方式,但自动生成这些代码后,如果bean中的属性一旦有修改、删除或增加时,需要重新生成或删除get /set等方法,给代码维护增加负担。

2.即使需要在这些方法中需要做额外的操作,只需要在对应类中手动重写即可。

未使用lombok之前,我们如何做的?

在类中alt+enter,然后选择需要的方法。

如下:

public class Student {

    private String name;

    private String studentId;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", studentId='" + studentId + '\'' +
                '}';
    }
}

如何使用lombok?

一 在idea中安装lombok插件

1.依次点击Preferences - Plugins,搜索lombok,最后点击Lombok Plugin (对,就下载最多的那个)intall。

2.重启idea即可使用

在Intellij IDEA中使用lombok插件进行综合项目开发

二 在项目对于的xml文件中添加maven依赖


 
   
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>

三 示例代码及说明

/**
* 如果没有添加@Setter注解,则无法使用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()); */
@Setter
@Getter
@ToString 
@EqualsAndHashCode //@Data
public class Student { 
      private String name; 
      private int age;
      private String male; 
      private String studentNo;
}

四 测试程序

@Log
public class LombockTest {
public static void main(String[] args) {

      //创建第一个学生对象
      Student student = new Student(); 
      student.setAge(19);          
      student.setMale("male"); 
      student.setName("Spring Boot"); 
      student.setStudentNo("2017");
      System.out.println(student.toString());

      //创建第二个学生对象
      Student student2 = new Student();
      student2.setAge(19);
      student2.setMale("male"); 
      student2.setName("Spring Boot");                       
      student2.setStudentNo("2017");

      //判断两个对象的引用是否相等 
      System.out.println(student.equals(student2));
      student2.setStudentNo("2018");
      System.out.println(student.equals(student2));
      log.info("lombok test"); }
}

五 运行结果

Student(name=SPring Boot,age=19,male=male,studentNo=2017)
true
false

ps:在使用了lombok之后,又重写了该方法,会覆盖此方法,代码就不贴了,亲测有效。

作者:tigerAndBull

链接:https://www.jianshu.com/p/39de2a8e4ae5


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Security, Privacy and Commerce, 2nd Edition

Web Security, Privacy and Commerce, 2nd Edition

Simson Garfinkel / O'Reilly Media / 2002-01-15 / USD 44.95

Since the first edition of this classic reference was published, World Wide Web use has exploded and e-commerce has become a daily part of business and personal life. As Web use has grown, so have ......一起来看看 《Web Security, Privacy and Commerce, 2nd Edition》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具