Lombok
在类上使用注解 @Data
,会在编译时自动添加 Setter-Getter 方法。
有一个需要避免踩坑的地方:属性名前两个字母都需要小写,前两个字母如果是小写大写会出现问题!
解决:
- 修改属性名字,让第二个字母小写,或者说是规定所有的属性的前两个字母必须小写
- 如果数据库已经设计好,并且前后端接口对接好了,不想修改,那就专门为这种特殊的属性使用idea生成get-set方法复制代码
现象:
1 | @Data |
当我们使用Mybatis插入数据的时候,发现,其他属性都能正常的插入,但是就是nMetaType属性在数据库一直是null.
原因:
Lombok对于第一个字母小写,第二个字母大写的属性生成的get-set方法和Mybatis以及idea或者说是Java官方认可的get-set方法生成的不一样:
1 | @Data |
idea,Mybatis,Java官方默认的行为为:
1 | public class NMetaVerify { |
Mybatis(3.4.6版本)解析get-set方法获取属性名字的源码:
1 | package org.apache.ibatis.reflection.property; |
Mybatis解析get-set方法为属性名字测试:
1 | @Test |