87.6.2. 自定义映射
通过自定义映射,您可以定义自己的逻辑,以便源字段映射到 target 字段。 它们的功能类似于 Dozer 客户转换器,有两个显著的区别:
- 您可以在一个类中有多个转换器方法,使用自定义映射。
- 不需要使用自定义映射实施特定于 Dozer 的界面。
通过使用映射配置中的内置 '_customMapping' 转换程序来声明自定义映射。 这个转换器的参数具有以下语法:
[class-name][,method-name]
方法名称是可选的 - Dozer 组件将搜索与映射所需的输入和输出类型匹配的方法。 下面提供了自定义映射和配置示例。
public class CustomMapper {
// All customer ids must be wrapped in "[ ]"
public Object mapCustomer(String customerId) {
return "[" + customerId + "]";
}
} <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">
<mapping>
<class-a>org.example.A</class-a>
<class-b>org.example.B</class-b>
<field custom-converter-id="_customMapping"
custom-converter-param="org.example.CustomMapper,mapCustomer">
<a>header.customerNum</a>
<b>custId</b>
</field>
</mapping>
</mappings>