转自:https://blog.csdn.net/qinxuefly/article/details/79159018

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

一、实战

1.安装lombok插件:

具体流程如图:

1.1

1.2

1.3

2.添加lombok的maven的pom.xml依赖:

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

3. 示例代码Student.java

  1. package com.lombok.demo;
  2. import lombok.EqualsAndHashCode;
  3. import lombok.Getter;
  4. import lombok.Setter;
  5. import lombok.ToString;
  6. /**
  7. * Created by zhangzh on 2017/2/8.
  8. */
  9. @Setter
  10. @Getter
  11. @ToString
  12. @EqualsAndHashCode
  13. public class Student {
  14. private String name;
  15. private int age;
  16. private String male;
  17. private String studentNo;
  18. }

4. 测试类LombokTest.java

  1. package com.lombok.demo;
  2. import lombok.extern.java.Log;
  3. /**
  4. * Created by zhangzh on 2017/2/8.
  5. */
  6. @Log
  7. public class LombokTest {
  8. public static void main(String[] args) {
  9. Student student = new Student();
  10. student.setAge(27);
  11. student.setMale("man");
  12. student.setName("lance");
  13. student.setStudentNo("2017");
  14. System.out.println(student.toString());
  15. Student student2 = new Student();
  16. student2.setAge(27);
  17. student2.setMale("man");
  18. student2.setName("lance");
  19. student2.setStudentNo("2017");
  20. System.out.println(student.equals(student2));
  21. student2.setStudentNo("2018");
  22. System.out.println(student.equals(student2));
  23. log.info("lombok test");
  24. }
  25. }

5. 输出结果:

  1. Student(name=lance, age=27, male=man, studentNo=2017)
  2. true
  3. false
  4. 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());

看,简单吧!

    1. <pre code_snippet_id="2447922" snippet_file_name="blog_20170615_5_1108377"></pre>
    2. <pre></pre>
    3. <pre></pre>
    4. <pre></pre>
    5. <pre></pre>
    6. <pre></pre>
    7. <pre></pre>
    8. <pre></pre>

34.Intellij IDEA 安装lombok及使用详解的更多相关文章

  1. Intellij IDEA 安装lombok及使用详解

    项目中经常使用bean,entity等类,绝大部分数据类类中都需要get.set.toString.equals和hashCode方法,虽然eclipse和idea开发环境下都有自动生成的快捷方式,但 ...

  2. Intellij IDEA中使用Debug调试详解

    转载:https://www.linuxidc.com/Linux/2017-09/146772.htm   Intellij IDEA中使用Debug调试详解 Debug用来追踪代码的运行流程,通常 ...

  3. Python安装、配置图文详解(转载)

    Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境(I ...

  4. Windows-008-VirtualBox 安装 Win7 前沿配置详解

    此文主要讲述在 Windows 系统下安装虚拟机 VirtualBox,及用 VirtualBox 安装 Win7 Professional 64bit 操作系统的前沿配置为例,配以图文进行详细的讲解 ...

  5. webpack安装配置使用教程详解

    webpack安装配置使用教程详解 www.111cn.net 更新:2015-09-01 编辑:swteen 来源:转载 本文章来为各位详细的介绍一下关于webpack安装配置使用教程吧,这篇文章对 ...

  6. 【和我一起学python吧】Python安装、配置图文详解

     Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境( ...

  7. Mac下Intellij IDea发布Java Web项目详解五 开始测试

    测试前准备工作目录 Mac下Intellij IDea发布Web项目详解一 Mac下Intellij IDea发布Java Web项目(适合第一次配置Tomcat的家伙们)详解二 Mac下Intell ...

  8. Linux centos7环境下安装JDK的步骤详解

    Linux centos7环境下安装JDK的步骤详解 测试root用户下JAVA版本 输入命令:   java –version 1.先到Oracle官网里下载好jdk,网址如下: http://ww ...

  9. Linux centos7环境下安装MySQL的步骤详解

    Linux centos7环境下安装MySQL的步骤详解 安装MySQL mysql 有两个跟windows不同的地方 1).my.ini 保存到/etc/my.ini 2).用户权限,单独用户执行 ...

随机推荐

  1. BZOJ 3209 数位DP

    思路: 先预处理出来组合数 按位做 枚举sum[x]是多少 注意Mod不是一个质数 //By SiriusRen #include <cstdio> using namespace std ...

  2. 浅谈贝塞尔曲线以及iOS中粘性动画的实现

    关于贝塞尔曲线,网上相关的文章很多,这里我主要想用更简单的方法让大家理解贝塞尔曲线,当然,这仅仅是我个人的理解,如有错误的地方还请大家能够帮忙指出来,这样大家才能一起进步. 贝塞尔曲线,常用到的可分为 ...

  3. Spring AOP 实现数据库读写分离

    背景 我们一般应用对数据库而言都是"读多写少",也就说对数据库读取数据的压力比较大,有一个思路就是说采用数据库集群的方案, 其中一个是主库,负责写入数据,我们称之为:写库: 其它都 ...

  4. python 中的property

    """ property() 的第一个参数是 getter 方法,第二个参数是 setter 方法 xx = property(a,b) @property #用于指示g ...

  5. mac terminal终端怎么退出python命令行

    >>>使用 quit(), exit(), 或者Command+d,或者Command+z退出命令行.

  6. [POI2011]MET-Meteors 整体二分_树状数组_卡常

    线段树肯定会 TLE 的,必须要用树状数组. Code: // luogu-judger-enable-o2 #include <cstdio> #include <algorith ...

  7. iOS开发——剪切板

    剪切板的调用是很简单的,常用的就这两个,不管文字或是图片的粘贴复制,对剪切板的操作就是基于下面两个属性的: [UIPasteboard generalPasteboard].string [UIPas ...

  8. 【Henu ACM Round#20 A】 Fancy Fence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 看看有没有(n-2)*180/n等于输入的a就好. [代码] #include <bits/stdc++.h> usin ...

  9. COGS——T 8. 备用交换机

    http://www.cogs.pro/cogs/problem/problem.php?pid=8 ★★   输入文件:gd.in   输出文件:gd.out   简单对比时间限制:1 s   内存 ...

  10. Liquibase被锁

    经常运行过程中出现 Liquibase - Waiting for changelog lock Waiting for changelog lock.... Running the migratio ...