I need to do something that looks like a simple mapping but I still can't manage how to do it. What I need to do is to map firstName + lastName on the first class to name on the second one.
Something like this:
class Person {
String firstName;
String lastName;
}
class PrintablePerson {
String name; // name should be firstName+" "+(lastName)
}
Wich is the best way to accomplish this?
UPDATE:
I already solved this by implementing my own Mapper by doing this:
public class MyCustomMapper extends CustomMapper<Person, PrintablePerson> {
@Override
public void mapAtoB(Person person, PrintablePerson printablePerson, MappingContext context) {
printablePerson.setName(person.getFirstName() + " " + person.getLastName());
}
}
Then i called the mapper in the customize method by using:
mapperFactory.classMap(Person.class, PrintablePerson.class)
.byDefault()
.customize(
new MyCustomMapper()
).register();
Look at Customizing individual ClassMaps
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.