Setter-Getter方法的坑
问题发现
我们在项目当中主要使用Lombok的Setter-Getter方法的注解,也就是组合注解@Data,但是在一次使用Mybatis插入数据的过程当中,出现了一个问题,问题描述如下:
我们有个实体类:
1 2 3 4 5 6
| @Data public class NMetaVerify{ private NMetaType nMetaType; private Long id; ....其他属性 }
|
当我们使用Mybatis插入数据的时候,发现,其他属性都能正常的插入,但是就是nMetaType属性在数据库一直是null。
解决
当我debug项目代码到调用Mybatis的插入SQL对应的方法的时候,我看到NMetaVerify对象的nMetaType属性还是有数据的,但是执行插入之后,数据库的nMetaType字段就是一直是null,原先我以为是我的枚举类型写法不正确,看了下别的同样具有枚举类型的字段,也是正常能插入到数据库当中的,这更让我感觉到疑惑了。
于是,我就跟踪Mybatis的源码,发现Mybatis在获取这个nMetaType属性的时候使用了反射,使用的是getxxxx方法来获取的,但是我发现nMetaType的get方法好像有点和Mybatis需要的getxxxx方法长的好像不一样,问题找到了!
原因
Lombok对于第一个字母小写,第二个字母大写的属性生成的get-set方法和Mybatis以及idea或者说是Java官方认可的get-set方法生成的不一样:
Lombok生成的Get-Set方法
1 2 3 4 5 6 7 8 9 10 11 12
| @Data public class NMetaVerify { private Long id; private NMetaType nMetaType; private Date createTime; public void lombokFound(){ NMetaVerify nMetaVerify = new NMetaVerify(); nMetaVerify.setNMetaType(NMetaType.TWO); nMetaVerify.getNMetaType(); } }
|
idea,Mybatis,Java官方默认的行为为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| 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() { return nMetaType; }
public void setnMetaType(NMetaType nMetaType) { this.nMetaType = nMetaType; }
public Date getCreateTime() { return createTime; }
public void setCreateTime(Date createTime) { this.createTime = createTime; } }
|
Mybatis(3.4.6版本)解析get-set方法获取属性名字的源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package org.apache.ibatis.reflection.property;
import java.util.Locale;
import org.apache.ibatis.reflection.ReflectionException;
public final class PropertyNamer {
private PropertyNamer() { }
public static String methodToProperty(String name) { if (name.startsWith("is")) { name = name.substring(2); } else if (name.startsWith("get") || name.startsWith("set")) { name = name.substring(3); } else { throw new ReflectionException("Error parsing property name '" + name + "'. Didn't start with 'is', 'get' or 'set'."); } if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) { name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1); }
return name; }
public static boolean isProperty(String name) { return name.startsWith("get") || name.startsWith("set") || name.startsWith("is"); }
public static boolean isGetter(String name) { return name.startsWith("get") || name.startsWith("is"); }
public static boolean isSetter(String name) { return name.startsWith("set"); }
}
|
Mybatis解析get-set方法为属性名字测试
1 2 3 4 5 6 7 8 9 10
| @Test public void foundPropertyNamer() { String isName = "isName"; String getName = "getName"; String getnMetaType = "getnMetaType"; String getNMetaType = "getNMetaType";
Stream.of(isName,getName,getnMetaType,getNMetaType) .forEach(methodName->System.out.println("方法名字是:"+methodName+" 属性名字:"+ PropertyNamer.methodToProperty(methodName))); }
|
输出结果如下:
1 2 3 4
| 方法名字是:isName 属性名字:name 方法名字是:getName 属性名字:name 方法名字是:getnMetaType 属性名字:nMetaType 方法名字是:getNMetaType 属性名字:NMetaType
|
解决方案
修改属性名字,让第二个字母小写,或者说是规定所有的属性的前两个字母必须小写
如果数据库已经设计好,并且前后端接口对接好了,不想修改,那就专门为这种特殊的属性使用idea生成get-set方法复制代码
@Accessor(chain = true)注解的问题
问题发现
在使用 easyexcel 导出的时候,发现以前的实体类导出都很正常,但是现在新加的实体类不正常了,比对了发现,新加的实体类增加了@Accessor(chain = true)注解,我们的目的主要是方便我们链式调用set方法:
1 2 3 4 5
| new UserDto() .setUserName("") .setAge(10) ........ .setBirthday(new Date());
|
原因
easyexcel底层使用的是cglib来做反射工具包的:
1 2 3 4 5 6 7 8 9 10 11
| com.alibaba.excel.read.listener.ModelBuildEventListener 类的第130行 BeanMap.create(resultModel).putAll(map);
最底层的是cglib的BeanMap的这个方法调用
abstract public Object put(Object bean, Object key, Object value);com.alibaba.excel.read.listener.ModelBuildEventListener 类的第130行 BeanMap.create(resultModel).putAll(map);
最底层的是cglib的BeanMap的这个方法调用
abstract public Object put(Object bean, Object key, Object value);
|
但是cglib使用的是Java的rt.jar里面的一个Introspector这个类的方法:
Introspector.java 第520行
1 2 3 4 5 6 7 8 9 10
| if (int.class.equals(argTypes[0]) && name.startsWith(GET_PREFIX)) { pd = new IndexedPropertyDescriptor(this.beanClass, name.substring(3), null, null, method, null); } else if (void.class.equals(resultType) && name.startsWith(SET_PREFIX)) { pd = new PropertyDescriptor(this.beanClass, name.substring(3), null, method); if (throwsException(method, PropertyVetoException.class)) { pd.setConstrained(true); } }
|
解决方案
去掉Accessor注解。
要么就等待easyexcel的作者替换掉底层的cglib或者是其他,反正是支持获取返回值不是void的setxxx方法就行复制代码。