dozer Mapper
本篇记录整理了在了解和使用dozer Mapper进行类转换的时候的一些知识点(因用的是xml配置的,因此以xml配置为主)。
一、引入dozer Mapper
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.4.0</version>
</dependency>
二、dozerBeanMapper.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配置-->
<mapping>
<class-a>com.example.dozer.BeanA</class-a>
<class-b>com.example.dozer.BeanBF</class-b>
<field>
<a>name</a>
<b>relation</b>
</field>
</mapping>
</mappings>
三、dozer Mapper的使用
3.1 通过DozerBeanMapper
Mapper mapper = new DozerBeanMapper();
BeanA sourceObject = new BeanA();
BeanBF destObject =
mapper.map(sourceObject, BeanBF.class);
这个时候运行的话,并不会成功转换成BeanBF实体类。因为xml文件并没有加载到Mapper mapper中。所以可以看一下DozerBeanMapper类中集中构造方法:
/*无参构造器,不指定任何参数*/
public DozerBeanMapper() {
this(Collections.emptyList());
}
/*指定xml文件地址的List<String>*/
public DozerBeanMapper(List<String> mappingFiles) {
this.log = LoggerFactory.getLogger(DozerBeanMapper.class);
this.statsMgr = GlobalStatistics.getInstance().getStatsMgr();
this.initializing = n