[英]Orika Mapping: Concat of two elements to one
我需要做一些看似简单映射的事情,但我仍然无法管理如何做到这一点。 我需要做的是将第一个类的firstName + lastName映射到第二个类的name。
像这样的东西:
class Person {
String firstName;
String lastName;
}
class PrintablePerson {
String name; // name should be firstName+" "+(lastName)
}
这是实现这一目标的最佳方式吗?
更新:
我已经通过这样做实现了我自己的Mapper来解决这个问题:
public class MyCustomMapper extends CustomMapper<Person, PrintablePerson> {
@Override
public void mapAtoB(Person person, PrintablePerson printablePerson, MappingContext context) {
printablePerson.setName(person.getFirstName() + " " + person.getLastName());
}
}
然后我使用以下方法在customize方法中调用mapper:
mapperFactory.classMap(Person.class, PrintablePerson.class)
.byDefault()
.customize(
new MyCustomMapper()
).register();
mapperFactory.classMap(Person.class, PrintablePerson.class)
.byDefault()
.customize(
new CustomMapper<Person, PrintablePerson>() {
public void mapAtoB(Person a, PrintablePerson b, MappingContext context) {
// add your custom mapping code here
b.setName(a.getFirstName() + " " + a.getLastName());
}
})
.register();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.