我试图在映射过程中使用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实际:
Annual Rate = 12, Monthly Rate: 12
发布于 2018-07-30 13:07:40
您需要添加一个手动转换器来转换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‘s getter返回annual/12。
public int getMonthly() {
return annual / 12;
}https://stackoverflow.com/questions/51593444
复制相似问题