Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PageInfo泛型转换✒️ pull#477 #553

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/main/java/com/github/pagehelper/PageInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.github.pagehelper;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;

Expand Down Expand Up @@ -147,6 +148,70 @@ public void calcByNavigatePages(int navigatePages) {
judgePageBoudary();
}

/**
* pageInfo泛型转换
*
* @param dtoClass DTO类的class对象
* @param <E> entity类
* @param <D> DTO类
* @return PageInfo<D> 返回转换后的PageInfo对象
*/
public <E, D> PageInfo<D> convert(Class<D> dtoClass){
return convert(this,dtoClass);
}

/**
* pageInfo泛型转换
*
* @param pageInfoE pageInfo<E>类对象
* @param dtoClass DTO类的class对象
* @param <E> entity类
* @param <D> DTO类
* @return PageInfo<D> 返回转换后的PageInfo对象
*/
public static <E, D> PageInfo<D> convert(PageInfo<E> pageInfoE, Class<D> dtoClass) {
//创建page对象,传入当前页,和每页数量进行初始化(page对象是ArrayList的子类,在ArrayList的基础上添加了分页的信息)
Page<D> 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<D>对象
return new PageInfo<D>(page, pageInfoE.getNavigatePages());
}

/**
* 复制Bean对象属性<br>
*
* @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();
}
}

/**
* 计算导航页
*/
Expand Down