Copy attributes tools

Transfer: https://blog.csdn.net/howroad/article/details/91308579

We are encountered in data replication between the value of the objects and entities in development.

One of the most common is Apache BeanUtilsBean of copyProperties.

The test found that most of the data format compatible with the tool, but in Java.sql.Date and java.util.Date copy, the original data will be sent as an error is empty.

By registering BeanUtilsBean converters, a simple method of packaging copyProperties.

Complete results: Method tools after copying objects encapsulate the call attributes, data always give a new face Date corresponding to the object attribute assignment is null.

 

End use:

The attach new new = the Attach the Attach ();
attach.setCreatePerson ( "123");
//attach.setCreateTime(new a Date ());
EbankApply ebankApply new new EbankApply = ();
EbankApply ebankApply2 new new EbankApply = ();
EbankApply ebankApply4 = new new EbankApply ();
the try {
org.apache.commons.beanutils.BeanUtils.copyProperties (ebankApply, the attach); // given a Date data is empty
org.apache.commons.beanutils.PropertyUtils.copyProperties (ebankApply2, attach); // normal
org.springframework.beans.BeanUtils.copyProperties (attach, ebankApply4); // Note that the order parameters and Apache reverse
} the catch (IllegalAccessException E) {
e.printStackTrace ();
} the catch (a InvocationTargetException E) {
e.printStackTrace ();
} catch (NoSuchMethodException e) {
e.printStackTrace ();
}
System.out.println (the attach);
System.out.println (ebankApply);
System.out.println (ebankApply2);
System.out.println (ebankApply4);
is not used BeanUtil, using PropertyUtils
----------------
Disclaimer: this article is CSDN blogger "howroad 'original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/howroad/article/details/91308579

Guess you like

Origin www.cnblogs.com/sharpest/p/11406778.html