51 lines
1.4 KiB
Java
51 lines
1.4 KiB
Java
package com.gunshi.project.xyt.util;
|
|
|
|
/**
|
|
* @author cxw
|
|
* @description: (描述这个类或者方法的作用)
|
|
* @classname ConvertUtil.java
|
|
* @create 2024-07-10, 星期三, 10:34:14
|
|
*/
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
public class ConvertUtil {
|
|
public static final Logger logger = LoggerFactory.getLogger(ConvertUtil.class);
|
|
|
|
public static <T> T entityToVo(Object source, Class<T> target) {
|
|
if (source == null) {
|
|
return null;
|
|
}
|
|
T targetObject = null;
|
|
try {
|
|
targetObject = target.newInstance();
|
|
BeanUtils.copyProperties(source, targetObject);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return targetObject;
|
|
}
|
|
|
|
public static <T> List<T> entityToVoList(Collection<?> sourceList, Class<T> target) {
|
|
if (sourceList == null) {
|
|
return null;
|
|
}
|
|
List<T> 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;
|
|
}
|
|
}
|