对象拷贝、实体转Map
2018-11-06 本文已影响0人
青丝如梦
实体转Map返回map对象:
import org.springframework.cglib.beans.BeanMap;
BeanMap.create(entityObj);
借助springframework对象拷贝
import org.springframework.beans.BeanUtils;
public class Utils {
/**
* 对象拷贝
*
* @param source 源对象
* @param target 希望拷贝成为的类型
* @param <T> 任意object
* @return 拷贝后的新对象
*/
public static <T> T copyProperties(Object source, Class<T> target) {
if (target == null) {
return null;
}
T targetInstance = null;
try {
targetInstance = target.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
if (source == null) {
return targetInstance;
}
BeanUtils.copyProperties(source, targetInstance);
return targetInstance;
}
}