import lombok 报错_lombok学习一:通过lombok解决idea mapper报错问题

首先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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值