java中使用反射将javaBean 跟 map 互转

1.bean转map

/**
     * bean转map
     * @param model
     * @return
     * @throws Exception
     */
    public static Map<String, Object> beanToMap(Object model) throws Exception{
        Map<String, Object> result = new HashMap<>();
        // 获取实体类的所有属性,返回Field数组
        Field[] field = model.getClass().getDeclaredFields();
        // 遍历所有属性
        for (int j = 0; j < field.length; j++) {
            // 获取属性的名字
            String key = field[j].getName();
            // 将属性的首字符大写,方便构造get,set方法
            String name = key.substring(0, 1).toUpperCase() + key.substring(1);
            // 获取属性的类型
            String type = field[j].getGenericType().toString();
            // 如果type是类类型,则前面包含"class ",后面跟类名
            log.info("属性为--"+type);
            if (type.equals("class java.lang.String")) {
                Method m = model.getClass().getMethod("get" + name);
                // 调用getter方法获取属性值
                String value = (String) m.invoke(model);
                if (value != null) {
                    result.put(key,value);//往map赋值
                }
            }
            if (type.equals("class java.lang.Integer")) {
                Method m = model.getClass().getMethod("get" + name);
                Integer value = (Integer) m.invoke(model);
                if (value != null) {
                    result.put(key,value);//往map赋值
                }
            }
            if (type.equals("class java.lang.Short")) {
                Method m = model.getClass().getMethod("get" + name);
                Short value = (Short) m.invoke(model);
                System.out.println("数据类型为:Short");
                if (value != null) {
                    result.put(key,value);//往map赋值
                }
            }
            if (type.equals("class java.lang.Double")) {
                Method m = model.getClass().getMethod("get" + name);
                Double value = (Double) m.invoke(model);
                if (value != null) {
                    result.put(key,value);//往map赋值
                }
            }
            if (type.equals("class java.lang.Boolean")) {
                Method m = model.getClass().getMethod("get" + name);
                Boolean value = (Boolean) m.invoke(model);
                if (value != null) {
                    result.put(key,value);//往map赋值
                }
            }
            if (type.equals("class java.util.Date")) {
                Method m = model.getClass().getMethod("get" + name);
                Date value = (Date) m.invoke(model);
                if (value != null) {
                    //时间转str
                   String val= DateUtil.getDateString(value);
                    result.put(key,val);//往map赋值
                }
            }
        }
        return result;
    }

2.map转bean

/**
     * Map 转bean
     * @param model
     * @param json
     * @return
     * @throws Exception
     */
    public static Object mapToBean(Object model, Map<String,Object> json) throws Exception{
        // 获取实体类的所有属性,返回Field数组
        Field[] field = model.getClass().getDeclaredFields();
        // 遍历所有属性
        for (int j = 0; j < field.length; j++) {
            // 获取属性的名字
            String name = field[j].getName();
            if(!json.containsKey(name)){
                continue;
            }
            if((model.getClass().getName().toLowerCase() + "Id").equals(field)){
                continue;
            }
            String originName = name;
            // 将属性的首字符大写,方便构造get,set方法
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
            // 获取属性的类型
            String type = field[j].getGenericType().toString();
            if (type.equals("class java.lang.String")) {
                Method m = model.getClass().getMethod("set" + name, String.class);
                // 调用setter方法赋值属性值
                m.invoke(model, json.get(originName).toString());
            }
            if (type.equals("class java.lang.Integer")) {
                Method m = model.getClass().getMethod("set" + name, Integer.class);
                if (StringUtils.isNotEmpty(json.get(originName).toString())){
                    m.invoke(model, Integer.parseInt(json.get(originName).toString()));
                }
            }
            if (type.equals("class java.lang.Short")) {
                Method m = model.getClass().getMethod("set" + name, Short.class);
                if (StringUtils.isNotEmpty(json.get(originName).toString())){
                    m.invoke(model, Short.parseShort(json.get(originName).toString()));
                }

            }
            if (type.equals("class java.lang.Double")) {
                Method m = model.getClass().getMethod("set" + name, Double.class);
                if (StringUtils.isNotEmpty(json.get(originName).toString())){
                    m.invoke(model, Double.parseDouble(json.get(originName).toString()));
                }

            }
            if (type.equals("class java.math.BigDecimal")) {
                Method m = model.getClass().getMethod("set" + name, BigDecimal.class);
                if (StringUtils.isNotEmpty(json.get(originName).toString())){
                    m.invoke(model, new BigDecimal(json.get(originName).toString().replaceAll(",", "")).setScale(4,BigDecimal.ROUND_HALF_UP));
                }
            }
            if (type.equals("class java.lang.Boolean")) {
                Method m = model.getClass().getMethod("set" + name, Boolean.class);
                m.invoke(model, Boolean.parseBoolean(json.get(originName).toString()));
            }
            if (type.equals("class java.util.Date")) {
                Method m = model.getClass().getMethod("set" + name, Date.class);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                if (StringUtils.isNotEmpty(json.get(originName).toString())&& !"null".equals(json.get(originName).toString())){
                    m.invoke(model, simpleDateFormat.parse(json.get(originName).toString().length() <= 10?json.get(originName).toString() + " 00:00:00" :json.get(originName).toString()));
                }
            }
        }

        return model;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值