搜索
您的当前位置:首页正文

BeanUtils.copyProperties() 用法

来源:知库网

虽然目前已经有很多文章介绍此方法的用法,但是既然自己遇到了。 还是记录一下吧。

/**

* Copythepropertyvaluesofthegivensource beanintothetarget bean.

*

Note: The sourceandtarget classes donothavetomatchoreven be derived

*fromeach other,aslongastheproperties match. Any bean propertiesthatthe* source bean exposesbutthetarget beandoesnotwill silently be ignored.

*

Thisisjust a convenience method. For more complex transfer needs,

* consider using a full BeanWrapper.

* @param sourcethesource bean

* @param targetthetarget bean

* @throws BeansExceptionifthecopying failed

* @see BeanWrapper

*/

public static void copyProperties(Object source, Object target) throws BeansException {

copyProperties(source, target, null, (String[]) null);

}

介绍:

BeanUtils.copyProperties(bean, detailBean); //bean 给detailBean 赋值

此方法是将前面的bean 给detailBean赋值。

详细介绍:

BorrowInfoBean bean=new BorrowInfoBean();

bean=this.getBorrowInfo(999); //此句意思就是查询bean的信息;

BorrowInfoBean detailBean=new BorrowInfoBean (); //新的bean

BeanUtils.copyProperties(bean, detailBean); //bean 给detailBean 赋值

Top