首先lombok是一款大幅度减少代码的插件
学习文档地址如下
https://www.projectlombok.org/features
val
val example =newArrayList();
example.add("Hello, World!");
val foo = example.get(0);
与java如下代码相等
finalArrayList example =newArrayList();
example.add("Hello, World!");
finalString foo = example.get(0);
var
var example = newArrayList();
example.add("Hello, World!");
val foo = example.get(0);
与java如下代码相等
ArrayList example = newArrayList();
example.add("Hello, World!");
String foo = example.get(0);
@NotNull用来判断参数不为空并且抛出异常
importlombok.NonNull;
public classNonNullExampleextendsSomething{
privateString name;
publicNonNullExample(@NonNullPerson person) {
super("Hello");
this.name = person.getName();
}
}
与如下java代码相等
importlombok.NonNull;
public classNonNullExampleextendsSomething{
privateString name;
publicNonNullExample(@NonNullPerson person) {
super("Hello");
if(person ==null) {
throw newNullPointerException("person is marked @NonNull but is null");
}
this.name = person.getName();
}
}
@CleanUp用于关闭各种资源
importlombok.Cleanup;
importjava.io.*;
public classCleanupExample{
public staticvoidmain(String[]args)throwsIOException{
@CleanupInputStream in =newFileInputStream(args[0]);
@CleanupOutputStream out =newFileOutputStream(args[1]);
byte[]b =newbyte[10000];
while(true) {
intr = in.read(b);
if(r == -1)break;
out.write(b,0, r);
}
}
}
与下面java代码相等
importjava.io.*;
public classCleanupExample{
public staticvoidmain(String[]args)throwsIOException{
InputStream in =newFileInputStream(args[0]);
try{
OutputStream out =newFileOutputStream(args[1]);
try{
byte[]b =newbyte[10000];
while(true) {
intr = in.read(b);
if(r == -1)break;
out.write(b,0, r);
}
}finally{
if(out !=null) {
out.close();
}
}
}finally{
if(in !=null) {
in.close();
}
}
}
}
@EqualsAndHashCode重写相等和hash方法。
importlombok.EqualsAndHashCode;
@EqualsAndHashCode
public classEqualsAndHashCodeExample{
private transientinttransientVar =10;
privateString name;
privatedoublescore;
@EqualsAndHashCode.ExcludeprivateShape shape =newSquare(5,10);
privateString[]tags;
@EqualsAndHashCode.Excludeprivateintid;
publicString getName() {
return this.name;
}
@EqualsAndHashCode(callSuper=true)
public static classSquareextendsShape{
private finalintwidth, height;
publicSquare(intwidth,intheight) {
this.width = width;
this.height = height;
}
}
}
与java代码相等
importjava.util.Arrays;
public classEqualsAndHashCodeExample{
private transientinttransientVar =10;
privateString name;
privatedoublescore;
privateShape shape =newSquare(5,10);
privateString[]tags;
privateintid;
publicString getName() {
return this.name;
}
@Overridepublicbooleanequals(Object o) {
if(o ==this)return true;
if(!(oinstanceofEqualsAndHashCodeExample))return false;
EqualsAndHashCodeExample other =(EqualsAndHashCodeExample)o;
if(!other.canEqual((Object)this))return false;
if(this.getName()==null? other.getName()!=null: !this.getName().equals(other.getName()))return false;
if(Double.compare(this.score, other.score)!=0)return false;
if(!Arrays.deepEquals(this.tags, other.tags))return false;
return true;
}
@OverridepublicinthashCode() {
finalintPRIME =59;
intresult =1;
finallongtemp1 = Double.doubleToLongBits(this.score);
result =(result*PRIME)+(this.name ==null?43:this.name.hashCode());
result =(result*PRIME)+(int)(temp1 ^(temp1 >>>32));
result =(result*PRIME)+ Arrays.deepHashCode(this.tags);
returnresult;
}
protectedbooleancanEqual(Object other) {
returnotherinstanceofEqualsAndHashCodeExample;
}
public static classSquareextendsShape{
private finalintwidth, height;
publicSquare(intwidth,intheight) {
this.width = width;
this.height = height;
}
@Overridepublicbooleanequals(Object o) {
if(o ==this)return true;
if(!(oinstanceofSquare))return false;
Square other =(Square)o;
if(!other.canEqual((Object)this))return false;
if(!super.equals(o))return false;
if(this.width != other.width)return false;
if(this.height != other.height)return false;
return true;
}
@OverridepublicinthashCode() {
finalintPRIME =59;
intresult =1;
result =(result*PRIME)+super.hashCode();
result =(result*PRIME)+this.width;
result =(result*PRIME)+this.height;
returnresult;
}
protectedbooleancanEqual(Object other) {
returnotherinstanceofSquare;
}
}
}
@Builder利用builder模式创建对象
MallPay mallPay = MallPay.builder()
.payAmount(100l)
.orderNumber("123456")
.issuccess((byte)1)
.createTime(new Date())
.updateTime(new Date()).build();
mallPayMapper.insertSelective(mallPay);
return mallPay;