|
@@ -0,0 +1,93 @@
|
|
|
|
+package com.chelvc.framework.common.util;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Collection;
|
|
|
|
+import java.util.Collections;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Optional;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+import org.springframework.cglib.beans.BeanCopier;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Bean对象拷贝工具
|
|
|
|
+ *
|
|
|
|
+ * @author igl
|
|
|
|
+ * @date 2023/12/13 23:48
|
|
|
|
+ */
|
|
|
|
+public class BeanCopyUtil {
|
|
|
|
+
|
|
|
|
+ private final static Map<String, BeanCopier> BEAN_COPIER_WAREHOUSE = new ConcurrentHashMap<>();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Bean对象拷贝
|
|
|
|
+ *
|
|
|
|
+ * @param dataSource 数据源
|
|
|
|
+ * @param targetClazz 目标数据类型
|
|
|
|
+ * @return {@link S}
|
|
|
|
+ * @author igl
|
|
|
|
+ * @date 2023/12/13 23:48
|
|
|
|
+ */
|
|
|
|
+ public static <T, S> S copy(T dataSource, Class<S> targetClazz) {
|
|
|
|
+ BeanCopier beanCopier = getBeanCopier(dataSource.getClass(), targetClazz);
|
|
|
|
+ S result;
|
|
|
|
+ try {
|
|
|
|
+ result = targetClazz.newInstance();
|
|
|
|
+ beanCopier.copy(dataSource, result, null);
|
|
|
|
+ } catch (InstantiationException | IllegalAccessException e) {
|
|
|
|
+ throw new RuntimeException("数据转换异常");
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Bean对象拷贝
|
|
|
|
+ *
|
|
|
|
+ * @param dataSource 数据源
|
|
|
|
+ * @param result 结果
|
|
|
|
+ * @param targetClazz 目标类型
|
|
|
|
+ * @return {@link S}
|
|
|
|
+ * @author igl
|
|
|
|
+ * @date 2023/12/13 23:48
|
|
|
|
+ */
|
|
|
|
+ public static <T, S> S copy(T dataSource, S result, Class<S> targetClazz) {
|
|
|
|
+ BeanCopier beanCopier = getBeanCopier(dataSource.getClass(), targetClazz);
|
|
|
|
+ beanCopier.copy(dataSource, result, null);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * List对象拷贝
|
|
|
|
+ *
|
|
|
|
+ * @param source
|
|
|
|
+ * @param targetClazz
|
|
|
|
+ * @return {@link List<S>}
|
|
|
|
+ * @author igl
|
|
|
|
+ * @date 2023/12/13 23:48
|
|
|
|
+ */
|
|
|
|
+ public static <T, S> List<S> copy(Collection<T> source, Class<S> targetClazz) {
|
|
|
|
+ if (Optional.ofNullable(source).orElse(Collections.emptyList()).isEmpty()) {
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
+ }
|
|
|
|
+ List<S> result = new ArrayList<>();
|
|
|
|
+ for (T t : source) {
|
|
|
|
+ S s = copy(t, targetClazz);
|
|
|
|
+ result.add(s);
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static <T, S> BeanCopier getBeanCopier(Class<T> sourceClazz, Class<S> targetClazz) {
|
|
|
|
+ String key = getKey(sourceClazz, targetClazz);
|
|
|
|
+ if (BEAN_COPIER_WAREHOUSE.containsKey(key)) {
|
|
|
|
+ return BEAN_COPIER_WAREHOUSE.get(key);
|
|
|
|
+ }
|
|
|
|
+ BeanCopier beanCopier = BeanCopier.create(sourceClazz, targetClazz, false);
|
|
|
|
+ BEAN_COPIER_WAREHOUSE.put(key, beanCopier);
|
|
|
|
+ return beanCopier;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static <T, S> String getKey(Class<T> sourceClazz, Class<S> targetClazz) {
|
|
|
|
+ return sourceClazz.getSimpleName() + "To" + targetClazz.getSimpleName();
|
|
|
|
+ }
|
|
|
|
+}
|