In a project, you often encounter a process of value passing between layers of objects, such as having a persistent class ClassA in the data persistence layer, which can become Viewa in the view layer, and typically, if two classes are structured, the most commonly used is beanutils.copyproperties ( Src,aim) method assigns the value of one object to another object. But if the attribute is different or the name is different, then need Dozer to complete, through the flexible configuration, achieves the copy between the different objects.
As follows:
An XML configuration file: Dozerbeanmapping.xml[XML] View Plain Copy <?xml version= "1.0" encoding= "UTF-8"?> <! doctype mappings public "-//dozer//dtd mappings//en" "http:// Dozer.sourceforge.net/dtd/dozerbeanmapping.dtd "> <mappings> <!-- <class-a> Specifies the source object to be replicated,<class-b> the target object,<a> the source object's property name, <b> the target object's property name. wildcard defaults to True, at which point the default is to map all properties, and if False, only the properties that are configured in the XML file. --> <configuration> <stop-on-errors>false</stop-on-errors> <date-format>MM/dd/yyyy HH:mm</date-format> <wildcard>true</wildcard> </configuration> &NBSP;&NBSP;&NBsp; <mapping> <class-a> com.njusc.view.boxview</class-a> <class-b >com.njusc.view.DepView</class-b> < field> <a>boxid </a> <b>depid </b> </field> <field> <a>depName</a> <b>depName</b> </field> &nbsP </mapping> </mappings>
Below two simple classes [Java] View plain copy package com.njusc.view; public class boxview