4 Komitmen 647c60f43e ... 39cce5b0d8

Pembuat SHA1 Pesan Tanggal
  WangChanghua 39cce5b0d8 Merge remote-tracking branch 'origin/master' into V1.0.1 1 tahun lalu
  igl 513fc45aa2 分页 1 tahun lalu
  igl df951021bb 动态内容 1 tahun lalu
  igl adc7b0c4c8 动态内容 1 tahun lalu

+ 9 - 0
framework-common/pom.xml

@@ -95,5 +95,14 @@
             <groupId>com.googlecode.libphonenumber</groupId>
             <artifactId>geocoder</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-core</artifactId>
+            <version>3.4.2</version>
+        </dependency>
     </dependencies>
 </project>

+ 27 - 0
framework-common/src/main/java/com/chelvc/framework/common/model/PagedDTO.java

@@ -0,0 +1,27 @@
+package com.chelvc.framework.common.model;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * 分页传输模型
+ *
+ * @author igl
+ * @date 2023/05/26 10:46
+ */
+@Data
+public class PagedDTO implements Serializable {
+
+  private static final long serialVersionUID = -6094379285329869450L;
+
+  /**
+   * 页码
+   */
+  private Long pageCode;
+
+  /**
+   * 页大小
+   */
+  private Long pageSize;
+}

+ 55 - 0
framework-common/src/main/java/com/chelvc/framework/common/model/PagedVO.java

@@ -0,0 +1,55 @@
+package com.chelvc.framework.common.model;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.chelvc.framework.common.util.BeanCopyUtil;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 分页视图
+ *
+ * @author 武尊
+ * @date 2023/04/16 23:09
+ */
+@Data
+public class PagedVO<T> implements Serializable {
+
+  private static final long serialVersionUID = 3889341994334964174L;
+
+  /**
+   * 总数
+   */
+  private Long totalNumber;
+
+  /**
+   * 页码
+   */
+  private Long pageNumber;
+
+  /**
+   * 页面大小
+   */
+  private Long pageSize;
+
+  /**
+   * 数据记录
+   */
+  private List<T> records;
+
+  public static <T, S> PagedVO<S> build(IPage<T> dataSource, Class<S> targetClazz) {
+    PagedVO<S> result = new PagedVO<>();
+    result.setTotalNumber(dataSource.getTotal());
+    result.setPageNumber(dataSource.getCurrent());
+    result.setPageSize(dataSource.getSize());
+    List<S> records = new ArrayList<>();
+    for (T t : dataSource.getRecords()) {
+      S s = BeanCopyUtil.copy(t, targetClazz);
+      records.add(s);
+    }
+    result.setRecords(records);
+    return result;
+  }
+}

+ 93 - 0
framework-common/src/main/java/com/chelvc/framework/common/util/BeanCopyUtil.java

@@ -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();
+  }
+}

+ 25 - 0
framework-database/src/main/java/com/chelvc/framework/database/util/PagingUtils.java

@@ -4,6 +4,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.function.Function;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.chelvc.framework.common.model.Pagination;
 import com.chelvc.framework.common.model.Paging;
@@ -80,6 +81,17 @@ public final class PagingUtils {
         return convert(page, (List<T>) ObjectUtils.ifNull(page, Page::getRecords));
     }
 
+    /**
+     * 将MyBatis Plus分页参数转换成分页返回参数
+     *
+     * @param page    原始分页信息
+     * @param <T>     目标对象类型泛型
+     * @return 分页返回参数
+     */
+    public static <T> Pagination<T> convert(IPage<?> page, Class<T> type) {
+        return convert(page, ObjectUtils.copying((List<?>) ObjectUtils.ifNull(page, page.getRecords()), type));
+    }
+
     /**
      * 将MyBatis Plus分页参数转换成分页返回参数
      *
@@ -119,6 +131,19 @@ public final class PagingUtils {
                 .records(records).build();
     }
 
+    /**
+     * 将MyBatis Plus分页参数转换成分页返回参数
+     *
+     * @param page    MyBatis Plus分页参数
+     * @param records 分页数据列表
+     * @param <T>     目标对象类型泛型
+     * @return 分页返回参数
+     */
+    public static <T> Pagination<T> convert(IPage<?> page, List<T> records) {
+        return page == null ? null : Pagination.<T>builder().total(page.getTotal()).pages(page.getPages())
+                .records(records).build();
+    }
+
     /**
      * 分页记录转换
      *