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

feat: 在PageInfo类中新增了用以进行数据对象转换的方法 #756

Closed
wants to merge 1 commit into from

Conversation

chodenke
Copy link

@chodenke chodenke commented Jul 7, 2023

阅读了PageInfo泛型转换✒️ pull#477 #553的相关内容,自己在使用Mybatis-PageHelper时也遇到同样的问题,咨询了大家的解决方案,均是自己在项目中实现一个转换工具类,这是我之前使用的转换工具类

import com.github.pagehelper.PageInfo;
import org.springframework.util.Assert;

import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 用来转换 PageInfo 中封装对象类型的转换器
 */
public class PageInfoConverter {

    /**
     * 解析 封装 T 类型实例的 PageInfo,创建并返回一个对象的 封装 E 类型实例的 PageInfo
     *
     * @param sourcePageInfo 源 PageInfo
     * @param function       T 类型实例 转换未 E 类型实例 的转换函数
     * @param <E>            目标类型
     * @param <T>            源类型
     * @return 封装 E 类型实例的 PageInfo实例
     */
    public static <E, T> PageInfo<E> convert(PageInfo<T> sourcePageInfo, Function<T, E> function) {
        Assert.notNull(sourcePageInfo, "sourcePageInfo must not be null");
        PageInfo<E> targetPageInfo = new PageInfo<>(sourcePageInfo.getList().stream().map(function).collect(Collectors.toList()));
        targetPageInfo.setPageNum(sourcePageInfo.getPageNum());
        targetPageInfo.setPageSize(sourcePageInfo.getPageSize());
        targetPageInfo.setSize(sourcePageInfo.getSize());
        targetPageInfo.setStartRow(sourcePageInfo.getStartRow());
        targetPageInfo.setEndRow(sourcePageInfo.getEndRow());
        targetPageInfo.setTotal(sourcePageInfo.getTotal());
        targetPageInfo.setPages(sourcePageInfo.getPages());
        targetPageInfo.setPrePage(sourcePageInfo.getPrePage());
        targetPageInfo.setNextPage(sourcePageInfo.getNextPage());
        targetPageInfo.setIsFirstPage(sourcePageInfo.isIsFirstPage());
        targetPageInfo.setIsLastPage(sourcePageInfo.isIsLastPage());
        targetPageInfo.setHasPreviousPage(sourcePageInfo.isHasPreviousPage());
        targetPageInfo.setHasNextPage(sourcePageInfo.isHasNextPage());
        targetPageInfo.setNavigatePages(sourcePageInfo.getNavigatePages());
        targetPageInfo.setNavigateFirstPage(sourcePageInfo.getNavigateFirstPage());
        targetPageInfo.setNavigateLastPage(sourcePageInfo.getNavigateLastPage());
        targetPageInfo.setNavigatepageNums(sourcePageInfo.getNavigatepageNums());
        return targetPageInfo;
    }
}

但本着使用一个插件时,需要配套提供的配置及自建工具类尽可能少的想法,又考虑您之前提到的”涉及属性转换,考虑性能问题“及”对分页插件的强依赖问题“等的担忧,最终建议使用这样的方式,即将转换的具体实现方式交由用户自己实现,借鉴Page类的toPageInfo()方法,在PageInfo类中新增类似的方法,如下

private <E> PageInfo<E> convert(Page.Function<T, E> function) {
    List<E> list = new ArrayList<E>(this.list.size());
    for (T t : this.list) {
        list.add(function.apply(t));
    }
    PageInfo<E> newPageInfo = new PageInfo<>(list);
    newPageInfo.setPageNum(this.pageNum);
    newPageInfo.setPageSize(this.pageSize);
    newPageInfo.setSize(this.size);
    newPageInfo.setStartRow(this.startRow);
    newPageInfo.setEndRow(this.endRow);
    newPageInfo.setTotal(this.total);
    newPageInfo.setPages(this.pages);
    newPageInfo.setPrePage(this.prePage);
    newPageInfo.setNextPage(this.nextPage);
    newPageInfo.setIsFirstPage(this.isFirstPage);
    newPageInfo.setIsLastPage(this.isLastPage);
    newPageInfo.setHasPreviousPage(this.hasPreviousPage);
    newPageInfo.setHasNextPage(this.hasNextPage);
    newPageInfo.setNavigatePages(this.navigatePages);
    newPageInfo.setNavigateFirstPage(this.navigateFirstPage);
    newPageInfo.setNavigateLastPage(this.navigateLastPage);
    newPageInfo.setNavigatepageNums(this.navigatepageNums);
    return newPageInfo;
}

这里的方法参数中使用的Function接口使用Page类中的,目的亦是不引入对jdk1.8的强依赖;

此时,我在控制器中使用时的代码由

PageInfo<Order> orderPageInfo = orderService.findPage(pageNum, pageSize, OrderSearch.of(orderSearchParam));
PageInfo<OrderDTO> orderDTOPageInfo = PageInfoConverter.convert(orderPageInfo, Order::getOrderDTO);

简化成

PageInfo<Order> orderPageInfo = orderService.findPage(pageNum, pageSize, OrderSearch.of(orderSearchParam));
PageInfo<OrderDTO> orderDTOPageInfo = orderPageInfo.convert(Order::getOrderDTO);

此时,OrderOrderDTO的转换由用户自己实现,但用户可以不再需要维护一个用来做数据转换的工具类,减轻了使用负担。
希望能够得到采纳。

@abel533
Copy link
Collaborator

abel533 commented Jul 10, 2023

基于jdk1.8的版本可以有这种方式。

@chodenke
Copy link
Author

谢谢您的肯定,单元测试中testSql545()的预期结果和实际结果不符导致测试不通过,我将关闭这个pr之后提交一个新的pr。

@chodenke chodenke closed this Jul 10, 2023
abel533 added a commit that referenced this pull request Jul 10, 2023
PageInfo类中新增了用以进行数据对象转换的方法 #756 的后续
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants