通用转换-借花献佛
public class ConverseUtil {
/**
* vo 转换为Do 工具类
*
* @param voEntity
* @param doClass
* @return
*/
public static <Do> Do voToDo(Object voEntity, Class<Do> doClass) {
// 判断dto是否为空!
if (voEntity == null) {
return null;
}
// 判断DoClass 是否为空
if (doClass == null) {
return null;
}
try {
Do newInstance = doClass.newInstance();
BeanUtils.copyProperties(voEntity, newInstance);
// Dto转换Do
return newInstance;
} catch (Exception e) {
return null;
}
}
/**
* do 转换为vo 工具类
*
* @param voClass
* @param doEntity
* @return
*/
public static <Dto> Dto doToVo(Object doEntity, Class<Dto> voClass) {
// 判断dto是否为空!
if (doEntity == null) {
return null;
}
// 判断DoClass 是否为空
if (voClass == null) {
return null;
}
try {
Dto newInstance = voClass.newInstance();
BeanUtils.copyProperties(doEntity, newInstance);
// Dto转换Do
return newInstance;
} catch (Exception e) {
return null;
}
}
}