对象转map工具类

  • Post author:
  • Post category:其他




对象转map工具类

package com.link.base.base.accnt.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dujiangzhou
 */
public class MapUtils {

    private static final Logger logger = LogManager.getLogger(MapUtils.class);

    /**
     * 将对象转成Map对象
     *
     * @param object
     */
    public static Map<String, Object> objectToMap(Object object) {
        Map<String, Object> map = new HashMap<>();
        if (object == null) {
            return map;
        }
        for (Field field : object.getClass().getDeclaredFields()) {
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(object);
                map.put(field.getName(), o);
                field.setAccessible(flag);
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
        return map;
    }

    /**
     * 将对象转成Map对象 去除对象值为空的映射
     *
     * @param object
     */
    public static Map<String, Object> objectToMap(Object object, Boolean removeNull, Boolean isBase) {

        Map<String, Object> map = new HashMap<>();
        //是否移除空值map
        if (removeNull == null) {
            removeNull = false;
        }
        if (object == null) {
            return map;
        }
        //是否跳过基础字段
        List<String> baseColumn = Arrays.asList("id", "created", "createdBy", "lastUpdated",
                "lastUpdatedBy", "orgId", "postnId", "corpid");
        if (isBase == null) {
            isBase = false;
        }

        for (Field field : object.getClass().getDeclaredFields()) {
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(object);
                if (removeNull && null == o) {
                    continue;
                }
                if (!isBase && baseColumn.contains(field.getName())) {
                    continue;
                }
                map.put(field.getName(), o);
                field.setAccessible(flag);
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
        return map;
    }


    /**
     * 将对象转成Map对象 指定字段对象值的映射
     *
     * @param object     转换的对象
     * @param objectList 转换的属性
     */
    public static Map<String, Object> objectToMap(Object object, List<String> objectList) {
        Map<String, Object> map = new HashMap<>(objectList.size() + 1);
        if (CollectionUtils.isEmpty(objectList)) {
            return map;
        }
        if (object == null) {
            return map;
        }
        for (Field field : object.getClass().getDeclaredFields()) {
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(object);
                if (!objectList.contains(field.getName())) {
                    continue;
                }
                map.put(field.getName(), o);
                field.setAccessible(flag);
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
        return map;
    }

 /**
     * 将对Map转为指定对象
     */
    public static Object mapToObject(Map<String, Object> map, Object object) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        if (object == null) {
            return null;
        }
        for (String key : map.keySet()) {
            for (Field field : object.getClass().getDeclaredFields()) {
                try {
                    boolean flag = field.isAccessible();
                    field.setAccessible(true);
                    if (!key.equals(field.getName())) {
                        continue;
                    }
                    //重新赋值
                    field.set(object, map.get(key));
                    field.setAccessible(flag);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return object;
    }

}



版权声明:本文为qq_44943313原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。