beanmapping之Orika

在处理后端协议时,时常需要将外域bean对象转换成当前域的bean对象,这时就需要对两个bean进行内容复制,如果觉得手动编码效率低,可以试试Orika开源库。
https://github.com/orika-mapper/orika
http://orika-mapper.github.io/orika-docs/

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(PersonDTO.class, Person.class) //A ClassMapBuilder
        .field("lastNames", "surnames") //Register field mappings
        .field("streetAddress", "address.street")
        .field("city", "address.city")
        .field("postalCode", "address.zipCode")
        .byDefault() //the remaining fields on both classes should be mapped matching the fields by name
        .register(); //register the mapping with the MapperFactory.