【java基本操作】java对象转换成map对象,map对象转换成java对象,不依赖框架,手写代码完成

【java基本操作】java对象转换成map对象,map对象转换成java对象,不依赖框架,手写代码完成


    /**
     * java实体转map
     *
     * @param obj
     * @return
     */
    public static Map javaBeanToMap(Object obj) {
        Map map = new HashMap<>(16);
        if (obj == null) {
            return map;
        }
        parseClassToMap(obj, obj.getClass(), map);
        return map;
    }

    private static void parseClassToMap(Object obj, Class aClass, Map map) {
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            try {
                map.put(field.getName(), field.get(obj));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                throw new RuntimeException("javaBeanToMap parse error");
            }
        }
        Class superclass = aClass.getSuperclass();
        if (superclass == null) {
            return;
        }
        parseClassToMap(obj, superclass, map);
    }

    /**
     * map转实体
     *
     * @param map
     * @param tClass
     * @param 
     * @return
     */
    public static  T mapToEntity(Map map, Class tClass) throws Exception {
        T entity = tClass.newInstance();
        for (Map.Entry entry : map.entrySet()) {
            String fieldName = entry.getKey();
            Object fieldValue = entry.getValue();
            Field field = tClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            Class fieldType = field.getType();
            if (fieldValue != null && !"".equals(fieldValue)) {
                Object convertedValue = convertValue(fieldValue, fieldType);
                field.set(entity, convertedValue);
            }
        }
        return entity;
    }

    /**
     * 转换为对应类型
     *
     * @param value
     * @param targetType
     * @return
     */
    private static Object convertValue(Object value, Class targetType) {
        if (targetType == String.class) {
            return value.toString();
        } else if (targetType == Integer.class || targetType == int.class) {
            return Integer.parseInt(value.toString());
        } else if (targetType == Long.class || targetType == long.class) {
            return Long.parseLong(value.toString());
        } else if (targetType == Double.class || targetType == double.class) {
            return Double.parseDouble(value.toString());
        } else if (targetType == Boolean.class || targetType == boolean.class) {
            return Boolean.parseBoolean(value.toString());
        } else if (targetType == Date.class) {
            return DateUtils.dateString2Date(value.toString());
        }
        return value;
    }


    /**
     * 设置指定列为null
     */
    public static void fieldNull(Object obj, String field) {
        Class aClass = obj.getClass();
        try {
            Field declaredField = aClass.getDeclaredField(field);
            declaredField.setAccessible(true);
            declaredField.set(obj, null);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            logger.warn("==> 置空指定列异常,获取属性:field:{} err:{}", field, e.getMessage());
        }
    }

你可能感兴趣的:(java,前端,网络)