• Map与实体之间转换


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    package com.thunisoft.maybee.engine.utils;
     
    import java.lang.reflect.Field;
    import java.lang.reflect.Modifier;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
     
    public class MapObjUtil {
     
        /**
         * 实体对象转成Map
         *
         * @param obj 实体对象
         * @return
         */
        public static Map<String, Object> object2Map(Object obj) {
            Map<String, Object> map = new HashMap<String, Object>();
            if (obj == null) {
                return map;
            }
            Class clazz = obj.getClass();
            Field[] fields = clazz.getDeclaredFields();
            try {
                for (Field field : fields) {
                    field.setAccessible(true);
                    map.put(field.getName(), field.get(obj));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return map;
        }
     
        /**
         * Map转成实体对象
         *
         * @param map   map实体对象包含属性
         * @param clazz 实体对象类型
         * @return
         */
        public static <T> T map2Object(Map<String, Object> map, Class<T> clazz) {
            if (map == null) {
                return null;
            }
            T obj = null;
            try {
                obj = clazz.newInstance();
     
                Field[] fields = obj.getClass().getDeclaredFields();
                for (Field field : fields) {
                    int mod = field.getModifiers();
                    if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                        continue;
                    }
                    field.setAccessible(true);
                    String filedTypeName = field.getType().getName();
                    if (filedTypeName.equalsIgnoreCase("java.util.date")) {
                        String datetimestamp = String.valueOf(map.get(field.getName()));
                        if (datetimestamp.equalsIgnoreCase("null")) {
                            field.set(obj, null);
                        } else {
                            field.set(obj, new Date(Long.parseLong(datetimestamp)));
                        }
                    } else {
                        field.set(obj, map.get(field.getName()));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return obj;
        }
    }
    原文地址:https://www.cnblogs.com/hfultrastrong/p/9075947.html
  • 相关阅读:
    使用EF进行简单的增删改查
    观察者模式(委托事件的小应用)
    lambda表达式和表达式树
    socket知识总结
    xml读写Demo
    winfrom DataGridView Demo
    6月26号.NET面试题(程序题部分)只要做懂这3道题肯定能脱离菜鸟称号!
    多线程与异步编程知识简单总结
    15年6月14号面试中没有回答出来的问题
    2020.5.15记一次阿里电话面试经历
  • 原文地址:https://www.cnblogs.com/jpfss/p/11573117.html
Copyright © 2020-2023  润新知