java 常用Bean 转换工具类

package com.hnf.framework.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: xxx
 * @Date: 2019/1/14 16:21
 * @Description: bean工具类
 */
public class MyBeanUtils {

    /**
     * 对象拷贝
     * @param source 源
     * @param target 目标
     */
    public static void copyProperties(Object source, Object target){
        if(source == null){return;}
        BeanUtils.copyProperties(source, target);
    }

    /**
     * List 对象拷贝
     * @param list 源
     * @param <T> 目标
     * @return 目标
     */
    public static <T, E> List copyList(List<T> list, Class<E> clazz) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList();
        }
        return JSON.parseArray(JSON.toJSONString(list), clazz);
    }

    /**
     * MAP拷贝
     * @param map 源
     * @return 目标
     */
    public static Map<String, Object> copyMap(Map map) {
        if(CollectionUtils.isEmpty(map)){
            return new HashMap<>();
        }
        return JSON.parseObject(JSON.toJSONString(map));
    }

    /**
     * json 转 Map
     * @param json
     * @return
     */
    public static Map<String, Object> jsonToMap(String json) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(json, Map.class);
        } catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }
}

  

posted @ 2019-05-28 16:14  `初一`  阅读(1676)  评论(0编辑  收藏  举报