lombok使用及常用注解
简介
大部分项目中都必不可少的包含数据库实体(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
lombok使用及常用注解的更多相关文章
- Eclipse安装lombok及常用注解
转自:https://blog.csdn.net/ZJDWHD/article/details/77795023 lombok的官方网址:http://projectlombok.org/ https ...
- eclipse安装lombok和常用注解使用
1.下载lombok.jar lombok 的官方网址:http://projectlombok.org/ 2.运行lombok.jar: java -jar D:\eclipse-luna\l ...
- 20190905 Lombok常用注解
Lombok常用注解 val 用于声明类型,将从初始化表达式推断出类型,仅适用于局部变量和foreach循环,而不适用于字段.声明的局部变量为final变量. Java自带类型推断随着JDK版本提升越 ...
- Lombok 常用注解
Lombok Lombok 能以简单的注解形式来简化 java 代码,提高开发人员的开发效率.例如开发中经常需要写的 javaBean,都需要花时间去添加相应的 getter/setter,也许还要去 ...
- lombok 简化java代码注解
lombok 简化java代码注解 安装lombok插件 以intellij ide为例 File-->Setting-->Plugins-->搜索"lombok plug ...
- Spring Boot常用注解
SpringBoot注解大全 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@Enable ...
- lombok 中的@Data注解
今天看到有代码中的Dao包中的类文件,写的极其简洁,甚至引起了开发工具InteliJ的报错,然后程序还能稳健地跑起来. import lombok.Data; @Data public class V ...
- Spring系列之Spring常用注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- SpringMVC常用注解實例詳解3:@ResponseBody
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
随机推荐
- sql 循环转移备份数据
--创建表结构 select top 1 * into ATable_20190710 from ATable --转移表数据 insert into ATable_20190710 select t ...
- centos7 追加python3 + 使用pip + virtualenv
一.安装Python3的方法: 首先安装依赖包: yum -y groupinstall "Development tools" yum -y install zlib-devel ...
- Java 概述和编程基础
First of all,Java概述: 类是Java程序设计的基石和基本单元: main()方法是程序的入口,它是共有的.静态的,参数String[] args表示一个字符串数组可以传入该程序,用来 ...
- L1-025. 正整数A+B 简单复习一下,。
本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B,其间以空格分开.问题是A和B不一定是满 ...
- C#求1-100的质数,100-1000的水仙花数,1-100所有的平方和平方平方根
//你们的鼓励是我最大的动力 大家可以多留言评论 在接下来很长一段时间我会从初级到高级每天更新 大家有想学习的内容也可以留言哦 //现在是我做C#老师的第28天,希望和大家一起努力 加油 using ...
- Left4Dead2 LAN Online
Left4Dead2 LAN Online Franklin vs Wolverine 求生之路 局域网联机说明 ============================ 局域网联机方法: 1.先找到 ...
- ubuntu 网卡名称重命名
ubuntu 网卡名称重命名 参考:https://blog.csdn.net/hzj_001/article/details/81587824 biosdevname 和 net.ifnames 两 ...
- Ubuntu14.04更新硬件实现堆栈(HWE)
Ubuntu14.04更新硬件实现堆栈(HWE) 来源: https://github.com/gatieme/AderXCoding/tree/master/system/tools/ubuntu_ ...
- 【其他】BootCDN
BootCDN 稳定.快速.免费的前端开源项目 CDN 加速服务 是 Bootstrap 中文网支持并维护的前端开源项目免费 CDN 服务,致力于为 Bootstrap.jQuery.Angular. ...
- loj 3014「JOI 2019 Final」独特的城市
loj 我本来是直接口胡了一个意思一样的做法的,但是因为觉得有点假+实现要用并查集(?)就卡了好一会儿... 对于一个点\(x\)来说,独特的点一定在它的最长链上,如果有独特的点不在最长链上,那么最长 ...