Bean转Map Map转Bean
package com.sino.framework.common.utils;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.collections.CollectionUtils;
import com.sino.framework.data.domain.PageBO;
import com.sino.framework.util.SimpleBeanUtils;
public class DAOUtils {
static {
ConvertUtils.register(new DateConverter(null), java.util.Date.class);
}
public static <T> List<T> populateList(Class<T> type, List<Map<String, Object>> listMapSource) throws Exception {
if (listMapSource==null) {
return null;
}
if (listMapSource.isEmpty()) {
return new ArrayList<>();
}
List<T> result = new ArrayList<>(listMapSource.size());
for (Map<String, Object> m : listMapSource) {
T u = type.newInstance();
BeanUtils.populate(u, m);
result.add(u);
}
return result;
}
public static <T> T populate(Class<T> type, Map<String, Object> map) throws Exception {
T result = null;
if (map != null) {
result = type.newInstance();
BeanUtils.populate(result, map);
}
return result;
}
@SuppressWarnings("rawtypes")
public static <T> List<T> cloneList(Class<T> type, List list) throws Exception {
List<T> listResult = null;
if (CollectionUtils.isNotEmpty(list)) {
listResult = new ArrayList<>();
for (Object u : list) {
T modelBO = type.newInstance();
BeanUtils.copyProperties(modelBO, u);
listResult.add(modelBO);
}
}
return listResult;
}
@SuppressWarnings("rawtypes")
public static <T> List<T> cloneList(Class<T> type, List list, IObjectCallBack<T> cb) throws Exception {
List<T> listResult = null;
if (CollectionUtils.isNotEmpty(list)) {
listResult = new ArrayList<>();
for (Object u : list) {
T modelBO = type.newInstance();
BeanUtils.copyProperties(modelBO, u);
if (cb != null) {
cb.callback(modelBO);
}
listResult.add(modelBO);
}
}
return listResult;
}
@SuppressWarnings("rawtypes")
public static <T> PageBO<T> clonePage(Class<T> type, PageBO bo) throws Exception {
List<T> listResult = null;
if (bo.getContent() != null) {
listResult = new ArrayList<>();
for (Object u : bo.getContent()) {
T modelBO = type.newInstance();
BeanUtils.copyProperties(modelBO, u);
listResult.add(modelBO);
}
}
return new PageBO<T>(listResult, bo.getTotal());
}
@SuppressWarnings("rawtypes")
public static <T> PageBO<T> clonePage(Class<T> type, PageBO bo, IObjectCallBack<T> cb) throws Exception {
List<T> listResult = null;
if (bo.getContent() != null) {
listResult = new ArrayList<>();
for (Object u : bo.getContent()) {
T modelBO = type.newInstance();
BeanUtils.copyProperties(modelBO, u);
if (cb != null) {
cb.callback(modelBO);
}
listResult.add(modelBO);
}
}
return new PageBO<T>(listResult, bo.getTotal());
}
public static <T> T cloneBean(Class<T> type, Object bo) throws Exception {
T result = null;
if (bo != null) {
result = type.newInstance();
BeanUtils.copyProperties(result, bo);
}
return result;
}
public static <T> T cloneBean(Class<T> type, Object bo, IObjectCallBack<T> cb) throws Exception {
T result = null;
if (bo != null) {
result = type.newInstance();
BeanUtils.copyProperties(result, bo);
if (cb != null) {
cb.callback(result);
}
}
return result;
}
public static Map<String, Object> bean2map(Object obj) throws Exception {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (!key.equals("class")) {
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
} catch (Exception e) {
throw new Exception(e);
}
return map;
}
public static <T> T map2bean(Class<T> type, Map<String, Object> map) throws Exception {
T result = null;
if (map != null) {
result = type.newInstance();
SimpleBeanUtils.populate(result, map);
}
return result;
}
}