package org.jeecg.modules.utils; import ma.glasnost.orika.MapperFacade; import ma.glasnost.orika.MapperFactory; import ma.glasnost.orika.converter.builtin.PassThroughConverter; import ma.glasnost.orika.impl.DefaultMapperFactory; import java.util.List; import java.util.Set; /** * 复制对象属性的工具类。 * Created by sanerhe on 2014/12/28. */ public class BeanMapper { /** * 实例. */ private static MapperFacade mapper; static { // 如果src中属性为null,就不复制到dest MapperFactory mapperFactory = new DefaultMapperFactory.Builder().mapNulls(false).build(); // 如果属性是Object,就只复制引用,不复制值,可以避免循环复制 mapperFactory.getConverterFactory().registerConverter(new PassThroughConverter(Object.class)); mapper = mapperFactory.getMapperFacade(); } /** * 把source中的值复制到destination中. */ public static void copy(Object source, Object destination) { mapper.map(source, destination); } /** * 把source中的值复制到destinationCls中. */ public static List copyList(List source, Class destinationCls) { return mapper.mapAsList(source, destinationCls); } /** * 把source中的值复制到destination中. */ public static List copyCollection(List source, List destination, Class destinationCls) { mapper.mapAsCollection(source, destination, destinationCls); return destination; } /** * 把source中的值复制到destination中. */ public static Set copyCollection(Set source, Class destinationCls) { Set destination = mapper.mapAsSet(source, destinationCls); return destination; } }