-
/**
-
* 数据对象转换工具
-
*/
-
public
class ConvertUtils {
-
-
private
static Logger logger = LoggerFactory.getLogger(ConvertUtils.class);
-
-
/**
-
* 转化出新对象
-
* @param source 源对象
-
* @param target 目标对象
-
* @param <T>
-
* @return
-
*/
-
public
static <T>
T sourceToTarget(Object source, Class<T> target){
-
if(source ==
null){
-
return
null;
-
}
-
T targetObject =
null;
-
try {
-
targetObject = target.newInstance();
-
BeanUtils.copyProperties(source, targetObject);
-
}
catch (Exception e) {
-
logger.error(
"convert error ", e);
-
}
-
return targetObject;
-
}
-
-
/**
-
* 转化出新对象集合
-
* @param sourceList 源对象集合
-
* @param target 目标对象
-
* @param <T> 新对象集合
-
* @return
-
*/
-
public
static <T>
List<T> sourceToTarget(Collection<?> sourceList, Class<T> target){
-
if(sourceList ==
null){
-
return
null;
-
}
-
List targetList =
new ArrayList<>(sourceList.size());
-
try {
-
for(Object source : sourceList){
-
T targetObject = target.newInstance();
-
BeanUtils.copyProperties(source, targetObject);
-
targetList.add(targetObject);
-
}
-
}
catch (Exception e){
-
logger.error(
"convert error ", e);
-
}
-
-
return targetList;
-
}
-
}
转载:https://blog.csdn.net/qq_42217906/article/details/104415317
查看评论