工具类
public class SmartBeanUtil { /** * 复制bean的属性 * @param source 源 要复制的对象 * @param target 目标 复制到此对象 */ public static void copyProperties(Object source, Object target) { BeanUtils.copyProperties(source, target); } /** * 复制对象 * @param source 源 要复制的对象 * @param target 目标 复制到此对象 */ public static <T> T copy(Object source, Class<T> target) { if (source == null || target == null) { return null; } try { T newInstance = target.newInstance(); BeanUtils.copyProperties(source, newInstance); return newInstance; } catch (Exception e) { throw new RuntimeException(e); } } /** * 复制list */ public static <T, K> List<K> copyList(List<T> source, Class<K> target) { if (null == source || source.isEmpty()) { return Collections.emptyList(); } return source.stream().map(e -> copy(e, target)).collect(Collectors.toList()); } }
例子:SinForm转Form
Form queryForm = SmartBeanUtil.copy(sinForm,Form .class);