本篇内容主要讲解“如何解决使用Lombok造成的事故”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何解决使用Lombok造成的事故”吧!
我们在项目当中主要使用Lombok的Setter-Getter方法的注解,也就是组合注解@Data,但是在一次使用Mybatis插入数据的过程当中,出现了一个问题,问题描述如下:
我们有个实体类:
@Datapublic 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方法
@Datapublic class NMetaVerify { private Long id; private NMetaType nMetaType; private Date createTime; public void lombokFound(){ NMetaVerify nMetaVerify = new NMetaVerify(); nMetaVerify.setNMetaType(NMetaType.TWO); //注意:nMetaType的set方法为setNMetaType,第一个n字母大写了, nMetaVerify.getNMetaType(); //getxxxx方法也是大写 }}
idea,Mybatis,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; }}
package org.apache.ibatis.reflection.property;import java.util.Locale;import org.apache.ibatis.reflection.ReflectionException;/** * @author Clinton Begin */public final class PropertyNamer { private PropertyNamer() { // Prevent Instantiation of Static Class } public static String methodToProperty(String name) { if (name.startsWith("is")) {//is开头的一般是bool类型,直接从第二个(索引)开始截取(简单粗暴) name = name.substring(2); } else if (name.startsWith("get") || name.startsWith("set")) {//set-get的就从第三个(索引)开始截取 name = name.substring(3); } else { throw new ReflectionException("Error parsing property name '" + name + "'. Didn't start with 'is', 'get' or 'set'."); } //下面这个判断很重要,可以分成两句话开始解释,解释如下 //第一句话:name.length()==1 // 对于属性只有一个字母的,例如private int x; // 对应的get-set方法是getX();setX(int x); //第二句话:name.length() > 1 && !Character.isUpperCase(name.charAt(1))) // 属性名字长度大于1,并且第二个(代码中的charAt(1),这个1是数组下标)字母是小写的 // 如果第二个char是大写的,那就直接返回name 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"); }}
@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))); }
输出结果如下:
方法名字是:isName 属性名字:name 方法名字是:getName 属性名字:name 方法名字是:getnMetaType 属性名字:nMetaType //这个以及下面的属性第二个字母都是大写,所以直接返回name 方法名字是:getNMetaType 属性名字:NMetaType
在使用 easyexcel 导出的时候,发现以前的实体类导出都很正常,但是现在新加的实体类不正常了,比对了发现,新加的实体类增加了@Accessor(chain = true)
注解,我们的目的主要是方便我们链式调用set方法:
new UserDto().setUserName("").setAge(10).........setBirthday(new Date());
easyexcel底层使用的是cglib来做反射工具包的:
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行
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); }}
到此,相信大家对“如何解决使用Lombok造成的事故”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/itsoku/blog/4779698