Dozer is a Java Bean Copy class library that performs better than Apache Beanutils, but it is inconvenient that both of them will copy the empty attributes. In Dozer, you can disable an empty property copy using an XML map, and you have to configure XML to feel cumbersome.
In fact, you can construct a beanmappingbuilder to configure the mapping.
Mapping (Sources.getclass (), Destination.getclass (), Mapnull (false), mapemptystring (false));
respectively on
Org.dozer.loader.api.TypeMappingOptions.mapEmptyString
Org.dozer.loader.api.TypeMappingOptions.mapNull
Set to False.
So it can be encapsulated into a util
public static void Copyproperties (final object sources, final object destination) {Weakr
Eference weakreference = new WeakReference (new Dozerbeanmapper ());
Dozerbeanmapper mapper = (dozerbeanmapper) weakreference.get ();
Mapper.addmapping (New Beanmappingbuilder () {@Override protected void Configure () {
Mapping (Sources.getclass (), Destination.getclass (), Mapnull (false), mapemptystring (false));
}
});
Mapper.map (sources, destination);
Mapper.destroy ();
Weakreference.clear (); }