Apache BeanUtils

Environment

commons-beanutils-1.8.0.jar

Usage

Bean對拷,用在基本型別,不適用Deep Copy,需客製

 BeanUtils.copyProperties(target, source);

由於 Long 等 Object 在做 Copy 時,預設會轉成Default Value (If Long Object is Null, default is new Long(0));如果要維持 Null,則需Register Converter (針對個別資料型態)

 ConvertUtilsBean converter = BeanUtilsBean.getInstance().getConvertUtils();
 converter.register(new LongConverter(null), Long.class);

如果所有資料型態都要維持 Null,則為:

 ConvertUtilsBean converter = BeanUtilsBean.getInstance().getConvertUtils();
 converter.register(false, true, 0);

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s