A powerful and easy-to-use Java Bean attribute replication Framework--dozer Introduction

Source: Internet
Author: User

Dozer is a Java bean to Java Bean Mapper This recursively copies data from one object to another. Typically, these Java Beans would be the different complex types.

For more information, see: http://dozer.sourceforge.net/documentation/about.html

Two simple examples:

    1. Map-to-bean
      assumes that the page parameter map was obtained by request and wants to convert it to a domain object –product.
      Product has several properties:

      private Long ID;
      private String name;
      Private String description;

      Code:

      //map--> Bean
      map<string,object> map = Maps.newhashmap ();
      Map.put ("id", 10000L);
      Map.put ("name", "Da");
      Map.put ("description", "Gold shell");

      dozerbeanmapper mapper = new Dozerbeanmapper ();
      Product Product = Mapper.map (map, Product.class);
      Assertthat (Product.getid ()). Isequalto (10000L);
      Assertthat (Product.getname ()). Isequalto ("the clatter");
      Assertthat (Product.getdescription ()). Isequalto ("Gold shell");

2. Bean-to-DTO

For the sake of decoupling, a separate DTO object is created for the interface provided to the third party, rather than the entity object that has the existing corresponding database table. It is necessary to transfer data between them.

If a Dto object is now defined, but the property name does not match the property name in the Product object (perhaps to avoid conflicts or ambiguities), as follows:

Private long productId;
Private String ProductName;
Private String desc;

Simply add an annotation to the relevant attribute, as shown below:

@Mapping ("id")

Private long productId;

@Mapping ("name")

Private String ProductName;
@Mapping ("description")
Private String desc;

      Complete code example see:

Product Product = new product ();
Product.setid (10001L);
Product.setname ("Hero");
Product.setdescription ("Black Shell");
Dozerbeanmapper mapper = new Dozerbeanmapper ();
Productdto productdto = Mapper.map (product, Productdto.class);
Assertthat (Productdto.getproductid ()). Isequalto (10001L);
Assertthat (Productdto.getproductname ()). Isequalto ("Hero");
Assertthat (Productdto.getdesc ()). Isequalto ("Black Shell");


A powerful and easy-to-use Java Bean attribute replication Framework--dozer Introduction

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.