gpt4 book ai didi

java - 模型映射器计算

转载 作者:行者123 更新时间:2023-12-02 02:00:55 26 4
gpt4 key购买 nike

我正在尝试使用 ModelMapper 在映射过程中计算属性。这可能吗,因为它没有按我的预期工作。

PropertyMap<com.fmg.myfluent.domain.Quote, ClientQuote> personMap = new     
PropertyMap<com.fmg.myfluent.domain.Quote, ClientQuote>() {
protected void configure() {
map().setTotalLoan(source.getTotalPayable());
// monthlyRate NOT Working!
map().setMonthlyRate((source.getAnnualRate()/12));
}
};

我预计月费率为年费率/12。但是,月费率在没有计算的情况下设置为年费率。

预期:

 Annual Rate = 12, Monthly Rate: 1

实际:

年费率 = 12,月费率:12

最佳答案

您需要添加手动转换器来转换 ModelMapper 中的值

Converter<Integer, Integer> annualToMonthlyConverter = ctx -> ctx.getSource() == 0 ? 0 : ctx.getSource() / 12;

现在使用此转换器将源年度字段转换为目标每月字段

PropertyMap<Source, Target> personMap = new
PropertyMap<Source, Target>() {
protected void configure() {
map().setAnnual(source.getAnnual());

using(annualToMonthlyConverter).map(source.getAnnual(), destination.getMonthly());
}
};

注意:

只是一个想法,根据你的设计,你也可以只映射源的年度字段,然后返回 annual/12来自您的目标类别的 monthly的 setter/getter

public int getMonthly() {
return annual / 12;
}

关于java - 模型映射器计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51593444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com