1.spring 自带拷贝工具
LoginUser user = new LoginUser();
// 将data中字段相同的属性拷贝到user中BeanUtils.copyProperties(data , user);
2. 基于Orika的字节码拷贝工具
1) pom
<!-- 基于字节码的深拷贝工具类 --><dependency><groupId>ma.glasnost.orika</groupId><artifactId>orika-core</artifactId><version>1.5.4</version></dependency>
2)工具类 OrikaUtils
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.TypeFactory;import java.util.Collection;
import java.util.List;
import java.util.Set;/*** @Description Orika深拷贝工具类* @date 2021/9/30 16:03
*/
public class OrikaUtils {private OrikaUtils() {}//单例模式(懒汉式、线程安全)private static class Builder{//通过工厂和建造者创建MapperFacade实例private static final MapperFacade mapper = new DefaultMapperFactory.Builder().build().getMapperFacade();}public static MapperFacade getMapper(){return Builder.mapper;}/*** @param srcObj 被拷贝对象* @param dstClass 目标对象类型* @return 返回新的目标对象的一个深拷贝** */public static <S, D> D mapBean(S srcObj, Class<D> dstClass) {return getMapper().map(srcObj, dstClass);}/*** @param srcList 映射的Iterable* @param dstClass 目标对象类型* @return 返回新的目标对象的一个深拷贝对象list** */public static <S, D> List<D> mapList(Iterable<S> srcList, Class<D> dstClass) {return getMapper().mapAsList(srcList, dstClass);}/*** @param srcList 映射的Iterable* @param srcClass 被拷贝对象类型* @param dstClass 目标对象类型* @return 返回新的目标对象的一个深拷贝对象list** */public static <S, D> List<D> mapList(Iterable<S> srcList, Class<S> srcClass, Class<D> dstClass) {return getMapper().mapAsList(srcList, TypeFactory.valueOf(srcClass), TypeFactory.valueOf(dstClass));}/*** @param srcSet 映射的Iterable* @param srcClass 目标对象类型* @return 返回新的目标对象的一个深拷贝对象set** */public static <S , D> Set<D> mapSet(Iterable<S> srcSet , Class<D> srcClass ){return getMapper().mapAsSet(srcSet , srcClass);}/*** @param srcSet 映射的Iterable* @param srcClass 被拷贝对象类型* @param dstClass 目标对象类型* @return 返回新的目标对象的一个深拷贝对象set** */public static <S,D> Set<D> mapSet(Iterable<S> srcSet , Class<S> srcClass , Class<D> dstClass ){return getMapper().mapAsSet(srcSet , TypeFactory.valueOf(srcClass) , TypeFactory.valueOf(dstClass));}/*** @param destination 目标对象数组* @param source 映射的Iterable* @param destinationClass 目标对象类型* @return 返回新的目标对象的一个深拷贝数组对象** */public static <S, D> D[] mapArray(D[] destination, Iterable<S> source, Class<D> destinationClass){return getMapper().mapAsArray(destination , source , destinationClass);}/*** @param destination 目标对象数组* @param source 数组对象* @param destinationClass 目标对象类型* @return 返回新的目标对象的一个深拷贝数组对象** */public static <S, D> D[] mapArray(D[] destination, S[] source, Class<D> destinationClass){return getMapper().mapAsArray(destination , source , destinationClass);}/*** @param source 映射的Iterable* @param destination Collection及子类(set,queue,list)* @param destinationClass 目标对象类型* @return 对传入destination进行拷贝赋值** */public static <S, D> void mapCollection(Iterable<S> source, Collection<D> destination, Class<D> destinationClass){getMapper().mapAsCollection(source,destination,destinationClass);}/*** @param source 映射的Iterable* @param destination Collection及子类(set,queue,list)* @param sourceClass 被拷贝的类型* @param destinationClass 目标对象类型* @return 对传入destination进行拷贝赋值** */public static <S, D> void mapCollection(Iterable<S> source, Collection<D> destination, Class<S> sourceClass, Class<D> destinationClass){getMapper().mapAsCollection(source,destination, TypeFactory.valueOf(sourceClass) , TypeFactory.valueOf(destinationClass));}
}