依赖:

commons-beanutils-1.9.3.jar、commons-lang-2.6.jar、dozer-5.3.2.jar、jcl-over-slf4j-1.7.25.jar、slf4j-api-1.7.25.jar

<dependency>
    <groupId>net.sf.dozer</groupId>
    <artifactId>dozer</artifactId>
    <version>5.4.0</version>
</dependency>

  

使用:

1、直接转化

private static Mapper mapper = new DozerBeanMapper();

public static <T> T transfor(Class<T> target, Object source) {
	if(source == null) {
		return null;
	}
	return mapper.map(source, target);
}

public static <T> List<T> transforList(Class<T> clazz, List<T> sources) {
	List<T> list = new ArrayList<>();
	for (Object o : sources) {
		T t = transfor(clazz, o);
		list.add(t);
	}
	return list;
}

  

 

2、根据xml转化

  

DozerBeanMapper mapper =  new DozerBeanMapper(); 
List myMappingFiles = new ArrayList(); 
myMappingFiles.add("people-person-mapper.xml");
mapper.setMappingFiles(myMappingFiles);
Person p1 = new Person();
p1.setId(1);
p1.setName("aa");
People p2 = mapper.map(p1, People.class);

<?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">
	<mapping>
		<class-a>dozer.People</class-a>
		<class-b>dozer.Person</class-b>
			<field>
				<a>id</a>
				<b>id</b>
			</field>
	        <field>
				<a>name</a>
				<b>name</b>
	        </field>
	</mapping>
</mappings>