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 entityToVo(Object source, Class 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 List entityToVoList(Collection sourceList, Class 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; } }