Lombok踩坑

Lombok对于第一个字母小写,第二个字母大写的属性生成的get-set方法和Mybatis以及idea或者说是Java官方认可的get-set方法生成的不一样:

@Data
public class NMetaVerify{
    private NMetaType nMetaType;
    private Long id;
}

Lombok生成的Get-Set方法为

NMetaVerify nMetaVerify = new NMetaVerify();
//注意:nMetaType的set方法为setNMetaType,第一个n字母大写了
nMetaVerify.setNMetaType(NMetaType.TWO); 
//getxxxx方法也是大写
nMetaVerify.getNMetaType();

二Mybatis、Idea、Java官方标准的生成规则为

public class NMetaVerify {
    private Long id;
    private NMetaType nMetaType;
    private Date createTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public NMetaType getnMetaType() {//注意:nMetaType属性的第一个字母小写
        return nMetaType;
    }

    public void setnMetaType(NMetaType nMetaType) {//注意:nMetaType属性的第一个字母小写
        this.nMetaType = nMetaType;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

将会导致MyBatis插入的时候无法读取到值
解决
1.使用 @JsonProperty(“nMetaType”) 修饰字段
2.手动生成Get-Set方法
3.修改属性名

@Accessor(chain = true)注解的问题

new UserDto()
.setUserName("")
.setAge(10)
.setBirthday(new Date());

easyexcel底层使用的是cglib来做反射工具包的:但是cglib使用的是Java的rt.jar里面的一个Introspector这个类的方法:只支持获取返回值不是void的setxxx方法

# Introspector.java 第520行
if (int.class.equals(argTypes[0]) && name.startsWith(GET_PREFIX)) {
   pd = new IndexedPropertyDescriptor(this.beanClass, name.substring(3), null, null, method, null);
   //下面这行判断,只获取返回值是void类型的setxxxx方法
 } else if (void.class.equals(resultType) && name.startsWith(SET_PREFIX)) {
    // Simple setter
    pd = new PropertyDescriptor(this.beanClass, name.substring(3), null, method);
    if (throwsException(method, PropertyVetoException.class)) {
       pd.setConstrained(true);
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值