在项目中使用pojo和vo(返回给前端或第三方接口的视图),会经常遇到pojo转vo的问题。之前总是以set方式去装配,但是遇到多个参数去set就会很麻烦。然后本人结合网上资料,使用CommonBeanUtils.copyProperties去对单个pojo转vo实现简化。
ps.pojo字段与vo字段形式、类型保持一致
我这个人真的是特别懒的人,能少些代码尽量少写代码;
包括下面查出来的pojo集合不做业务处理,直接转化成vo集合返回,本人也懒得写。。。。
1 2 3 4 5 6
| List<RespSelectedActivitiesVo> voList = new ArrayList<>(); promotionAreaList.forEach(promotionArea -> { RespSelectedActivitiesVo vo = new RespSelectedActivitiesVo(); CommonBeanUtils.copyProperties(promotionArea, vo); voList.add(vo); });
|
所以我又写了一个集合转化为vo的方法CommonBeanUtils.copyListProperties。
所以以上代码简化为一行搞定。
1
| return CommonBeanUtils.copyListProperties(promotionAreaList, RespSelectedActivitiesVo.class);
|
源代码分割线——————————————————————————————————————————
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.FatalBeanException; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils;
import javax.xml.datatype.XMLGregorianCalendar; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List;
@Slf4j public abstract class CommonBeanUtils extends org.springframework.beans.BeanUtils {
public static void copyProperties(Object source, Object target) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (value != null) { Method writeMethod = targetPd.getWriteMethod(); Type targetParameterType = writeMethod.getGenericParameterTypes()[0]; if (!(targetParameterType.equals(XMLGregorianCalendar.class))) { if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } } catch (Throwable ex) { log.error(ex.getMessage()); throw new FatalBeanException("Could not copy properties from source to target", ex); } } } } }
public static <T> List<T> copyListProperties(List<? extends Object> sources, final Class<T> voClass) { Assert.isTrue(!CollectionUtils.isEmpty(sources), "Source must not be null"); List<T> targets = new ArrayList<>(); sources.forEach(source -> { try { T target = voClass.newInstance(); copyProperties(source, target); targets.add(target); } catch (InstantiationException | IllegalAccessException e) { log.error(e.getMessage()); } }); return targets; }
}
|