From 6d08eec100243e0be59a207d57ab4db7974e51e4 Mon Sep 17 00:00:00 2001 From: yangbin <yangbin@qq.com> Date: 星期一, 29 七月 2024 15:26:26 +0800 Subject: [PATCH] Merge branch 'master' of http://117.34.109.166:18448/r/mdc_yituo --- lxzn-module-mdc/src/main/java/org/jeecg/modules/utils/BeanMapper.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 59 insertions(+), 0 deletions(-) diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/utils/BeanMapper.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/utils/BeanMapper.java new file mode 100644 index 0000000..4282f60 --- /dev/null +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/utils/BeanMapper.java @@ -0,0 +1,59 @@ +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(); + } + + /** + * 鎶妔ource涓殑鍊煎鍒跺埌destination涓�. + */ + public static void copy(Object source, Object destination) { + mapper.map(source, destination); + } + + /** + * 鎶妔ource涓殑鍊煎鍒跺埌destinationCls涓�. + */ + public static <S, D> List<D> copyList(List<S> source, Class<D> destinationCls) { + return mapper.mapAsList(source, destinationCls); + } + + /** + * 鎶妔ource涓殑鍊煎鍒跺埌destination涓�. + */ + public static <S, D> List<D> copyCollection(List<S> source, List<D> destination, Class<D> destinationCls) { + mapper.mapAsCollection(source, destination, destinationCls); + return destination; + } + + /** + * 鎶妔ource涓殑鍊煎鍒跺埌destination涓�. + */ + public static <S, D> Set<D> copyCollection(Set<S> source, Class<D> destinationCls) { + Set<D> destination = mapper.mapAsSet(source, destinationCls); + return destination; + } +} -- Gitblit v1.9.3