MapStruct 子实体

MapStruct Child Entity

我正在尝试使用 mapstruct 将 DTO 转换为我的 class 表示形式。

class 如下所示:

public class Loan{
    private Amount total;
    private Amount paid;
}

public class Amount{
    private Long amount;
    private String currency;
}

DTO class 类似于:

public class LoanDTO{
    private Long paidAmount;
    private Long totalAmount;
    private String currency;
}

我的映射器函数应该是这样的,但我不确定如何才能正确映射 'Amount'。

@Mapper
public interface ResposeMap {

    Loan toLoan(LoanDTO loanDTO);
}

如果你使用java8和mapstruct 1.3(也许它在早期版本中工作)那么它可以是这样的:

@Mapper
public interface ResposeMap {

    @Mapping(target = "total.amount", source = "totalAmount")
    @Mapping(target = "total.currency", source = "currency")
    @Mapping(target = "paid.amount", source = "paidAmount")
    @Mapping(target = "paid.currency", source = "currency")
    Loan toLoan(LoanDTO loanDTO);
}

更多信息:Mapstruct documentation