package com.yqzx.common.util; import lombok.extern.slf4j.Slf4j; import org.apache.commons.beanutils.BeanMap; import org.apache.commons.beanutils.BeanUtils; import java.util.Map; /** * @Description 类转换 * @Author wh * @Date 2023/12/9 16:45 */ @Slf4j public class BeanUtil { /** * bean 的转换 * @param bean * @param map * @param * @return */ public static Map beanToMap(T bean, Map map) { if (bean == null) { return map; } try { Map maps = BeanUtils.describe(bean); for (Map.Entry mp: maps.entrySet()) { map.put(mp.getKey(), mp.getValue()); } } catch (Exception e) { log.error(e.getMessage()); } return map; } public static T mapToBean(Map map, Class clazz){ T bean = null; try { bean = clazz.newInstance(); BeanUtils.populate(bean, map); } catch (Exception e) { log.error(e.getMessage()); } return bean; } public static Map objToMap(Object obj) { if (obj == null) { return new BeanMap(); } return new BeanMap(obj); } }