Dozer study notes, bulldozer

Source: Internet
Author: User

Dozer study notes, bulldozer
Introduction

Dozer is a javaBean an ing tool used to automatically adapt a class to another class. It supports Simple ing, it also supports bidirectional recursive ing of complex types (official website ).

Example

For example, assume that we now have two object classes: User and UserDetail. But when we publish the object to the public, we released two transmission objects: UserDto and UserSimpleDto, now we can achieve mutual conversion between them:

 

User: record login name and password

1/** 2 * @ author X 3 */4 public class User {5 6 private String loginName; // login name 7 private String password; // login password 8 private UserDetail userDetail; // user details 9 10 10 public String getLoginName () {11 return loginName; 12} 13 14 public void setLoginName (String loginName) {15 this. loginName = loginName; 16} 17 18 public String getPassword () {19 return password; 20} 21 22 public void setPassword (String password) {23 this. password = password; 24} 25 26 public UserDetail getUserDetail () {27 return userDetail; 28} 29 30 public void setUserDetail (UserDetail userDetail) {31 this. userDetail = userDetail; 32} 33}User. java

User details: record the user's mobile phone number

1/** 2 * @ author X 3 */4 public class UserDetail {5 6 private String phoneNumber; // mobile phone number 7 8 public String getPhoneNumber () {9 return phoneNumber; 10} 11 12 public void setPhoneNumber (String phoneNumber) {13 this. phoneNumber = phoneNumber; 14} 15}UserDetail. java

User transmission object: records the user's login name and user's mobile phone number

1/** 2 * @ author X 3 */4 public class UserDto {5 6 private String userName; // userName 7 private String userPhone; // The user's mobile phone 8 9 public String getUserName () {10 return userName; 11} 12 13 public void setUserName (String userName) {14 this. userName = userName; 15} 16 17 public String getUserPhone () {18 return userPhone; 19} 20 21 public void setUserPhone (String userPhone) {22 this. userPhone = userPhone; 23} 24}UserDto. java

User simple transfer object: records the user's login name

1/** 2 * @ author X 3 */4 public class UserSimpleDto {5 private String loginName; // login name 6 7 public String getLoginName () {8 return loginName; 9} 10 11 public void setLoginName (String loginName) {12 this. loginName = loginName; 13} 14}UserSimpleDto. javaUser-> UserSimpleDto

This is very simple. Because the values are the same, no additional configuration is required. The Code is as follows:

Import org. dozer. dozerBeanMapper; import org. dozer. mapper;/*** @ author X */public class Run {public static void main (String [] args) {// assign User user User = new User () to the target object (); user. setLoginName ("shaguar"); user. setPassword ("123456"); // converts Mapper mapper = new DozerBeanMapper (); UserSimpleDto dto = mapper. map (user, UserSimpleDto. class); System. out. println (dto. getLoginName ());}}Run. java

Running result:

User-> UserDto

This is more complicated, because UserDto contains not only the User name, but also the PhoneNumber in UserDetail. We need to manually configure it to solve the Map problem.

First: add multiple configuration files

Ing Configuration

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <mappings xmlns = "http://dozer.sourceforge.net" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 3 xsi: schemaLocation = "http://dozer.sourceforge.net 4 http://dozer.sourceforge.net/schema/beanmapping.xsd"> 5 <mapping> 6 <class-a> User </class-a> 7 <class-B> UserDto </class-B> 8 <field> 9 <a> UserDetail. phoneNumber </a> 10 <B> userPhone </B> 11 </field> 12 <field> 13 <a> loginName </a> 14 <B> userName </B> 15 </field> 16 </mapping> 17 </mappings>UserToUserDetailConfig. xml

Call method (you only need to set the configuration file before ing .)

1 import org. dozer. dozerBeanMapper; 2 import org. dozer. mapper; 3 4 import java. util. arrayList; 5 import java. util. list; 6 7/** 8 * @ author X 9 */10 public class Run {11 public static void main (String [] args) {12 // assign 13 User user = new User (); 14 user to the target object. setLoginName ("shaguar"); 15 user. setPassword ("123456"); 16 UserDetail userDetail = new UserDetail (); 17 userDetail. setPhoneNumber ("1881234567890"); 18 user. setUserDetail (userDetail); 19 20 List mappingFile = new ArrayList (); 21 mappingFile. add ("UserToUserDtoConfig. xml "); 22 DozerBeanMapper mapper = new DozerBeanMapper (); 23 mapper. setMappingFiles (mappingFile); 24 UserDto dto = mapper. map (user, UserDto. class); 25 System. out. println (dto. getUserName (); 26 System. out. println (dto. getUserPhone (); 27 28} 29}Run. java

Type 2: Add a default configuration file (multiple mappings)

Of course, if you do not want to load a configuration file every time, you can write the file named dozerBeanMapping according to dozer's conventions. xml configuration, and put it into the root directory, so you can use the following method to create the dozer Mapper:

1 import org. dozer. dozerBeanMapper; 2 import org. dozer. dozerBeanMapperSingletonWrapper; 3 import org. dozer. mapper; 4 5 import java. util. arrayList; 6 import java. util. list; 7 8/** 9 * @ author X10 */11 public class Run {12 public static void main (String [] args) {13 // assign 14 User user = new User (); 15 user to the target object. setLoginName ("shaguar"); 16 user. setPassword ("123456"); 17 UserDetail userDetail = new UserDetail (); 18 userDetail. setPhoneNumber ("1881234567890"); 19 user. setUserDetail (userDetail); 20 21 Mapper mapper = DozerBeanMapperSingletonWrapper. getInstance (); 22 UserDto dto = mapper. map (user, UserDto. class); 23 System. out. println (dto. getUserName (); 24 System. out. println (dto. getUserPhone (); 25} 26}Run. java

The following results can be obtained in either of the two methods:

Integration with spring

Let me study it (To be continued ......)

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.