我们有个实体类:
@Data
publicclassNMetaVerify{
private NMetaType nMetaType;
private Long id;
....其他属性
}
#Lombok生成的Get-Set方法
@Data
publicclassNMetaVerify{
private Long id;
private NMetaType nMetaType;
private Date createTime;
publicvoidlombokFound(){
NMetaVerify nMetaVerify = new NMetaVerify();
nMetaVerify.setNMetaType(NMetaType.TWO); //注意:nMetaType的set方法为setNMetaType,第一个n字母大写了,
nMetaVerify.getNMetaType(); //getxxxx方法也是大写
}
}
#idea,Mybatis,Java官方默认的行为为:
publicclassNMetaVerify{
private Long id;
private NMetaType nMetaType;
private Date createTime;
public Long getId(){
return id;
}
publicvoidsetId(Long id){
this.id = id;
}
public NMetaType getnMetaType(){//注意:nMetaType属性的第一个字母小写
return nMetaType;
}
publicvoidsetnMetaType(NMetaType nMetaType){//注意:nMetaType属性的第一个字母小写
this.nMetaType = nMetaType;
}
public Date getCreateTime(){
return createTime;
}
publicvoidsetCreateTime(Date createTime){
this.createTime = createTime;
}
}
package org.apache.ibatis.reflection.property;
import java.util.Locale;
import org.apache.ibatis.reflection.ReflectionException;
/**
* @author Clinton Begin
*/
publicfinalclassPropertyNamer{
privatePropertyNamer(){
// Prevent Instantiation of Static Class
}
publicstatic String methodToProperty(String name){
if (name.startsWith("is")) {//is开头的一般是bool类型,直接从第二个(索引)开始截取(简单粗暴)
name = name.substring(2);
} elseif (name.startsWith("get") || name.startsWith("set")) {//set-get的就从第三个(索引)开始截取
name = name.substring(3);
} else {
thrownew 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;
}
publicstaticbooleanisProperty(String name){
return name.startsWith("get") || name.startsWith("set") || name.startsWith("is");
}
publicstaticbooleanisGetter(String name){
return name.startsWith("get") || name.startsWith("is");
}
publicstaticbooleanisSetter(String name){
return name.startsWith("set");
}
}
@Test
publicvoidfoundPropertyNamer(){
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
1.修改属性名字,让第二个字母小写,或者说是规定所有的属性的前两个字母必须小写
2.如果数据库已经设计好,并且前后端接口对接好了,不想修改,那就专门为这种特殊的属性使用idea生成get-set方法
1.去掉Accessor注解
2.要么就等待easyexcel的作者替换掉底层的cglib或者是其他,反正是支持获取返回值不是void的setxxx方法就行
new UserDto()
.setUserName("")
.setAge(10)
........
.setBirthday(new Date());
com.alibaba.excel.read.listener.ModelBuildEventListener 类的第130行
BeanMap.create(resultModel).putAll(map);
最底层的是cglib的BeanMap的这个方法调用
abstractpublic Object put(Object bean, Object key, Object value);
# 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方法
} elseif (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);
}
}
来源:juejin.im/post/6881432532332576781
往期推荐
一款牛逼的IDEA插件神器:让代码命名变得轻松高效
MySQL模糊查询再也用不着like+%了!
3 个奇淫巧技,分库分表 LIMIT 翻页性能直接拉满!
别再写冗余缓存代码了!Easy-Cache 给你统一解决方案,爽到飞起!
千万级大表如何删除数据?
开源项目|用Java开发一款AI系统,支持文案/PPT/图片/视频生成

