Tommy

Tommy

写代码是热爱,但也是生活 !
github

Entity class to Vo returns the generic method CopyList to the frontend.

When writing interfaces, you need to return a Vo object each time, so I encapsulated a copy method#

public class BeanCopyUtils {

    private BeanCopyUtils() {
    }

    public static <V> V copyBean(Object source,Class<V> clazz) {
        //Create the target object
        V result = null;
        try {
            result = clazz.newInstance();
            //Implement property copy
            BeanUtils.copyProperties(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //Return the result
        return result;
    }
    public static <O,V> List<V> copyBeanList(List<O> list, Class<V> clazz){
        return list.stream()
                .map(o -> copyBean(o, clazz))
                .collect(Collectors.toList());
    }
}

Usage#

//Bean copy
        List<HotArticleVo> hotArticleVos = 
                BeanCopyUtils.copyBeanList(articles, HotArticleVo.class);

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.