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

Orika map two String fields into one - taking into account the remaining fields

我遇到这样的情况:

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 映射到所有字段?

您可以使用:

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();