BeanUtils tools copy of the object copyProperties

    <1> Spring provides a very good object replication method copyProperties, members of the object assignment is based on the list of members of the target object, and will skip the field and ignore the source object does not exist, this method is safe, not because two structural differences between objects cause an error, but must ensure that the same two members of the same name tag type.

a, b for the object, a target object can not be null or b

BeanUtils.copyProperties (a, b);

org.springframework.beans.BeanUtils: a copy to b,

org.apache.commons.beanutils.BeanUtils: b copy to a.

When the status variable is not present in a, b in the presence of the status variable as a long, after the result of the execution, b the status becomes 0,

View source if the source attribute value is null, then, long type variable sets the default value.

    Limit <2> BeanUtils.copyProperties () method

Only when the two classes are the same attribute will be copied, not treated with different properties;

For java.util.Date class does not support, for java.sql.Date support;

Interger, long type default value is null, a default value is 0 after use, to be noted.

Before using BeanUtils.copyProperties copy field, the default field value converting some common digital type is null, Interger, Long does not like the default null field is converted to a 0. Apache.commons can be used inside the packet converter Long, et Interger default value setting field is null.

static{
    ConvertUtils.register(new LongConverter(null),Long.class);
    ConvertUtils.register(new IntergerConverter(null),Integer.class);
}

 

 

Published 36 original articles · won praise 19 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_27182767/article/details/91528038