java - 模型映射器计算
问题描述
我正在尝试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
解决方案
您需要添加一个手动转换器来转换一个值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
吸气剂返回
public int getMonthly() {
return annual / 12;
}
推荐阅读
- java - Geotools“无法加载 GDAL 本机库。” 在运行时,在 Eclipse 中可以
- c++ - C++参数包扩展
- c++ - 将程序与未定义的符号链接时遇到问题
- database - 多个线程试图同时在同一个 No-Sql 数据存储上写入
- java - Spring with JWT auth,获取当前用户
- ffmpeg - ffmpeg- 帧数
- selenium - 在动态步骤定义中使用表
- java - 如何突出显示回收站视图的单行或项目并将突出显示的行滚动到屏幕顶部
- javascript - 对象修改事件 FabricJS(I-Text 背景颜色)
- python - 当我在条件语句中检查函数的值时,该值与我返回的变量的值不同