MapStruct:将嵌套对象属性映射到属性

时间:2018-05-24 13:07:38

标签: java mapstruct

假设我有以下对象:

class Person {
    String firstName;
    String lastName;
}

class PersonBLO {
    Person person;
    Integer foo; // Some calculated business property
}

class PersonDTO {
    String firstName;
    String lastName;
    Integer foo;
}

我发现自己编写了以下映射器:

@Mapping(target = "firstName", source = "person.firstName")
@Mapping(target = "lastName", source = "person.lastName")
PersonDTO personBLOToPersonDTO(PersonBLO personBLO);

是否可以将所有person.*属性自动映射到相应的*属性?

2 个答案:

答案 0 :(得分:2)

现在,with version 1.4mapstruct以上的用户可以执行以下操作:

@Mapping(target = ".", source = "person")
PersonDTO personBLOToPersonDTO(PersonBLO personBLO);

它将尝试将person的所有字段映射到当前目标。

答案 1 :(得分:0)

目前无法使用通配符。

你可以做的是提供一个只调用正确方法的自定义方法。例如:

/source-charset:utf-8 /execution-charset:utf-8
相关问题