虽然目前已经有很多文章介绍此方法的用法,但是既然自己遇到了。 还是记录一下吧。
导包:import org.springframework.beans.BeanUtils;
源代码的介绍:
/**
* Copy the property values of the given source bean into the target bean.
* <p>Note: The source and target classes do not have to match or even be derived
* from each other, as long as the properties match. Any bean properties that the
* source bean exposes but the target bean does not will silently be ignored.
* <p>This is just a convenience method. For more complex transfer needs,
* consider using a full BeanWrapper.
* @param source the source bean
* @param target the target bean
* @throws BeansException if the copying 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 赋值
(未验证,从别的博客看到)要注意一点,java.util.Date是不被支持的,而它的子类java.sql.Date是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用java.sql.Date类型。否则在转换时会提示argument mistype异常。