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);
}
}