Python 官方文档:入门教程 => 点击学习
目录BeanUtils.copyProperties()拷贝id属性失败部分代码如下解决方法BeanUtils.copyProperties 出错BeanUtils.copyProp
po类中id有值,但是使用BeanUtils.copyProperties()拷贝出的vo类id属性为null,检查后发现是因为po继承的父类声明了一个泛型。
public abstract class AbstractEntity<ID extends Serializable> implements Serializable {
protected ID id;
protected ID createdBy;
protected Date createdTime;
protected ID lastModifiedBy;
protected Date lastModifiedTime;
public void setId(ID id) {
this.id = id;
}
public ID getId() {
return this.id;
}
查看BeanUtils.copyProperties()源码中有一段判断如下:
if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType()))
当执行到获取vo类的writeMethod即setId()参数类型,结果是Long类型,而po类的readMethod即getId()返回值类型获取到的结果却是Serializable所以BeanUtils认为属性类型不同,所以不会拷贝id属性。
暂不清楚po类extends AbstractEntity<Long>后为什么读取到的类型不是Long而是父类型Serializable,暂时先不用泛型,把id类型直接定义为Long,问题解决~
注意:属性复制,不同jar中的方法,用法不一样!
spring 包(org.springframework.beans)中
BeanUtils.copyProperties(A,B);
是A中的值赋值给B
Apache 包(org.apache.commons.beanutils)中(常用)
BeanUtils.copyProperties(A,B);
是B中的值赋值给A
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: BeanUtils.copyProperties()拷贝id属性失败的原因及解决
本文链接: https://www.lsjlt.com/news/134201.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0