Dozerbeanmapper + Object Transfer Map method

Source: Internet
Author: User

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
  1. Import com.google.common.collect.Lists;
  2. Import java.util.Collection;
  3. Import Java.util.Iterator;
  4. Import java.util.List;
  5. Import Org.dozer.DozerBeanMapper;
  6. Public class Beanmapper
  7. {
  8. private static Dozerbeanmapper dozer = new Dozerbeanmapper ();
  9. /** 
  10. * Constructs a new Destinationclass instance object, through the contents of the field in the source object
  11. * Map to the Destinationclass instance object and return the new Destinationclass instance object.
  12. *
  13. * @param source data Object
  14. * @param destinationclass to construct a new instance object class
  15. */
  16. public static <T> T map (Object source, class<t> Destinationclass)
  17. {
  18. return Dozer.map (source, Destinationclass);
  19. }
  20. public static <T> list<t> maplist (Collection sourceList, class<t> destinationclass)
  21. {
  22. List destinationlist = Lists.newarraylist ();
  23. For (Iterator i$ = Sourcelist.iterator (); I$.hasnext ();)  {Object sourceObject = I$.next ();
  24. Object destinationobject = Dozer.map (SourceObject, Destinationclass);
  25. Destinationlist.add (Destinationobject);
  26. }
  27. return destinationlist;
  28. }
  29. /** 
  30. * Copy all the property values of the object source into the object destination.
  31. *
  32. * @param source Object source
  33. * @param Destination Object destination
  34. */
  35. public static void Copy (object source, Object Destinationobject)
  36. {
  37. Dozer.map (source, destinationobject);
  38. }
  39. }

Use:

Java code
  1. Smiaasquotav result = null;
  2. try {
  3. result = Limitservice.getlimitdetails (Id,parentid);
  4. if (Result! = null) {
  5. Response.setdata (Beanmapper.map (result, map.   Class));
  6. Response.setsuccess (true);
  7. }
  8. }

Beanmapper.map (result, Map.class)

Convert to a Map object.

Java code
    1. Public static <T> map<string, t> tomap (Object target) {
    2. return Tomap (target,false);
    3. }
    4. /**
    5. * Convert all properties of the target object to a Map object
    6. *
    7. * @param target Object
    8. * @param ignoreparent whether to ignore the properties of the parent class
    9. *
    10. * @return Map
    11. */
    12. Public static <T> map<string, t> tomap (Object target,boolean ignoreparent) {
    13. return Tomap (Target,ignoreparent,false);
    14. }
    15. /**
    16. * Convert all properties of the target object to a Map object
    17. *
    18. * @param target Object
    19. * @param ignoreparent whether to ignore the properties of the parent class
    20. * @param ignoreemptyvalue whether to add null values to the map
    21. *
    22. * @return Map
    23. */
    24. Public static <T> map<string, t> tomap (Object target,boolean ignoreparent,Boolean Ignoreemptyvalue) {
    25. return Tomap (Target,ignoreparent,ignoreemptyvalue,new string[0]);
    26. }
    27. /**
    28. * Convert all properties of the target object to a Map object
    29. *
    30. * @param target Object
    31. * @param ignoreparent whether to ignore the properties of the parent class
    32. * @param ignoreemptyvalue whether to add null values to the map
    33. * @param ignoreproperties does not need to add a property name to the map
    34. */
    35. public static <T> map<string, t> tomap (Object target,boolean ignoreparent,Boolean Ignoreemptyvalue,string ... ignoreproperties) {
    36. map<string, t> map = new hashmap<string, t> ();
    37. list<field> fields = Reflectionutils.getaccessiblefields (Target.getclass (), ignoreparent);
    38. For (iterator<field> it = Fields.iterator (); It.hasnext ();) {  
    39. Field field = It.next ();
    40. T value = null;
    41. try {
    42. Value = (T) field.get (target);
    43. } catch (Exception e) {
    44. E.printstacktrace ();
    45. }
    46. if (Ignoreemptyvalue
    47. && ((value = = Null | | value.tostring (). Equals (""))
    48. || (Value instanceof Collection && ((collection<?>) value). IsEmpty ())
    49. || (Value instanceof Map && ((map<?,? >) value). IsEmpty ()))) {  
    50. continue;
    51. }
    52. Boolean flag = true;
    53. String key = Field.getname ();
    54. For (String ignoreproperty:ignoreproperties) {
    55. if (key.equals (Ignoreproperty)) {
    56. Flag = false;
    57. Break ;
    58. }
    59. }
    60. if (flag) {
    61. Map.put (key, value);
    62. }
    63. }
    64. return map;
    65. }

Dozerbeanmapper + Object Transfer Map method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.