關(guān)于兩個(gè)BeanUtils.copyProperties()的用法及區(qū)別
兩個(gè)BeanUtils.copyProperties()用法及區(qū)別
這兩個(gè)類在不同的包下面,而這兩個(gè)類的copyProperties()方法里面?zhèn)鬟f的參數(shù)賦值是相反的。
例如:
a,b為對象
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會進(jìn)行類型轉(zhuǎn)換,而PropertyUtils.copyProperties不會。
既然進(jìn)行了類型轉(zhuǎn)換,那BeanUtils.copyProperties的速度比不上PropertyUtils.copyProperties。
因此,PropertyUtils.copyProperties應(yīng)用的范圍稍為窄一點(diǎn),它只對名字和類型都一樣的屬性進(jìn)行copy,如果名字一樣但類型不一樣,它會報(bào)錯
使用BeanUtils有幾個(gè)要注意的地方:
1.對于類型為Boolean/Short/Integer/Float/Double的屬性,它會轉(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è)類型都有對應(yīng)的基本類型,在進(jìn)行類型轉(zhuǎn)換時(shí),有可能遇到類似Integer -> int的轉(zhuǎn)換,此時(shí)顯然不能對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.對于java.util.Date/BigDecimal/java.sql.Date/java.sql.Timestamp/java.sql.Time這幾個(gè)類,如果值為null,則在copy時(shí)會拋異常,需要使用對應(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í)會拋異常,例如: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還會經(jīng)常碰到這樣變態(tài)的需求:
- 假設(shè)是從A復(fù)制到B:
- 需求1:如果B中某字段有值(不為null),則該字段不復(fù)制;也就是B中該字段沒值時(shí),才進(jìn)行復(fù)制,適合于對B進(jìn)行補(bǔ)充值的情況。
- 需求2:如果A中某字段沒值(為null),則該字段不復(fù)制,也就是不要把null復(fù)制到B當(dāng)中。
- 對于需求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); } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot?Security認(rèn)證之Redis緩存用戶信息詳解
本文介紹了如何使用Spring Boot Security進(jìn)行認(rèn)證,并通過Redis緩存用戶信息以提高系統(tǒng)性能,通過配置RedisUserDetailsManager,我們成功地將用戶信息存儲到了Redis中,并在Spring Security中進(jìn)行了集成,需要的朋友可以參考下2024-01-01Springboot hibernate envers使用過程詳解
這篇文章主要介紹了Springboot hibernate envers使用過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06詳解eclipse項(xiàng)目中.classpath文件的使用
這篇文章主要介紹了詳解eclipse項(xiàng)目中.classpath文件的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10SpringBoot響應(yīng)出現(xiàn)中文亂碼的解決方法
這篇文章主要介紹了SpringBoot響應(yīng)出現(xiàn)中文亂碼的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起來學(xué)習(xí)吧2024-02-02詳解Java對象轉(zhuǎn)換神器MapStruct庫的使用
在我們?nèi)粘i_發(fā)的程序中,為了各層之間解耦,一般會定義不同的對象用來在不同層之間傳遞數(shù)據(jù)。當(dāng)在不同層之間傳輸數(shù)據(jù)時(shí),不可避免地經(jīng)常需要將這些對象進(jìn)行相互轉(zhuǎn)換。今天給大家介紹一個(gè)對象轉(zhuǎn)換工具M(jìn)apStruct,代碼簡潔安全、性能高,強(qiáng)烈推薦2022-09-09java+mysql實(shí)現(xiàn)圖書館管理系統(tǒng)實(shí)戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了java+mysql實(shí)現(xiàn)圖書館管理系統(tǒng)實(shí)戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12