项目中,经常会遇到各层对象之间相互进行值传递的过程,如在数据据持久层有一持久类ClassA,在视图层可能会变为ViewA,通常情况下,如果两个类结构一样,最常使用的是BeanUtils.copyProperties(src,aim)方法将一个对象的值赋给另一个对象!但是如果属性不同或者名称不同,则需要Dozer来完成,通过灵活的配置,达到不同对象间的拷贝!dozer当字段名称相同时,可以直接复制,不相同时,可通过配置文件进行复制
具体看一个比较复杂的例子:
package com.jasson.mode; public enum GenderType { male, //男 female //女 }
package com.jasson.mode; import java.util.Date; /** * @author jgao1 * */ public class Info { private int id; private Date createDate; private GenderType gender; public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public GenderType getGender() { return gender; } public void setGender(GenderType gender) { this.gender = gender; } }
package com.jasson.mode; public class InfoVO { private int id; private String date; private Integer gender; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } }
package com.jasson.mode; public class User { private int id; private String name; private String password; private Info info; private String option; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Info getInfo() { return info; } public void setInfo(Info info) { this.info = info; } public String getOption() { return option; } public void setOption(String option) { this.option = option; } }
package com.jasson.mode; public class UserVO { private int id; private String userName; private String password; private InfoVO info; private Integer option; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public InfoVO getInfo() { return info; } public void setInfo(InfoVO info) { this.info = info; } public Integer getOption() { return option; } public void setOption(Integer option) { this.option = option; } }
两个对象相互转换,需要如下自定义convert
package com.jasson.util; import org.dozer.CustomConverter; import com.jasson.mode.GenderType; public class EnumConverter implements CustomConverter { public Object convert(Object destinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) { Object returnVale = null; if (sourceFieldValue != null) { if (sourceFieldValue instanceof Integer) { if ((Integer) sourceFieldValue == 0) { returnVale = GenderType.male; } else if ((Integer) sourceFieldValue == 1) { returnVale = GenderType.female; } } else if (sourceFieldValue instanceof Enum) { if (sourceFieldValue == GenderType.male) { returnVale = 0; } else if (sourceFieldValue == GenderType.female) { returnVale = 1; } } } return returnVale; } }
package com.jasson.util; import org.dozer.CustomConverter; public class SpecialConverter implements CustomConverter { public Object convert(Object destinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) { Object returnVale = null; if (sourceFieldValue != null) { if (sourceFieldValue instanceof Integer) { if ((Integer) sourceFieldValue == 0) { returnVale = "NO"; } else if ((Integer) sourceFieldValue == 1) { returnVale = "OK"; } } else if (sourceFieldValue instanceof String) { if (sourceFieldValue.equals("NO")) { returnVale = 0; } else if (sourceFieldValue.equals("OK")) { returnVale = 1; } } } return returnVale; } }
配置的xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> <configuration> <stop-on-errors>true</stop-on-errors> <date-format>yyyy-MM-dd</date-format> <wildcard>true</wildcard> <custom-converters> <converter type=" com.jasson.util.EnumConverter"> <class-a>java.lang.Enum</class-a> <class-b>java.lang.Integer</class-b> </converter> </custom-converters> </configuration> <mapping date-format="yyyy-MM-dd"> <class-a>com.jasson.mode.User</class-a> <class-b>com.jasson.mode.UserVO</class-b> <field> <a>name</a> <b>userName</b> </field> <field> <a>info.createDate</a> <b>info.date</b> </field> <field custom-converter="com.jasson.util.EnumConverter"> <a>info.gender</a> <b>info.gender</b> </field> <field custom-converter="com.jasson.util.SpecialConverter"> <a>option</a> <b>option</b> </field> </mapping> </mappings>
spring中的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-lazy-init="true"> <bean id="mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean"> <property name="mappingFiles"> <list> <value>classpath*:dozerconfig/dozerBeanMapping.xml</value> </list> </property> </bean> </beans>
package com.jasson.Test; import java.util.Date; import org.dozer.Mapper; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jasson.mode.GenderType; import com.jasson.mode.Info; import com.jasson.mode.User; import com.jasson.mode.UserVO; public class MainTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); Mapper mapper = (Mapper) ctx.getBean("mapper"); Info info = new Info(); info.setCreateDate(new Date()); info.setGender(GenderType.female); info.setId(100); User user = new User(); user.setId(100); user.setName("jasson"); user.setPassword("password"); user.setInfo(info); user.setOption("NO"); UserVO uservo = mapper.map(user, UserVO.class); System.out.println(user.getId()+"<------>"+uservo.getId()); System.out.println(user.getPassword()+"<------>"+uservo.getPassword()); System.out.println(user.getName()+"<------>"+uservo.getUserName()); System.out.println(user.getInfo().getId()+"<------>"+uservo.getInfo().getId()); System.out.println(user.getInfo().getGender()+"<------>"+uservo.getInfo().getGender()); System.out.println(user.getInfo().getCreateDate()+"<------>"+uservo.getInfo().getDate()); System.out.println(user.getOption()+"<------>"+uservo.getOption()); } }
结果如下:
100<------>100
password<------>password
jasson<------>jasson
100<------>100
female<------>1
Mon May 14 18:15:29 CST 2012<------>2012-05-14
NO<------>0
实现完全的复制功能。