根据传入的字段列表,动态调用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) {
  
}
posted @ 2021-05-07 20:05  ysloong  阅读(295)  评论(0编辑  收藏  举报