一、使用lombok简化代码

lombok提供了很多注解,在编译时候生成java代码,代替了手工编写一些简单的代码,使程序员可以关注更重要的实现。

二、常用注解

以model为例

public class DataDemo {
private Integer id;
private String name;
private Date time;
}

一下是添加不同lombok注解的编译结果示例,编译结果很简单,不需要做什么说明,直接上代码:

@Getter / @Setter

public class GetterSetterDemo {
private Integer id;
private String name;
private Date time; public GetterSetterDemo() { } public Integer getId() { return this.id; } public String getName() { return this.name; } public Date getTime() { return this.time; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setTime(Date time) { this.time = time; }
}

@ToString

model上添加注解:@ToString(exclude = {"id"}, includeFieldNames = false)

public class ToStringDemo {
private Integer id;
private String name;
private Date time; public ToStringDemo() { } public String toString() {
return "ToStringDemo(" + this.name + ", " + this.time + ")";
}
}

@Data

@Data 注解相当于 Getter + Setter + ToString + @RequiredArgsConstrutor,可以用在pojo上

public class DataDemo {
private Integer id;
private String name;
private Date time; public DataDemo() { } public Integer getId() { return this.id;} public String getName() { return this.name; } public Date getTime() { return this.time; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setTime(Date time) { this.time = time; } public boolean equals(Object o) { ... } protected boolean canEqual(Object other) { ... } public int hashCode() { ... } public String toString() {
return "DataDemo(id=" + this.getId() + ", name=" + this.getName() + ", time=" + this.getTime() + ")";
}
}

@Builder

public class BuilderDemo {
private Integer id;
private String name;
private Date time; @ConstructorProperties({"id", "name", "time"})
BuilderDemo(Integer id, String name, Date time) {
this.id = id;
this.name = name;
this.time = time;
} public static BuilderDemo.BuilderDemoBuilder builder() {
return new BuilderDemo.BuilderDemoBuilder();
} public static class BuilderDemoBuilder {
private Integer id;
private String name;
private Date time; BuilderDemoBuilder() {
} public BuilderDemo.BuilderDemoBuilder id(Integer id) {
this.id = id;
return this;
} public BuilderDemo.BuilderDemoBuilder name(String name) {
this.name = name;
return this;
} public BuilderDemo.BuilderDemoBuilder time(Date time) {
this.time = time;
return this;
} public BuilderDemo build() {
return new BuilderDemo(this.id, this.name, this.time);
} public String toString() {
return "BuilderDemo.BuilderDemoBuilder(id=" + this.id + ", name=" + this.name + ", time=" + this.time + ")";
}
}
}

构造函数

@AllArgsConstructor    全部参数构造函数

@NoArgsConstructor   无参数构造函数

@RequiredArgsConstructor   NoNull参数和常量构造函数

/**@AllArgsConstructor*/
public class AllArgsConstructorDemo {
private Integer id;
private String name;
private Date time; @ConstructorProperties({"id", "name", "time"})
public AllArgsConstructorDemo(Integer id, String name, Date time) {
this.id = id;
this.name = name;
this.time = time;
}
} /**@NoArgsConstructor*/
public class NoArgsConstructorDemo {
private Integer id;
private String name;
private Date time; public NoArgsConstructorDemo() { }
} /**@RequiredArgsConstructor(staticName = "of")*/
public class RequiredArgsConstructorDemo {
private Integer id;
private String name;
private Date time; private RequiredArgsConstructorDemo() { } public static RequiredArgsConstructorDemo of() {
return new RequiredArgsConstructorDemo();
}
} /**@RequiredArgsConstructor(staticName = "of")*/
public class RequiredArgsConstructorDemo {
private Integer id;
@NonNull
private String name;
@NonNull
private Date time; @ConstructorProperties({"name", "time"})
private RequiredArgsConstructorDemo(@NonNull String name, @NonNull Date time) {
if(name == null) {
throw new NullPointerException("name");
} else if(time == null) {
throw new NullPointerException("time");
} else {
this.name = name;
this.time = time;
}
} public static RequiredArgsConstructorDemo of(@NonNull String name, @NonNull Date time) {
return new RequiredArgsConstructorDemo(name, time);
}
}

@Value

public final class ValueDemo {
private final Integer id;
private final String name;
private final Date time; @ConstructorProperties({"id", "name", "time"})
public ValueDemo(Integer id, String name, Date time) {
this.id = id;
this.name = name;
this.time = time;
} public Integer getId() {
return this.id;
} public String getName() {
return this.name;
} public Date getTime() {
return this.time;
} public boolean equals(Object o) { ... } public int hashCode() { ... } public String toString() {
return "ValueDemo(id=" + this.getId() + ", name=" + this.getName() + ", time=" + this.getTime() + ")";
}
}

lombok常见注解的更多相关文章

  1. 小记----------lombok插件idea的安装&常见注解解释及小案例

    Lombok安装插件 软件:idea 2018.3.6版本 1.打开settings

  2. eclipse 按装lombok与注解说明

    原文:http://www.cnblogs.com/ywqbj/p/5711691.html 一.安装lombok 1.下载   lombok-1.16.16.jar 包 我的下载完后放到:/root ...

  3. 来自lombok的注解(解决idea中的找不到get,set方法,找不到log的问题)

    今天看代码,发现idea报错,仔细一看调用的get,set方法bean中都没有,但是运行起来却没有问题,这个让我很疑惑.后来发现在类上有一个以前没见过的注解@Data,大概就是因为有他的原因.这个注解 ...

  4. lombok常用注解@Data@AllArgsConstructor@NoArgsConstructor@Builder@Accessors

    原贴:https://blog.csdn.net/ChenXvYuan_001/article/details/84961992 https://blog.csdn.net/weixin_382293 ...

  5. Lombok - 使用注解让你的JavaBean变得更加简洁

    Lombok - 工具简介: Lombok是一个编译时注释预处理器,有助于在编译时注入一些代码.Lombok提供了一组在开发时处理的注释,以将代码注入到Java应用程序中,注入的代码在开发环境中立即可 ...

  6. 20190905 Lombok常用注解

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

  7. Lombok 使用介绍(常见注解)

    目录 @Data @NonNull @Getter & @Setter @ToString @EqualsAndHashCode @NoArgsConstructor, @RequiredAr ...

  8. Eclipse - 安装lombok后注解无效

    安装 lombok lombok 的安装过程挺简单的,网上已经有很多相关的博客,这里就不在多说了,可以参考这篇:eclipse集成lombok注解不起作用 但是我按照网上的方式安装之后,注解一直不起作 ...

  9. lombok @EqualsAndHashCode 注解讲解

    官方文档:@EqualsAndHashCode 原文中提到的大致有以下几点: 1. 此注解会生成equals(Object other) 和 hashCode()方法. 2. 它默认使用非静态,非瞬态 ...

随机推荐

  1. 如何利用h5将视频设置为背景

    我们常常有着将视频作为网页背景的需要,但是在设置时也经常差强人意,今天设置了一下,可以基本达到要求了,可能有些小细节做的不是太好,希望指出来,一起进步 第一步:准备工作 工欲善其事必先利其器,我们首先 ...

  2. php学习四:数组(一)

    1.  直接赋值方式: ①   索引数组:以索引来存储数据,内存不是连续的,但是js中的内存是连续的 代码如下: $array[0] = "11"; $array[1] = &qu ...

  3. JAVA Comparator 接口排序用法

    java的比较器有两类,分别是Comparable接口和Comparator接口. 在为对象数组进行排序时,比较器的作用非常明显,首先来讲解Comparable接口. 让需要进行排序的对象实现Comp ...

  4. IT公司常见的内网漏洞表格

    访问控制类漏洞与隐患 这一类漏洞与隐患属于访问控制与身份鉴别问题,一般有没有配置访问控制.访问控制弱(弱口令或者空口令),身份鉴别可以绕过等问题 漏洞协议组件 漏洞类型 漏洞评级 SSH 弱口令 严重 ...

  5. dubbo有什么作用

    转自:http://blog.csdn.net/ichsonx/article/details/39008519 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的R ...

  6. Java中实现序列化的两种方式 Serializable 接口和 Externalizable接口

    对象的序列化就是将对象写入输出流中. 反序列化就是从输入流中将对象读取出来. 用来实现序列化的类都在java.io包中,我们常用的类或接口有: ObjectOutputStream:提供序列化对象并把 ...

  7. ZOJ 3209 Treasure Map(精确覆盖)

    Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 KB Your boss once had got many copies of ...

  8. CH5E02 花店橱窗【线性DP】

    5E02 花店橱窗 0x5E「动态规划」练习 背景 xq和他的老婆xz最近开了一家花店,他们准备把店里最好看的花都摆在橱窗里.但是他们有很多花瓶,每个花瓶都具有各自的特点,因此,当各个花瓶中放入不同的 ...

  9. 数据去重优化 MemoryError 内存不足

    from ProjectUtil.usingModuleTOMODIFY import getNow export_q_f, q_l, start_ = '/mnt/mongoexport/super ...

  10. Information:java: Errors occurred while compiling module 'spring'

    IntellJ Idea遇到Errors occurred while compiling module的解决方法 - sully2008的专栏 - CSDN博客 https://blog.csdn. ...