Entity class to Vo returns the generic method CopyList to the frontend.
117
AI Translation
This post is translated from Chinese into English through AI.View Original
AI-generated summary
In the given code, a utility class called BeanCopyUtils is created to copy objects. It provides methods to copy a single object and a list of objects. The copyBean method creates a new object of the specified class and copies the properties from the source object using BeanUtils.copyProperties. The copyBeanList method uses the copyBean method to copy each object in the list and returns a new list of copied objects. The utility class can be used by calling the copyBeanList method and passing the list of objects to be copied and the target class.
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()); }}