MapStruct子实体

问题描述 投票:0回答:1

我试图使用mapstruct将一个DTO转换为我的类表示。

该类看起来像下面的东西。

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

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

DTO类应该是这样的:

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

我的映射函数应该是这样的 但我不知道如何才能正确映射 "金额"。

@Mapper
public interface ResposeMap {

    Loan toLoan(LoanDTO loanDTO);
}

java spring-boot mapstruct
1个回答
1
投票

如果你使用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文档

© www.soinside.com 2019 - 2024. All rights reserved.