Beanutils.copyProperties( )使用遇到的问题

Beanutils.copyProperties( )使用遇到的问题

BeanUtils.copyProperties VS PropertyUtils.copyProperties

两者最大的区别是:
BeanUtils.copyProperties会进行类型转换,而PropertyUtils.copyProperties不会。
既然进行了类型转换,那BeanUtils.copyProperties的速度比不上PropertyUtils.copyProperties。
因此,PropertyUtils.copyProperties应用的范围稍为窄一点,它只对名字和类型都一样的属性进行copy,如果名字一样但类型不一样,它会报错

使用BeanUtils有几个要注意的地方:
1.对于类型为Boolean/Short/Integer/Float/Double的属性,它会转换为0:


```java
public class User { 
 
 private Integer intVal; 
  
 private Double doubleVal; 
  
 private Short shortVal; 
  
 private Long longVal; 
  
 private Float floatVal; 
  
 private Byte byteVal; 
  
 private Boolean booleanVal; 
} 
 
User src = new User(); 
User dest = new User(); 
BeanUtils.copyProperties(dest, src); 
System.out.println(src); 
System.out.println(dest); 
 
//输出  
User [intVal=null, doubleVal=null, shortVal=null, longVal=null, floatVal=null, byteVal=null, booleanVal=null] 
User [intVal=0, doubleVal=0.0, shortVal=0, longVal=0, floatVal=0.0, byteVal=0, booleanVal=false] 

在stackoverflow上有人解释说是因为这几个类型都有对应的基本类型,在进行类型转换时,有可能遇到类似Integer -> int的转换,此时显然不能对int类型的属性赋值为null,因此统一转换为0。

如何让它不要转为0呢?可以这样:

import org.apache.commons.beanutils.converters.IntegerConverter; 
 
IntegerConverter converter = new IntegerConverter(null); //默认为null,而不是0 
BeanUtilsBean beanUtilsBean = new BeanUtilsBean(); 
beanUtilsBean.getConvertUtils().register(converter, Integer.class); 

2.对于java.util.Date/BigDecimal/java.sql.Date/java.sql.Timestamp/java.sql.Time这几个类,如果值为null,则在copy时会抛异常,需要使用对应的Conveter:

public class User2 { 
 
 private java.util.Date javaUtilDateVal; 
  
 private java.sql.Date javaSqlDateVal; 
  
 private java.sql.Timestamp javaSqlTimeStampVal; 
  
 private BigDecimal bigDecimalVal; 
 
 private java.sql.Time javaSqlTime; 
 
} 
 
User2 src = new User2(); 
User2 dest = new User2(); 
 
BeanUtilsBean beanUtilsBean = new BeanUtilsBean(); 
 
//如果没有下面几行,则在转换null时会抛异常,例如:org.apache.commons.beanutils.ConversionException: No value specified for 'BigDecimal' 
//在org.apache.commons.beanutils.converters这个包下面有很多的Converter,可以按需要使用 
beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.BigDecimalConverter(null), BigDecimal.class); 
beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.DateConverter(null), java.util.Date.class); 
 
beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.SqlTimestampConverter(null), java.sql.Timestamp.class); 
beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.sql.Date.class); 
beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.SqlTimeConverter(null), java.sql.Time.class); 
 
beanUtilsBean.copyProperties(dest, src); 
System.out.println(src); 
System.out.println(dest); 

使用BeanUtils还会经常碰到这样变态的需求:
假设是从A复制到B:
需求1:如果B中某字段有值(不为null),则该字段不复制;也就是B中该字段没值时,才进行复制,适合于对B进行补充值的情况。
需求2:如果A中某字段没值(为null),则该字段不复制,也就是不要把null复制到B当中。

对于需求1,可以这样:

import org.apache.commons.beanutils.BeanUtilsBean; 
import org.apache.commons.beanutils.PropertyUtils; 
 
public class CopyWhenNullBeanUtilsBean extends BeanUtilsBean{ 
 
 @Override 
 public void copyProperty(Object bean, String name, Object value) 
   throws IllegalAccessException, InvocationTargetException { 
  try { 
   Object destValue = PropertyUtils.getSimpleProperty(bean, name); 
   if (destValue == null) { 
    super.copyProperty(bean, name, value); 
   } 
  } catch (NoSuchMethodException e) { 
   throw new RuntimeException(e); 
  } 
 } 
 
} 

对于需求2,可以这样:

import org.apache.commons.beanutils.BeanUtilsBean; 
 
public class CopyFromNotNullBeanUtilsBean extends BeanUtilsBean { 
 
 @Override 
 public void copyProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException { 
  if (value == null) { 
   return; 
  } 
  super.copyProperty(bean, name, value); 
 } 
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

元气满满@WJW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值