java的反射
对应字段model
protected HashMap<String,HashMap> getModel(){ HashMap<String, HashMap> model = new HashMap<String, HashMap>(); HashMap<String, String > entry= new HashMap<String, String>();
model.put("entry",entry)
)
}
交换
protected void getValueMapByExample(HashMap<String,String> model,BaseObject baseObject, HashMap objInfo) { Class<?> classType = baseObject.getClass(); //获得该对象的类型类 Field fields[] = classType.getDeclaredFields(); //获取声明的参数名集合 Set entries = model.entrySet(); //获取模板的集合 Iterator iter = entries.iterator(); //遍历模板 try { for(String key:model.keySet()){ String value=model.get(key); //获取模板的值 for (int i = 0; i < fields.length; i++) { Field field = fields[i]; String fieldName = field.getName(); //获取参数方法的名字 if (fieldName.toLowerCase().equals("serialVersionUID".toLowerCase()) || fieldName.toLowerCase().contains("$SWITCH_TABLE".toLowerCase())) continue; //如果方法名是这些没有用的,就退出本次循环 if (!StringUtil.isNullorEmpty(objInfo.get(key)) && value.compareTo(fieldName) == 0) { //如果objInfo的值不等空 并且 参数名等于 String firstLetter = fieldName.substring(0, 1).toUpperCase(); //第一个字母变大写 String setMethodName = "set" + firstLetter + fieldName.substring(1); //set的方法 Method setMethod = classType.getMethod(setMethodName, new Class[] {field.getType()}); //获取方法 if (field.getType()== String.class){ setMethod.invoke(baseObject,objInfo.get(key)); //给baseObject赋值 }else if(field.getType()==float.class){ setMethod.invoke(baseObject,Float.parseFloat(objInfo.get(key)+"")); //上同 }else if(field.getType()==Integer.class){ setMethod.invoke(baseObject,Integer.parseInt(objInfo.get(key)+"")); }else if(field.getType()==double.class){ setMethod.invoke(baseObject,Double.parseDouble(objInfo.get(key)+"")); } } } } } catch (Exception e) { e.printStackTrace(); } }