2

我需要做一些看起来像简单映射的事情,但我仍然无法管理如何去做。我需要做的是将第一个类上的 firstName + lastName 映射到第二个类上的名称。

像这样的东西:

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

然后我使用以下方法在自定义方法中调用映射器:

mapperFactory.classMap(Person.class, PrintablePerson.class)
.byDefault()
.customize(
     new MyCustomMapper()
 ).register();
4

1 回答 1

3

查看自定义单个 ClassMap

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();
于 2014-04-08T12:16:06.763 回答