dozer-First Knowledge

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.

3. Code
Two beans

Java code
  1. Public class Book {
  2. private String name;
  3. private String author;
  4. Public String GetName () {
  5. return name;
  6. }
  7. public void SetName (String name) {
  8. this.name = name;
  9. }
  10. Public String Getauthor () {
  11. return author;
  12. }
  13. public void Setauthor (String author) {
  14. This.author = author;
  15. }
  16. Public book () {
  17. }
  18. }


Java code
  1. Public class Bookvo {
  2. private String Namevo;
  3. private String Authorvo;
  4. Public String Getnamevo () {
  5. return Namevo;
  6. }
  7. public void Setnamevo (String namevo) {
  8. This.namevo = Namevo;
  9. }
  10. Public String Getauthorvo () {
  11. return Authorvo;
  12. }
  13. public void Setauthorvo (String authorvo) {
  14. This.authorvo = Authorvo;
  15. }
  16. Public Bookvo () {
  17. }
  18. }



Bookmapper.xml configuration file, which is used to set the Dozerbeanmapper.

XML code
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <mappings xmlns="http://dozer.sourceforge.net"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemalocation= "Http://dozer.sourceforge.net
  5. Http://dozer.sourceforge.net/schema/beanmapping.xsd ">
  6. <mapping>
  7. <class-a>test. Book</class-a>
  8. <class-b>test. Bookvo</class-b>
  9. <field>
  10. <a>name</a>
  11. <b>namevo</b>
  12. </field>
  13. <field>
  14. <a>author</a>
  15. <b>authorvo</b>
  16. </field>
  17. </mapping>
  18. </Mappings>




Test class

Java code
    1. Package test;
    2. Import java.util.ArrayList;
    3. Import java.util.List;
    4. Import Org.apache.commons.logging.Log;
    5. Import Org.apache.commons.logging.LogFactory;
    6. Import Org.dozer.DozerBeanMapper;
    7. /**
    8. Description
    9. *
    10. * @author LSR
    11. * @version 17, 2011
    12. */
    13. Public class Dozertest {
    14. /** 
    15. * @param args
    16. */
    17. public static void Main (string[] args) {
    18. Testbyxml ();
    19. }
    20. public static void Testbyxml () {
    21. Book book = new book ();
    22. Book.setauthor ("LSR");
    23. Book.setname ("dozer demo");
    24. Dozerbeanmapper mapper = new Dozerbeanmapper ();
    25. list<string> mappers = new arraylist<string> ();
    26. Mappers.add ("Bookmapper.xml");
    27. Mapper.setmappingfiles (mappers);
    28. Bookvo vo = new Bookvo ();
    29. Mapper.map (book, VO);
    30. System.out.println ("book ' s Name:" + book.getname ());
    31. System.out.println ("Bookvo ' s Name:" + Vo.getnamevo ());
    32. }
    33. }

dozer-First Knowledge

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.