一个关于类(Object)转换问题的类: 可以将一个类或对象的属性名和属性值分离出来,不使用XML技术,有点像“类的反向实例化”(自己取的名字^_^) 包含了一些常用方法,必要的大家可以自行增加。 package engin; import java.lang.reflect.Field; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import entity.Song; public class ObjectUtils { /** * 将指定对象属性名称和属性值转化为Map键值对 * * @param obj * @return */ public static HashMap objectToMap(Object obj) throws Exception { if (obj == null) { throw new Exception("对象为空"); } Class clazz = obj.getClass(); HashMap map = new HashMap(); getClass(clazz,map,obj); HashMap newMap = convertHashMap(map); return newMap; } /** * 带入一个对象,一个对象中的属性名称,取得该属性的值 * @param obj * @param strProperty * @return * @throws Exception */ public static String getPropertyValueFormObject(Object obj, String strProperty) throws Exception { HashMap newMap = ObjectUtils.objectToMap(obj); if (newMap == null) { return ""; } else { Object objReturn = newMap.get(strProperty); if (objReturn==null) { return ""; } else { return objReturn.toString(); } } } /** * */ private static void getClass(Class clazz,HashMap map,Object obj) throws Exception{ if(clazz.getSimpleName().equals("Object")){ return; } Field[] fields = clazz.getDeclaredFields(); if (fields == null || fields.length <= 0) { throw new Exception("当前对象中没有任何属性值"); } for(int i=0;i<fields.length;i++){ fields[i].setAccessible(true); String name=fields[i].getName(); Object value=fields[i].get(obj); map.put(name,value); } Class superClzz=clazz.getSuperclass(); getClass(superClzz,map,obj); } /** * * @param map * @return * @throws Exception */ private static HashMap convertHashMap(HashMap map) throws Exception { HashMap newMap = new HashMap(); Set keys = map.keySet(); Iterator it = keys.iterator(); while (it.hasNext()) { Object key = it.next(); convertToString(map.get(key), newMap, key); } return newMap; } /** * * @param value * @param newMap * @param key */ private static void convertToString(Object value, HashMap newMap, Object key) { if (value != null) { Class clazz = value.getClass(); if (isBaseType(clazz)) { newMap.put(key, value.toString()); } else if (clazz == String.class) { newMap.put(key, value.toString()); } else if (clazz == Date.class) { Date date = (Date) value; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); newMap.put(key, sdf.format(date)); } else if (clazz == Timestamp.class) { Timestamp timestamp = (Timestamp) value; long times = timestamp.getTime(); Date date = new Date(times); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); newMap.put(key, sdf.format(date)); } else if (clazz == java.sql.Date.class) { java.sql.Date sqlDate = (java.sql.Date) value; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); newMap.put(key, sdf.format(sqlDate)); } else { newMap.put(key, value); } } else { newMap.put(key, value); } } /** * * @param clazz * @return */ private static boolean isBaseType(Class clazz) { if (clazz == Integer.class) { return true; } if (clazz == Long.class) { return true; } if (clazz == Double.class) { return true; } if (clazz == Byte.class) { return true; } if (clazz == Float.class) { return true; } if (clazz == Short.class) { return true; } if (clazz == Boolean.class) { return true; } return false; } public static Iterator getObjectElements(Object obj) throws Exception{ HashMap map = ObjectUtils.objectToMap(obj); Set entrys = map.entrySet(); return entrys.iterator(); } /** * 测试 * * @param args */ public static void main(String args[]) throws Exception { Song song = new Song(); song.setId(1); Object obj=song; Iterator it = getObjectElements(obj); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); System.out.println(entry.getKey() + ":" + entry.getValue()); } } }