最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于兩個(gè)BeanUtils.copyProperties()的用法及區(qū)別

 更新時(shí)間:2022年06月15日 09:54:53   作者:大鵬小站  
這篇文章主要介紹了關(guān)于兩個(gè)BeanUtils.copyProperties()的用法及區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

兩個(gè)BeanUtils.copyProperties()用法及區(qū)別

這兩個(gè)類在不同的包下面,而這兩個(gè)類的copyProperties()方法里面?zhèn)鬟f的參數(shù)賦值是相反的。

例如:

a,b為對(duì)象

BeanUtils.copyProperties(a, b);

public static void copyProperties(Object source, Object target) throws BeansException {//source 源文件,target 目標(biāo)文件
? ? ? ? copyProperties(source, target, (Class)null, (String[])null);
? ? }

BeanUtils是org.apache.commons.beanutils.BeanUtils,b拷貝到a

?public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {//destination,目標(biāo)文件,original原始的,源文件
? ? ? ? BeanUtilsBean.getInstance().copyProperties(dest, orig);
? ? }

這兩個(gè)不要搞混了! 

使用Beanutils.copyProperties()遇到的問題

BeanUtils.copyProperties VS PropertyUtils.copyProperties

兩者最大的區(qū)別是:

BeanUtils.copyProperties會(huì)進(jìn)行類型轉(zhuǎn)換,而PropertyUtils.copyProperties不會(huì)。

既然進(jìn)行了類型轉(zhuǎn)換,那BeanUtils.copyProperties的速度比不上PropertyUtils.copyProperties。

因此,PropertyUtils.copyProperties應(yīng)用的范圍稍為窄一點(diǎn),它只對(duì)名字和類型都一樣的屬性進(jìn)行copy,如果名字一樣但類型不一樣,它會(huì)報(bào)錯(cuò)

使用BeanUtils有幾個(gè)要注意的地方:

1.對(duì)于類型為Boolean/Short/Integer/Float/Double的屬性,它會(huì)轉(zhuǎn)換為0: 

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上有人解釋說是因?yàn)檫@幾個(gè)類型都有對(duì)應(yīng)的基本類型,在進(jìn)行類型轉(zhuǎn)換時(shí),有可能遇到類似Integer -> int的轉(zhuǎn)換,此時(shí)顯然不能對(duì)int類型的屬性賦值為null,因此統(tǒng)一轉(zhuǎn)換為0。

如何讓它不要轉(zhuǎn)為0呢?可以這樣:

import org.apache.commons.beanutils.converters.IntegerConverter; ?
IntegerConverter converter = new IntegerConverter(null); //默認(rèn)為null,而不是0?
BeanUtilsBean beanUtilsBean = new BeanUtilsBean();?
beanUtilsBean.getConvertUtils().register(converter, Integer.class);?

2.對(duì)于java.util.Date/BigDecimal/java.sql.Date/java.sql.Timestamp/java.sql.Time這幾個(gè)類,如果值為null,則在copy時(shí)會(huì)拋異常,需要使用對(duì)應(yīng)的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(); 
 
//如果沒有下面幾行,則在轉(zhuǎn)換null時(shí)會(huì)拋異常,例如:org.apache.commons.beanutils.ConversionException: No value specified for 'BigDecimal' 
//在org.apache.commons.beanutils.converters這個(gè)包下面有很多的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還會(huì)經(jīng)常碰到這樣變態(tài)的需求:

  • 假設(shè)是從A復(fù)制到B:
  • 需求1:如果B中某字段有值(不為null),則該字段不復(fù)制;也就是B中該字段沒值時(shí),才進(jìn)行復(fù)制,適合于對(duì)B進(jìn)行補(bǔ)充值的情況。
  • 需求2:如果A中某字段沒值(為null),則該字段不復(fù)制,也就是不要把null復(fù)制到B當(dāng)中。
  • 對(duì)于需求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); 
  } 
 }  
} 

對(duì)于需求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); 
 } 
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java多線程通信:交替打印ABAB實(shí)例

    Java多線程通信:交替打印ABAB實(shí)例

    這篇文章主要介紹了Java多線程通信:交替打印ABAB實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 最新評(píng)論

    涿鹿县| 江安县| 莱西市| 大同市| 平阳县| 报价| 长武县| 富顺县| 泗水县| 广元市| 永清县| 都匀市| 泽州县| 德清县| 龙山县| 昌都县| 石棉县| 亚东县| 和平区| 阿城市| 朝阳市| 华容县| 开阳县| 敦化市| 鲁山县| 合阳县| 扶风县| 台东市| 丁青县| 汝州市| 兰州市| 东辽县| 招远市| 资溪县| 桐庐县| 灵川县| 仁化县| 丰都县| 枣庄市| 吕梁市| 尉犁县|