pojo转vo更简便、更优化

在项目中使用pojo和vo(返回给前端或第三方接口的视图),会经常遇到pojo转vo的问题。之前总是以set方式去装配,但是遇到多个参数去set就会很麻烦。然后本人结合网上资料,使用CommonBeanUtils.copyProperties去对单个pojo转vo实现简化。

ps.pojo字段与vo字段形式、类型保持一致

我这个人真的是特别懒的人,能少些代码尽量少写代码;

包括下面查出来的pojo集合不做业务处理,直接转化成vo集合返回,本人也懒得写。。。。

List<RespSelectedActivitiesVo> voList = new ArrayList<>();
promotionAreaList.forEach(promotionArea -> {
	RespSelectedActivitiesVo vo = new RespSelectedActivitiesVo();
	CommonBeanUtils.copyProperties(promotionArea, vo);
	voList.add(vo);
});

所以我又写了一个集合转化为vo的方法CommonBeanUtils.copyListProperties。

所以以上代码简化为一行搞定。

return CommonBeanUtils.copyListProperties(promotionAreaList, RespSelectedActivitiesVo.class);

源代码分割线------------------------------------------------------------------------------------------------------------------------------


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;

/**
 * Bean对象转化
 *
 * @author 闵渭凯
 * <p>
 * 2018年8月20日
 */
@Slf4j
public abstract class CommonBeanUtils extends org.springframework.beans.BeanUtils {

	/**
	 * 对象赋值
	 *
	 * @param source 源对象
	 * @param target 目标对象
	 * @throws BeansException
	 */
	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);
						// 这里判断以下value是否为空 当然这里也能进行一些特殊要求的处理 例如绑定时格式转换等等
						if (value != null) {
							Method writeMethod = targetPd.getWriteMethod();
							Type targetParameterType = writeMethod.getGenericParameterTypes()[0];
							// 特殊类型不再执行copy XMLGregorianCalendar
							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);
					}
				}
			}
		}
	}

	/**
	 * 集合对象转化赋值
	 * @param sources 源集合对象
	 * @param voClass vo类型
	 * @param <T>
	 * @return
	 */
	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;
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_41187876/article/details/95071851