Orika将两个String字段映射为一个-考虑到其余字段

时间:2018-07-07 15:49:18

标签: java orika

我有这样的状况:

class Person {
    String firstName;
    String lastName;
    Integer age;
    Float height;
//seters, getters, etc.
}

class PersonDto{
     String name; // it should be: firstName + " " + lastName
     Integer personAge;
     Float height;
}

如何映射Person-> PersonDto与所有字段?

1 个答案:

答案 0 :(得分:3)

您可以使用:

mapperFactory.classMap(Person.class, PersonDTO.class)
.field("age","personAge")
.byDefault()
.customize(
   new CustomMapper<Person, PersonDTO> {
      public void mapAtoB(Person a, PersonDTO b, MappingContext context) {
         b.setName(a.getFirstName()+ " "+a.getLastName());
      } 
   })
.register();