From 1b752c35cc0c7f51922351d5bcca88a043002ae8 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期一, 06 十二月 2021 10:40:57 +0800
Subject: [PATCH] update 使用 Cglib 重构 BeanCopyUtils 性能优异

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/BeanCopyUtils.java |   95 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 73 insertions(+), 22 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 8bf40d1..3bc4434 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,16 +1,16 @@
 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 java.util.List;
-import java.util.stream.Collectors;
+import java.util.Map;
 
 /**
- * bean娣辨嫹璐濆伐鍏�
+ * bean娣辨嫹璐濆伐鍏�(鍩轰簬 cglib 鎬ц兘浼樺紓)
  *
  * @author Lion Li
  */
@@ -19,48 +19,99 @@
     /**
      * 鍗曞璞″熀浜巆lass鍒涘缓鎷疯礉
      *
-     * @param source      鏁版嵁鏉ユ簮瀹炰綋
-     * @param copyOptions copy鏉′欢
-     * @param desc        鎻忚堪瀵硅薄 杞崲鍚庣殑瀵硅薄
+     * @param source 鏁版嵁鏉ユ簮瀹炰綋
+     * @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);
+    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        杞崲鍚庣殑瀵硅薄
+     * @param source 鏁版嵁鏉ユ簮瀹炰綋
+     * @param desc   杞崲鍚庣殑瀵硅薄
      * @return desc
      */
-    public static <T, V> V oneCopy(T source, CopyOptions copyOptions, V desc) {
+    public static <T, V> V copy(T source, V desc) {
         if (ObjectUtil.isNull(source)) {
             return null;
         }
-        return BeanCopier.create(source, desc, copyOptions).copy();
+        if (ObjectUtil.isNull(desc)) {
+            return null;
+        }
+        CglibUtil.copy(source, desc);
+        return desc;
     }
 
     /**
      * 鍒楄〃瀵硅薄鍩轰簬class鍒涘缓鎷疯礉
      *
-     * @param sourceList  鏁版嵁鏉ユ簮瀹炰綋鍒楄〃
-     * @param copyOptions copy鏉′欢
-     * @param desc        鎻忚堪瀵硅薄 杞崲鍚庣殑瀵硅薄
+     * @param sourceList 鏁版嵁鏉ユ簮瀹炰綋鍒楄〃
+     * @param desc       鎻忚堪瀵硅薄 杞崲鍚庣殑瀵硅薄
      * @return desc
      */
-    public static <T, V> List<V> listCopy(List<T> sourceList, CopyOptions copyOptions, Class<V> 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 sourceList.stream()
-                .map(source -> oneCopy(source, copyOptions, desc))
-                .collect(Collectors.toList());
+        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