From a07c004983de84137bf37e094b77cac78c00b6ef Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期四, 09 六月 2022 10:35:45 +0800
Subject: [PATCH] update 补全导包
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/BeanCopyUtils.java | 208 ++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 169 insertions(+), 39 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..9390604 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,185 @@
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.lang.SimpleCache;
+import cn.hutool.core.map.MapUtil;
+import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
+import cn.hutool.core.util.StrUtil;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.springframework.cglib.beans.BeanCopier;
+import org.springframework.cglib.beans.BeanMap;
+import org.springframework.cglib.core.Converter;
import java.util.List;
+import java.util.Map;
import java.util.stream.Collectors;
/**
- * 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;
+ }
+ final V target = ReflectUtil.newInstanceIfPossible(desc);
+ return copy(source, target);
+ }
- /**
- * 鍗曞璞″熀浜庡璞″垱寤烘嫹璐�
- *
- * @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;
+ }
+ BeanCopier beanCopier = BeanCopierCache.INSTANCE.get(source.getClass(), desc.getClass(), null);
+ beanCopier.copy(source, desc, null);
+ 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 sourceList.stream().map(source -> {
+ V target = ReflectUtil.newInstanceIfPossible(desc);
+ copy(source, target);
+ return target;
+ }).collect(Collectors.toList());
+ }
+
+ /**
+ * bean鎷疯礉鍒癿ap
+ *
+ * @param bean 鏁版嵁鏉ユ簮瀹炰綋
+ * @return map瀵硅薄
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> Map<String, Object> copyToMap(T bean) {
+ if (ObjectUtil.isNull(bean)) {
+ return null;
+ }
+ return BeanMap.create(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;
+ }
+ T bean = ReflectUtil.newInstanceIfPossible(beanClass);
+ return mapToBean(map, bean);
+ }
+
+ /**
+ * 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;
+ }
+ BeanMap.create(bean).putAll(map);
+ return bean;
+ }
+
+ /**
+ * BeanCopier灞炴�х紦瀛�<br>
+ * 缂撳瓨鐢ㄤ簬闃叉澶氭鍙嶅皠閫犳垚鐨勬�ц兘闂
+ *
+ * @author Looly
+ * @since 5.4.1
+ */
+ public enum BeanCopierCache {
+ /**
+ * BeanCopier灞炴�х紦瀛樺崟渚�
+ */
+ INSTANCE;
+
+ private final SimpleCache<String, BeanCopier> cache = new SimpleCache<>();
+
+ /**
+ * 鑾峰緱绫讳笌杞崲鍣ㄧ敓鎴愮殑key鍦▄@link BeanCopier}鐨凪ap涓搴旂殑鍏冪礌
+ *
+ * @param srcClass 婧怋ean鐨勭被
+ * @param targetClass 鐩爣Bean鐨勭被
+ * @param converter 杞崲鍣�
+ * @return Map涓搴旂殑BeanCopier
+ */
+ public BeanCopier get(Class<?> srcClass, Class<?> targetClass, Converter converter) {
+ final String key = genKey(srcClass, targetClass, converter);
+ return cache.get(key, () -> BeanCopier.create(srcClass, targetClass, converter != null));
+ }
+
+ /**
+ * 鑾峰緱绫讳笌杞崲鍣ㄧ敓鎴愮殑key
+ *
+ * @param srcClass 婧怋ean鐨勭被
+ * @param targetClass 鐩爣Bean鐨勭被
+ * @param converter 杞崲鍣�
+ * @return 灞炴�у悕鍜孧ap鏄犲皠鐨刱ey
+ */
+ private String genKey(Class<?> srcClass, Class<?> targetClass, Converter converter) {
+ final StringBuilder key = StrUtil.builder()
+ .append(srcClass.getName()).append('#').append(targetClass.getName());
+ if(null != converter){
+ key.append('#').append(converter.getClass().getName());
+ }
+ return key.toString();
+ }
+ }
+
}
--
Gitblit v1.9.3