- 代码示例
代码语言:javascript
复制package com.simple.util.bean;
import org.springframework.cglib.beans.BeanMap;
import java.util.HashMap;
import java.util.Map;
/**
* @program: simple_tools
* @description: BeanMapUtils
* @author: ChenWenLong
* @create: 2019-12-29 14:14
**/
public class BeanMapUtils {
/**
* 将对象属性转化为map结合
*/
public static <T> Map<String, Object> beanToMap(T bean) {
Map<String, Object> map = new HashMap<>();
if (bean != null) {
BeanMap beanMap = BeanMap.create(bean);
for (Object key : beanMap.keySet()) {
map.put(key "", beanMap.get(key));
}
}
return map;
}
/**
* 将map集合中的数据转化为指定对象的同名属性中
*/
public static <T> T mapToBean(Map<String, Object> map,Class<T> clazz) throws Exception {
T bean = clazz.newInstance();
BeanMap beanMap = BeanMap.create(bean);
beanMap.putAll(map);
return bean;
}
}