欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java实体类转换

程序员文章站 2022-06-15 12:00:11
...

//实体类转换成另外一个实体类
public static T sourceToTarget(Object source, Class target) {
if (source == null) {
return null;
} else {
Object targetObject = null;

        try {
            targetObject = target.newInstance();
            BeanUtils.copyProperties(source, targetObject);
        } catch (Exception var4) {
            logger.error("convert error ", var4);
        }

        return targetObject;
    }
}

public static <T> List<T> sourceToTarget(Collection<?> sourceList, Class<T> target) {
    if (sourceList == null) {
        return null;
    } else {
        ArrayList targetList = new ArrayList(sourceList.size());

        try {
            Iterator var3 = sourceList.iterator();

            while(var3.hasNext()) {
                Object source = var3.next();
                T targetObject = target.newInstance();
                BeanUtils.copyProperties(source, targetObject);
                targetList.add(targetObject);
            }
        } catch (Exception var6) {
            logger.error("convert error ", var6);
        }

        return targetList;
    }
}
相关标签: java