java 实体类和Map之间互转

(1)Map转化为实体类

/***
	 * convert map to object ,see setObjectValue(obj, map)
	 * @param map : key是对象的成员变量,其value就是成员变量的值
	 * @param clazz
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 */
	public static Object convertMap2Obj(Map<String, Object> map,Class clazz) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException, IllegalArgumentException{
		if(ValueWidget.isNullOrEmpty(map)){
			return null;
		}
		Object obj=clazz.newInstance();
		setObjectValue(obj, map);
		/*for(Iterator it=map.entrySet().iterator();it.hasNext();){
			Map.Entry<String, Object> entry=(Map.Entry<String, Object>)it.next();
			String key=entry.getKey();
			Object val=entry.getValue();
		}*/
		return obj;
	}
/***
	 * 利用反射设置对象的属性值. 注意:属性可以没有setter 方法.
	 * 
	 * @param obj
	 * @param params
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static void setObjectValue(Object obj, Map<String, Object> params)
			throws SecurityException,
			IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
		if(ValueWidget.isNullOrEmpty(obj)){
			return;
		}
		if (ValueWidget.isNullOrEmpty(params)) {
			return;
		}
		Class<?> clazz = obj.getClass();
		for (Iterator it = params.entrySet().iterator(); it.hasNext();) {
			Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
					.next();
			String key = entry.getKey();
			Object propertyValue = entry.getValue();
			if (ValueWidget.isNullOrEmpty(propertyValue)) {
				continue;
			}
			Field name = getSpecifiedField(clazz, key);
			if (name != null) {
				name.setAccessible(true);
				name.set(obj, propertyValue);
			}
		}

	}

(2)把实体类转化为Map

    /***
     * convert entity to Map
     * @param obj
     * @param excludeProperties
     * @param excludeZero : 是否过滤zero
     * @return
     * @throws SecurityException
     * @throws NoSuchFieldException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static Map convertObj2Map(Object obj, String[] excludeProperties, boolean excludeZero) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Map map = new HashMap();
        List<Field> fieldsList = ReflectHWUtils.getAllFieldList(obj.getClass());
        for (int i = 0; i < fieldsList.size(); i++) {
            Field f = fieldsList.get(i);

            if (SystemHWUtil.isContains(excludeProperties, f.getName())) {
                continue;
            }
            Object propertyValue = ReflectHWUtils.getObjectValue(obj, f);
            if (excludeZero) {
                if (propertyValue instanceof Integer) {
                    int ii = (Integer) propertyValue;
                    if (ii == 0) {
                        continue;
                    }
                } else if (propertyValue instanceof Long) {
                    int ii = ((Long) propertyValue).intValue();
                    if (ii == 0) {
                        continue;
                    }
                }
            }
            if (!ValueWidget.isNullOrEmpty(propertyValue)) {
                map.put(f.getName(), propertyValue);
            }
        }
        return map;
    }

调用:

Map condition = null;
            try {
                condition = ReflectHWUtils.convertObj2Map(roleLevel, columns, true);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

jar包见附件 中的/Users/whuanghkl/work/project/io0007/src/main/java/com/common/util/ReflectHWUtils.java

猜你喜欢

转载自hw1287789687.iteye.com/blog/2315931