} catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); }
return fieldPool; }
/** * 根据表单对象,为业务对象赋值 * @param map 表单对象 * @return */ public T mapping(Map<String, String[]> map){ Map<String, Object> fieldPool = this.fieldPool(map);
try { for (Entry<String, String[]> entrySet : map.entrySet()) { String[] keyArr = entrySet.getKey().split("\\."); int length = keyArr.length;
Object object = fieldPool.get(length==1?"root":keyArr[length-2]); Field f = object.getClass().getDeclaredField(keyArr[length-1]); f.setAccessible(true); String typeName = f.getType().getSimpleName();
if (typeName.equals("String")) f.set(object, entrySet.getValue()[0]); if (typeName.equals("int") || typeName.equals("Integer")) f.set(object, Integer.valueOf(entrySet.getValue()[0])); if (typeName.equals("long") || typeName.equals("Long")) f.set(object, Long.valueOf(entrySet.getValue()[0])); if(typeName.equals("Date")){ Date date = this.simpleDateFormat.parse(entrySet.getValue()[0]); f.set(object, date); }
for (int i = keyArr.length - 2; i >= 0; i--) { String keyParent = i != 0 ? keyArr[i - 1] : "root"; String key2 = keyArr[i];
Field f2 = objectParent.getClass().getDeclaredField(key2); f2.setAccessible(true); f2.set(objectParent, object); } }
} catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } catch (ParseException e) { throw new RuntimeException(e); }
return (T) fieldPool.get("root"); }
public static void main(String[] args) { Map<String, String[]> map = new HashMap<String, String[]>(); map.put("id", new String[]{"123456"}); map.put("tUser.name", new String[]{"a1"}); map.put("tUser.email", new String[]{"a1@ff.com"}); map.put("tUser.phone", new String[]{"123"}); map.put("tUser.password", new String[]{"111"}); map.put("tUser.registerTime", new String[]{"2010-09-06"}); map.put("tUsergroup.name", new String[]{"g1"}); map.put("tUsergroup.explain", new String[]{"aaaaaaaaaaaaaa"}); map.put("tUser.tUserDetails.surname", new String[]{"哈哈"}); map.put("tUser.tUserDetails.sex", new String[]{"1"});