maven引入依赖
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.2.1</version>
</dependency>
创建映射文件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">
<mapping>
<class-a>handler.entity.Vo</class-a>
<class-b>handler.entity.Po</class-b>
<field>
<a>ss</a>
<b>id</b>
</field>
<field>
<a>username</a>
<b>name</b>
</field>
<field>
<a>time</a>
<b>localTime</b>
</field>
<field>
<a>path1</a>
<b>image1</b>
</field>
<field>
<a>path2</a>
<b>image2</b>
</field>
<field>
<a>path3</a>
<b>image3</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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-autowire="byName" default-lazy-init="false">
<bean id="mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean">
<property name="mappingFiles">
<list>
<value>classpath*:dozer/dozerBeanMapping.xml</value>
</list>
</property>
</bean>
</beans>
test测试
import handler.entity.Po;
import handler.entity.Vo;
import org.dozer.Mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author whh
* @date 2019/3/15 15:22
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath:spring-dozer.xml"})
public class Test2 {
@Autowired
Mapper mapper;
@Test
public void ss() {
Po po = new Po();
po.setId(110L);
po.setName("测试");
po.setImage1("11");
po.setImage2("22");
po.setImage3("33");
Vo map = mapper.map(po, Vo.class);
System.out.println("map = " + map);
}
}