JavaBeanUtils javaBean转map, 实体类转map,实体集合转List<Map>

复制代码
package com.application.util;

import com.alibaba.fastjson.JSON;
import org.springframework.util.ObjectUtils;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JavaBeanUtils {

    /**
     * 实体类 转 Map
     * @param bean 要转化的JavaBean 对象
     * @param flagNull true 去除Bean字段空值,  false Bean空字段,同样生成Map
     * @return 转化出来的  Map 对象
     * @throws IntrospectionException   如果分析类属性失败
     * @throws IllegalAccessError   如果实例化 JavaBean 失败
     * @throws InvocationTargetException 如果调用属性的 setter 方法失败
     */
    public static Map<String, Object> convertBeanToMap(Object bean, boolean flagNull) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
        Class type = bean.getClass();
        Map<String, Object> returnMap = new HashMap<>();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            if (!"class".equals(propertyName)) {
                Method readMethod = descriptor.getReadMethod();
                Object result = readMethod.invoke(bean, new Object[0]);
                if (flagNull) {
                    if (!ObjectUtils.isEmpty(result)) {
                        returnMap.put(propertyName, result);
                    }
                } else {
                    if (ObjectUtils.isEmpty(result)) {
                        returnMap.put(propertyName, "");
                    } else {
                        returnMap.put(propertyName, result);
                    }
                }
            }
        }
        return  returnMap;
    }

    /**
     * 实体类 转 Map
     * @param obj
     * @return
     */
    public static Map<String, Object> beanToMap(Object obj) {
        Map<String, Object> params = JSON.parseObject(JSON.toJSONString(obj), Map.class);
        return params;
    }

    /**
     * Map转实体类
     *
     * @return
     */
    public static <T> T mapToBean(Map<String, Object> params, Class<T> clazz) {
        T obj = JSON.parseObject(JSON.toJSONString(params), clazz);
        return obj;
    }
复制代码
/**
 * list<Bean>转List<Map>
 * @return
 */
public static <T>  List<Map<String,Object>>beanListToMapList(List<T> beanList) {
    List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
    for (T bean : beanList) {
    Map<String,Object>  map = beanToMap(bean);
        resultList.add(map);
    }
    return resultList;
}
复制代码
}
相关推荐
人工小情绪几秒前
Linux下离线安装timm
linux·运维·服务器
Trouvaille ~几秒前
【MySQL篇】表的操作:数据的容器
linux·数据库·mysql·oracle·xshell·ddl·表的操作
疯狂成瘾者6 分钟前
增强型大模型代理
python
小李云雾7 分钟前
FastAPI 后端开发:文件上传 + 表单提交
开发语言·python·lua·postman·fastapi
Legend NO248 分钟前
数据资产评估风险识别、分析与管控体系建设
大数据·人工智能·python
爱学习的小囧8 分钟前
vSphere 9.0 API 实操教程 —— 轻松检索 vGPU 与 DirectPath 配置文件
linux·运维·服务器·网络·数据库·esxi·vmware
CresCent_Charles11 分钟前
(已解决)踩坑记录:Windows 11安装pointops编译时报错
windows
llm大模型算法工程师weng13 分钟前
Python敏感词检测方案详解
开发语言·python·c#
鸿儒51714 分钟前
利用gdal进行RPC经纬度与像素坐标转换问题记录
linux·rpc·gdal
Ghost Face...16 分钟前
深入解析Loongson LSDC DRM驱动:从原理到实现
linux