[英]Map firstName, lastName in one bean to name in another
I am trying to map two beans in Java using Dozer and have the following scenario: 我正在尝试使用Dozer在Java中映射两个bean,并且具有以下情形:
CustomerA has firstName and lastName 客户A具有名字和姓氏
public Class CustomerA {
public String firstName;
public String lastName;
...
// Getters and Setters
}
CustomerB has name, which needs to be mapped to firstName+" "+lastName
CustomerB具有名称,该名称需要映射到firstName+" "+lastName
public Class CustomerB {
public String name;
...
// Getters and Setters
}
Is it possible to achieve this using Dozer? 是否可以使用推土机实现此目的? How will the mapping file look like? 映射文件的外观如何? I have a custom convertor as follows: 我有一个自定义转换器,如下所示:
public class NameConvertor {
public Object convert(Object destination, Object source, Class destClass, Class sourceClass) {
Object retValue = null;
if (source == null) {
return null;
}
if (source instanceof CustomerA) {
CustomerA src = (CustomerA) source;
CustomerB dest = (CustomerB) destination;
if (destination == null) {
dest = new CustomerB();
}
dest.setName(src.getFirstName()+" "+src.getLastName());
retValue = dest;
} else if (source instanceof CustomerB) {
CustomerB src = (CustomerB) source;
CustomerA dest = (CustomerA) destination;
if (destination == null) {
dest = new CustomerA();
}
dest.setFirstName(src.getName().split(" ")[0]);
dest.setLastName(src.getName().split(" ")[1]);
retValue = dest;
}
return retValue;
}
}
You could pass 'this' (your instance of A) into a custom converter, concatenate the two fields together and return them to be mapped to the destination. 您可以将“ this”(您的A的实例)传递到自定义转换器中,将两个字段连接在一起,然后将它们返回以映射到目标。
So, in your mapping you would have something like this: 因此,在映射中您将具有以下内容:
<mapping>
<class-a>path.to.CustomerA</class-a>
<class-b>path.to.CustomerB</class-b>
<field custom-converter="NameConverter" type="one-way">
<a>this</a>
<b>name</b>
</field>
</mapping>
Then in your converter you would have something like this: 然后,在您的转换器中,您将得到以下内容:
CustomerA customerA = (CustomerA) source;
StringBuilder builder = new StringBuilder();
builder.append(customerA.getFirstName());
builder.append(customerA.getLastName());
return builder.toString();
I tested with some sample code and it mapped as expected. 我用一些示例代码进行了测试,并按预期进行了映射。 Let me know if it works for you! 请让我知道这对你有没有用!
How do I map multiple fields to a single field? 如何将多个字段映射到单个字段?
Dozer doesn't currently support this. 推土机目前不支持此功能。 And because of the complexities around implementing it, this feature is not currently on the road map. 而且由于实施它的复杂性,该功能目前尚不在路线图上。 A possible solution would be to wrap the multiple fields in a custom complex type and then define a custom converter for mapping between the complex type and the single field. 一种可能的解决方案是将多个字段包装为自定义复杂类型,然后定义一个自定义转换器,以在复杂类型和单个字段之间进行映射。 This way, you could handle the custom logic required to map the three fields into the single one within the custom converter. 这样,您可以处理将三个字段映射到自定义转换器中的单个字段所需的自定义逻辑。
Source: http://dozer.sourceforge.net/documentation/faq.html#mult-fields-to-single-field 来源: http : //dozer.sourceforge.net/documentation/faq.html#mult-fields-to-single-field
Also I advice to look here http://www.tikalk.com/advanced-dozer-mappings/ 我也建议看这里 http://www.tikalk.com/advanced-dozer-mappings/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.