java中如何使用Beanutils.copyProperties()

小編這次要給大家分享的是java中如何使用Beanutils.copyProperties( ),文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

蒲縣ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

昨天測試小姐姐將我的一個(gè)bug單重開了,emmmm....內(nèi)心OS:就調(diào)整下對象某個(gè)屬性類型這么簡單的操作,我怎么可能會出錯呢,一定不是我的鍋?。ut再怎么抗拒,bug還是要改的,畢竟晚上就要發(fā)版本了~~

老老實(shí)實(shí)將我前天改的部分跟了一遍,恩,完美,沒有任何的缺失~~but本應(yīng)success的測試數(shù)據(jù),接口返還的結(jié)果確實(shí)是false來著,那還是老老實(shí)實(shí)debug吧。

一步步跟下來,恩,多么順暢,就說一定不是我的鍋~~誒?不對不對,這里的ID值,為啥是null?傳過來有值的呀??!發(fā)現(xiàn)原來是別人在修復(fù)bug時(shí)用了Beanutils.copyProperties( obj1,obj2),but 因?yàn)閛bj1中屬性名稱id,obj2中屬性名稱afterId,因?yàn)锽eanutils.copyProperties( )要求處理的兩個(gè)對象的屬性名稱相同時(shí)才可以正常賦值,那好吧,我們今天就好好講講關(guān)于Beanutils.copyProperties( )吧~

1、簡介

BeanUtils提供對Java反射和自省API的包裝。其主要目的是利用反射機(jī)制對JavaBean的屬性進(jìn)行處理。

2、用法

如果有兩個(gè)具有很多相同屬性的JavaBean,一個(gè)很常見的情況就是Struts里的PO對象(持久對象)和對應(yīng)的ActionForm。例如:一個(gè)用戶注冊頁面,有一個(gè)User實(shí)體類和一個(gè)UserActionForm實(shí)體類,我們一般會在Action里從ActionForm構(gòu)造一個(gè)PO對象,傳統(tǒng)的方式是使用類似下面的語句對屬性逐個(gè)賦值:

// 獲取 ActionForm 表單數(shù)據(jù) 
 UserActionForm uForm = (UserActionForm) form; 

// 構(gòu)造一個(gè)User對象 
User user = new User(); 
 
// 逐一賦值 
user.setUsername(uForm.getUsername); 
user.setPassword(uForm.getPassword); 
user.setAge(uForm.getAge); 
........... 
........... 
 
// 然后調(diào)用JDBC、或操作Hibernate 持久化對象User到數(shù)據(jù)庫 
... 

通過這樣的方法如果表單屬性字段很多,達(dá)到了100、1000甚至更多,那我們不是要寫100、1000行set、get了。誰都不愿意這樣做。

而我們使用 BeanUtils.copyProperties() 方法以后,代碼量大大的減少,而且整體程序看著也簡潔明朗,代碼如下:

// 獲取 ActionForm 表單數(shù)據(jù) 
UserActionForm uForm = (UserActionForm) form; 

// 構(gòu)造一個(gè)User對象 
User user = new User(); 
 
// 賦值 
BeanUtils.copyProperties(user, uForm); 

// 然后調(diào)用JDBC、或操作Hibernate 持久化對象User到數(shù)據(jù)庫 
....... 

注:如果User和UserActionForm 間存在名稱不相同的屬性,則BeanUtils不對這些屬性進(jìn)行處理,需要手動處理。例如:
User類里面有個(gè)createDate 創(chuàng)建時(shí)間字段,而UserActionForm里面無此字段。BeanUtils.copyProperties()不會對此字段做任何處理。必須要自己手動處理。
用法總結(jié)如下:

[java] view plain copy print?

BeanUtils.copyProperties("要轉(zhuǎn)換的類", "轉(zhuǎn)換后的類"); 
[java] view plain copy print?

PropertyUtils.copyProperties("要轉(zhuǎn)換的類", "轉(zhuǎn)換后的類");

用法其實(shí)很簡單,第一個(gè)參數(shù)是要轉(zhuǎn)換的類,第二個(gè)參數(shù)是轉(zhuǎn)換后的類。

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); 
 } 
}

看完這篇關(guān)于java中如何使用Beanutils.copyProperties( )的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。

本文標(biāo)題:java中如何使用Beanutils.copyProperties()
網(wǎng)頁地址:http://bm7419.com/article38/pcoosp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)網(wǎng)站制作、小程序開發(fā)、商城網(wǎng)站、App開發(fā)云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化