博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lombok常见注解
阅读量:4322 次
发布时间:2019-06-06

本文共 5460 字,大约阅读时间需要 18 分钟。

一、使用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; }}
View Code

@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 + ")";    }}
View Code

@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() + ")";    }}
View Code

@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 + ")"; } }}
View Code

@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); }}
View Code

@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() + ")"; }}
View Code

 

转载于:https://www.cnblogs.com/mr-yang-localhost/p/9330349.html

你可能感兴趣的文章
GIS当代技术群2084282(opening)
查看>>
arcengine 经典代码(转) 空间查询 在一个图层上画一个polygon,根据该polygon查询出图层上与之相交的polygon并高亮显示出来...
查看>>
BurpSuite中的安全测试插件推荐
查看>>
用存储过程实现获取字符串中的数据添加到列中
查看>>
GZIP压缩传输服务器配置
查看>>
Velocity模版进行shiro验证
查看>>
新生舞会
查看>>
双倍回文(bzoj 2342)
查看>>
微软Coco Blockchain Framework:一键解决企业级区块链三大难题
查看>>
Azure 虚拟机诊断设置问题排查
查看>>
C++入门经典-例4.8-同名的全局变量和局部变量
查看>>
文章阅读报告 -- 自媒体时代的电子阅读
查看>>
python并行编程学习之并行计算存储体系结构
查看>>
Asp.net常用的51个代码(非常实用)
查看>>
深度学习中一些常用函数的偏导数
查看>>
解决离线Could not parse configuration:hibernate.cfg.xml错误
查看>>
关于Win7 x64下过TP保护(应用层)(转)
查看>>
6月7号
查看>>
JS五星级评分效果(类似与淘宝打分效果)
查看>>
JQuery的源码阅读
查看>>