From 961073ec96bfbb920f9afd952052c20b3791eb4d Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期二, 15 二月 2022 10:45:04 +0800 Subject: [PATCH] update 优化 TreeBuildUtils 工具 使用反射自动获取顶级父id --- ruoyi-common/src/main/java/com/ruoyi/common/utils/BeanCopyUtils.java | 149 ++++++++++++++++++++++++++++++++++++------------- 1 files changed, 109 insertions(+), 40 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/BeanCopyUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/BeanCopyUtils.java index bea09f8..23ace00 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/BeanCopyUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/BeanCopyUtils.java @@ -1,55 +1,124 @@ package com.ruoyi.common.utils; -import cn.hutool.core.bean.copier.BeanCopier; -import cn.hutool.core.bean.copier.CopyOptions; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ReflectUtil; +import cn.hutool.extra.cglib.CglibUtil; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import java.util.List; -import java.util.stream.Collectors; +import java.util.Map; /** - * bean娣辨嫹璐濆伐鍏� + * bean娣辨嫹璐濆伐鍏�(鍩轰簬 cglib 鎬ц兘浼樺紓) + * <p> + * 閲嶇偣 cglib 涓嶆敮鎸� 鎷疯礉鍒伴摼寮忓璞� + * 渚嬪: 婧愬璞� 鎷疯礉鍒� 鐩爣(閾惧紡瀵硅薄) + * 璇峰尯鍒嗗ソ`娴呮嫹璐漙鍜宍娣辨嫹璐漙鍐嶅仛浣跨敤 * * @author Lion Li */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class BeanCopyUtils { - /** - * 鍗曞璞″熀浜巆lass鍒涘缓鎷疯礉 - * - * @param source 鏁版嵁鏉ユ簮瀹炰綋 - * @param copyOptions copy鏉′欢 - * @param desc 鎻忚堪瀵硅薄 杞崲鍚庣殑瀵硅薄 - * @return desc - */ - public static <T, V> V oneCopy(T source, CopyOptions copyOptions, Class<V> desc) { - V v = ReflectUtil.newInstanceIfPossible(desc); - return oneCopy(source, copyOptions, v); - } + /** + * 鍗曞璞″熀浜巆lass鍒涘缓鎷疯礉 + * + * @param source 鏁版嵁鏉ユ簮瀹炰綋 + * @param desc 鎻忚堪瀵硅薄 杞崲鍚庣殑瀵硅薄 + * @return desc + */ + public static <T, V> V copy(T source, Class<V> desc) { + if (ObjectUtil.isNull(source)) { + return null; + } + if (ObjectUtil.isNull(desc)) { + return null; + } + return CglibUtil.copy(source, desc); + } - /** - * 鍗曞璞″熀浜庡璞″垱寤烘嫹璐� - * - * @param source 鏁版嵁鏉ユ簮瀹炰綋 - * @param copyOptions copy鏉′欢 - * @param desc 杞崲鍚庣殑瀵硅薄 - * @return desc - */ - public static <T, V> V oneCopy(T source, CopyOptions copyOptions, V desc) { - return BeanCopier.create(source, desc, copyOptions).copy(); - } + /** + * 鍗曞璞″熀浜庡璞″垱寤烘嫹璐� + * + * @param source 鏁版嵁鏉ユ簮瀹炰綋 + * @param desc 杞崲鍚庣殑瀵硅薄 + * @return desc + */ + public static <T, V> V copy(T source, V desc) { + if (ObjectUtil.isNull(source)) { + return null; + } + if (ObjectUtil.isNull(desc)) { + return null; + } + CglibUtil.copy(source, desc); + return desc; + } - /** - * 鍒楄〃瀵硅薄鍩轰簬class鍒涘缓鎷疯礉 - * - * @param sourceList 鏁版嵁鏉ユ簮瀹炰綋鍒楄〃 - * @param copyOptions copy鏉′欢 - * @param desc 鎻忚堪瀵硅薄 杞崲鍚庣殑瀵硅薄 - * @return desc - */ - public static <T, V> List<V> listCopy(List<T> sourceList, CopyOptions copyOptions, Class<V> desc) { - return sourceList.stream() - .map(source -> oneCopy(source, copyOptions, desc)) - .collect(Collectors.toList()); - } + /** + * 鍒楄〃瀵硅薄鍩轰簬class鍒涘缓鎷疯礉 + * + * @param sourceList 鏁版嵁鏉ユ簮瀹炰綋鍒楄〃 + * @param desc 鎻忚堪瀵硅薄 杞崲鍚庣殑瀵硅薄 + * @return desc + */ + public static <T, V> List<V> copyList(List<T> sourceList, Class<V> desc) { + if (ObjectUtil.isNull(sourceList)) { + return null; + } + if (CollUtil.isEmpty(sourceList)) { + return CollUtil.newArrayList(); + } + return CglibUtil.copyList(sourceList, () -> ReflectUtil.newInstanceIfPossible(desc)); + } + + /** + * bean鎷疯礉鍒癿ap + * + * @param bean 鏁版嵁鏉ユ簮瀹炰綋 + * @return map瀵硅薄 + */ + public static <T> Map<String, Object> copyToMap(T bean) { + if (ObjectUtil.isNull(bean)) { + return null; + } + return CglibUtil.toMap(bean); + } + + /** + * map鎷疯礉鍒癰ean + * + * @param map 鏁版嵁鏉ユ簮 + * @param beanClass bean绫� + * @return bean瀵硅薄 + */ + public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass) { + if (MapUtil.isEmpty(map)) { + return null; + } + if (ObjectUtil.isNull(beanClass)) { + return null; + } + return CglibUtil.toBean(map, beanClass); + } + + /** + * map鎷疯礉鍒癰ean + * + * @param map 鏁版嵁鏉ユ簮 + * @param bean bean瀵硅薄 + * @return bean瀵硅薄 + */ + public static <T> T mapToBean(Map<String, Object> map, T bean) { + if (MapUtil.isEmpty(map)) { + return null; + } + if (ObjectUtil.isNull(bean)) { + return null; + } + return CglibUtil.fillBean(map, bean); + } } -- Gitblit v1.9.3