根据传入的字段列表,动态调用set方法方法
// 根据传入的字段列表,动态调用set方法方法
static <T> void newFieldAutoSet(T t,Map<String, Object> params, String ... newFields) throws Exception {
try {
for (String newField : newFields) {
Field field = t.getClass().getDeclaredField(newField);
field.setAccessible(true);
String newFieldStr = (String) params.get(newField);
if (StringUtil.isNotBlack(newFieldStr)) {
field.set(t,new BigDecimal(newFieldStr));
} else {
field.set(t,BigDecimal.ZERO);
}
}
} catch (Exception e) {
throw new Exception("限额字段字符串转BigDecimal失败");
}
}
// 调用
// 新增字段字符串转为BigDecimal类型
try {
newFieldAutoSet(record,params,
"mobilePaySingleLimit","mobilePayDayLimit","mobilePayMonthLimit"
,"mobilePaySingleLimitEncrypt","mobilePayDayLimitEncrypt","mobilePayMonthLimitEncrypt"
,"merchantDayLimit","merchantMonthLimit");
} catch (Exception e) {
}
通过知识/经验的分享,节省开发者的时间.