1. Introduction
Dozer is a JavaBean mapping tool, similar to the Apache Beanutils. But Dozer is more powerful, it can handle the mapping between complex types flexibly. Not only can simple attribute mapping, complex type mapping, bidirectional mapping, recursive mapping and so on, and can be flexibly configured through XML configuration file.
2. Preparation
Let's try it out for a little bit now.
First, you need to download the jar package,
Dozer.jar:http://dozer.sourceforge.net/downloading.html
We need Slf4j.jar,commons-lang.jar,commons-beanutil.jar, Commons-loggin.jar.
http://lishaorui.iteye.com/blog/1151513
Java code
- Import com.google.common.collect.Lists;
- Import java.util.Collection;
- Import Java.util.Iterator;
- Import java.util.List;
- Import Org.dozer.DozerBeanMapper;
- Public class Beanmapper
- {
- private static Dozerbeanmapper dozer = new Dozerbeanmapper ();
- /**
- * Constructs a new Destinationclass instance object, through the contents of the field in the source object
- * Map to the Destinationclass instance object and return the new Destinationclass instance object.
- *
- * @param source data Object
- * @param destinationclass to construct a new instance object class
- */
- public static <T> T map (Object source, class<t> Destinationclass)
- {
- return Dozer.map (source, Destinationclass);
- }
- public static <T> list<t> maplist (Collection sourceList, class<t> destinationclass)
- {
- List destinationlist = Lists.newarraylist ();
- For (Iterator i$ = Sourcelist.iterator (); I$.hasnext ();) {Object sourceObject = I$.next ();
- Object destinationobject = Dozer.map (SourceObject, Destinationclass);
- Destinationlist.add (Destinationobject);
- }
- return destinationlist;
- }
- /**
- * Copy all the property values of the object source into the object destination.
- *
- * @param source Object source
- * @param Destination Object destination
- */
- public static void Copy (object source, Object Destinationobject)
- {
- Dozer.map (source, destinationobject);
- }
- }
Use:
Java code
- Smiaasquotav result = null;
- try {
- result = Limitservice.getlimitdetails (Id,parentid);
- if (Result! = null) {
- Response.setdata (Beanmapper.map (result, map. Class));
- Response.setsuccess (true);
- }
- }
Beanmapper.map (result, Map.class)
Convert to a Map object.
Java code
- Public static <T> map<string, t> tomap (Object target) {
- return Tomap (target,false);
- }
- /**
- * Convert all properties of the target object to a Map object
- *
- * @param target Object
- * @param ignoreparent whether to ignore the properties of the parent class
- *
- * @return Map
- */
- Public static <T> map<string, t> tomap (Object target,boolean ignoreparent) {
- return Tomap (Target,ignoreparent,false);
- }
- /**
- * Convert all properties of the target object to a Map object
- *
- * @param target Object
- * @param ignoreparent whether to ignore the properties of the parent class
- * @param ignoreemptyvalue whether to add null values to the map
- *
- * @return Map
- */
- Public static <T> map<string, t> tomap (Object target,boolean ignoreparent,Boolean Ignoreemptyvalue) {
- return Tomap (Target,ignoreparent,ignoreemptyvalue,new string[0]);
- }
- /**
- * Convert all properties of the target object to a Map object
- *
- * @param target Object
- * @param ignoreparent whether to ignore the properties of the parent class
- * @param ignoreemptyvalue whether to add null values to the map
- * @param ignoreproperties does not need to add a property name to the map
- */
- public static <T> map<string, t> tomap (Object target,boolean ignoreparent,Boolean Ignoreemptyvalue,string ... ignoreproperties) {
- map<string, t> map = new hashmap<string, t> ();
- list<field> fields = Reflectionutils.getaccessiblefields (Target.getclass (), ignoreparent);
- For (iterator<field> it = Fields.iterator (); It.hasnext ();) {
- Field field = It.next ();
- T value = null;
- try {
- Value = (T) field.get (target);
- } catch (Exception e) {
- E.printstacktrace ();
- }
- if (Ignoreemptyvalue
- && ((value = = Null | | value.tostring (). Equals (""))
- || (Value instanceof Collection && ((collection<?>) value). IsEmpty ())
- || (Value instanceof Map && ((map<?,? >) value). IsEmpty ()))) {
- continue;
- }
- Boolean flag = true;
- String key = Field.getname ();
- For (String ignoreproperty:ignoreproperties) {
- if (key.equals (Ignoreproperty)) {
- Flag = false;
- Break ;
- }
- }
- if (flag) {
- Map.put (key, value);
- }
- }
- return map;
- }
Dozerbeanmapper + Object Transfer Map method