dozer GitHub地址:https://github.com/DozerMapper/dozer
官方wiki:Introduction · GitBook
第一步:导包
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-core</artifactId>
<version>6.5.2</version>
</dependency>
第二步:配置转换器
DozerConfig.java文件
package com.ymf.invoice.config;
import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.List;
@Configuration
public class DozerConfig {
@Bean
public Mapper dozerBeanMapper() {
List<String> mappingFiles = Arrays.asList(
"dozer-configration-mapping.xml"
);
return DozerBeanMapperBuilder.create()
.withMappingFiles(mappingFiles)
.build();
}
}
dozer-configration-mapping.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
</mappings>
第三步:简单测试
import com.github.dozermapper.core.Mapper;
@Autowired
private Mapper mapper;
InvoiceTaskDto invoiceTaskDto = mapper.map(invoiceTask, InvoiceTaskDto.class);
本文介绍了如何在Java应用中使用Dozer库进行对象映射。首先,通过Maven添加Dozer的依赖。接着,配置DozerBeanMapper,并指定映射配置文件。最后,展示了简单的对象映射测试用例,将InvoiceTask对象转换为InvoiceTaskDto对象。
1395

被折叠的 条评论
为什么被折叠?



