简体   繁体   English

推土机:将单个字段映射到列表中

[英]Dozer: Map single field into a List

How do you map a single field into a List / Collection in Dozer? 如何将单个字段映射到Dozer中的List / Collection

class SrcFoo {
    private String id;
    private List<SrcBar> bars;
}

class SrcBar {
    private String name;
}

Here are my destination objects: 这是我的目标对象:

class DestFoo {
    private List<DestBar> destBars;
}

class DestBar {
    private String fooId; // Populated by SrcFoo.id
    private String barName;
}

I want all DestBar.fooId (entire list of DestBars) to be populated with SrcFoo.id 我希望所有DestBar.fooId (整个DestBar.fooId列表)都使用SrcFoo.id填充

This question is similar to this one posted here, expect I want to map my single field to every item in the list. 这个问题与此处发布的问题类似,我希望将单个字段映射到列表中的每个项目。 Dozer: map single field to Set 推土机:将单个字段映射到设置

I tried the following, but it only populated DestBar.fooId for the first item in the list. 我尝试了以下操作,但DestBar.fooIdDestBar.fooId填充了列表中的第一项。

<mapping> 
     <class-a>SrcFoo</class-a> 
     <class-b>DestFoo</class-b> 
     <field>
         <a>bars</a>
         <b>destBars</b>
     </field> 
     <field>
         <a>id</a>
         <b>destBars.fooId</b> <!-- same affect as destBars[0].fooId ? -->
     </field> 
</mapping>

Dozer does not support that type of mapping. 推土机不支持这种类型的映射。 In order to do that type of mapping, you would have to know the indexes on your Collection (static mapping). 为了进行这种类型的映射,您必须知道Collection上的索引(静态映射)。 This a Job for a custom converter, Create a converter of String to List (of DestBar) like this: 这是一个自定义转换器的Job,创建一个String to List(of DestBar)的转换器,如下所示:

public class YourConverter extends DozerConverter<String, List>

Implement the mapping logic in your converter (Just set the String Id where required) and configure your dozer file like this: 在转换器中实现映射逻辑(只需在需要时设置String Id)并配置您的dozer文件,如下所示:

<mapping> 
     <class-a>SrcFoo</class-a> 
     <class-b>DestFoo</class-b> 
     ...
     <field custom-converter="yourpackage.YourConverter">
         <a>id</a>
         <b>destBars</b>
     </field> 
</mapping>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM