将集合转换为实体数据

发布时间:2020-06-24 21:06:21 作者:ffly1209
来源:网络 阅读:265

public void convertMapToDataEntity(Map map) throws SystemException {
  Field[] fields = super.getClass().getDeclaredFields();
  String strValue = null;

  Field currField = null;
  String currFieldName = null;
  String currFieldTypeName = null;
  String methodName = null;
  Method currMethod = null;
  try {
   for (int i = 0; i < fields.length; ++i) {
    currField = fields[i];

    currFieldName = currField.getName();
    if (!(map.containsKey(currFieldName.toLowerCase())))
     continue;
    strValue = (String) map.get(currFieldName.toLowerCase());
    this.logger.debug("map参数:" + currFieldName + " =  " + strValue);

    if ((strValue == null) || (strValue.equals("")))
     continue;
    currFieldTypeName = currField.getType().getName();
    methodName = "set"
      + currFieldName.substring(0, 1).toUpperCase()
      + currFieldName.substring(1);

    if (currFieldTypeName.equals(Double.TYPE.getName())) {
     currMethod = super.getClass().getMethod(methodName,
       new Class[] { Double.TYPE });
     currMethod.invoke(this, new Object[] { new Double(
       DataFormat.parseNumber(strValue.trim())) });
    } else if (currFieldTypeName.equals(Long.TYPE.getName())) {
     currMethod = super.getClass().getMethod(methodName,
       new Class[] { Long.TYPE });
     currMethod.invoke(this,
       new Object[] { new Long(strValue.trim()) });
    } else if (currFieldTypeName.equals(Timestamp.class.getName())) {
     currMethod = super.getClass().getMethod(methodName,
       new Class[] { Timestamp.class });
     currMethod.invoke(this, new Object[] { DataFormat
       .getDateTime(strValue.trim()) });
    } else if (currFieldTypeName.equals(Date.class.getName())) {
     currMethod = super.getClass().getMethod(methodName,
       new Class[] { Date.class });
     currMethod.invoke(this, new Object[] { DataFormat
       .parseDate(strValue.trim()) });
    } else if (currFieldTypeName.equals(String.class.getName())) {
     currMethod = super.getClass().getMethod(methodName,
       new Class[] { String.class });
     currMethod.invoke(this, new Object[] { strValue.trim() });
    } else {
     this.logger.error("方法[" + methodName
       + "]的参数为未知的数据类型,无法进行转换");
    }
   }

  } catch (Exception e) {
   this.logger.error(e.getMessage(), e);
   e.printStackTrace();
   throw new SystemException("从ServletRequest中获得dataentity错误", e);
  }
 }

推荐阅读:
  1. DataTable 转实体
  2. 使用python怎么将列表转换为集合

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

将集合转换为实体数据 集合转换

上一篇:Rails测试《十一》添加邮件发送程序及测试邮件发送程序

下一篇:golang中Any类型使用及空接口中类型查询

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》