将集合转换为实体数据

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);
  }
 }

你可能感兴趣的:(将集合转换为实体数据)