diff --git a/src/main/java/com/github/pagehelper/PageInfo.java b/src/main/java/com/github/pagehelper/PageInfo.java index 45f18995..b91f9bfe 100644 --- a/src/main/java/com/github/pagehelper/PageInfo.java +++ b/src/main/java/com/github/pagehelper/PageInfo.java @@ -24,6 +24,7 @@ package com.github.pagehelper; +import java.lang.reflect.Field; import java.util.Collection; import java.util.List; @@ -147,6 +148,70 @@ public void calcByNavigatePages(int navigatePages) { judgePageBoudary(); } + /** + * pageInfo泛型转换 + * + * @param dtoClass DTO类的class对象 + * @param entity类 + * @param DTO类 + * @return PageInfo 返回转换后的PageInfo对象 + */ + public PageInfo convert(Class dtoClass){ + return convert(this,dtoClass); + } + + /** + * pageInfo泛型转换 + * + * @param pageInfoE pageInfo类对象 + * @param dtoClass DTO类的class对象 + * @param entity类 + * @param DTO类 + * @return PageInfo 返回转换后的PageInfo对象 + */ + public static PageInfo convert(PageInfo pageInfoE, Class dtoClass) { + //创建page对象,传入当前页,和每页数量进行初始化(page对象是ArrayList的子类,在ArrayList的基础上添加了分页的信息) + Page page = new Page(pageInfoE.getPageNum(), pageInfoE.getPageSize()); + //传入总记录数 + page.setTotal(pageInfoE.getTotal()); + //遍历原entity的列表 + for (E e : pageInfoE.getList()) { + try { + //通过class对象生成DTO的对象 + D d = dtoClass.getConstructor().newInstance(); + //使用BeanUtils将entity中与dto相同的属性拷贝到dto中并放入page列表 + copyProperties(e, d); + page.add(d); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + //通过page对象以及pageInfoE中的导航页码数创建要返回的pageInfo对象 + return new PageInfo(page, pageInfoE.getNavigatePages()); + } + + /** + * 复制Bean对象属性
+ * + * @param source 源Bean对象 + * @param target 目标Bean对象 + */ + public static void copyProperties(Object source, Object target) { + try { + for (Field targetField : target.getClass().getDeclaredFields()) { + targetField.setAccessible(true); + for (Field sourceField : source.getClass().getDeclaredFields()) { + sourceField.setAccessible(true); + if (targetField.getName().equals(sourceField.getName())) { + targetField.set(target, sourceField.get(source)); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + /** * 计算导航页 */