JAVA实现分页的方式

1.Mybatis-plus配置类,通过拦截器实现分页

@Configuration
public class MyBatisConfiguration extends BaseAdminController {
@Bean
public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    List<ISqlParser> sqlParserList = new ArrayList<>();
    // 添加租户sql解析链,如果是开发业务系统,可以去掉这个配置,不去也可以。
    TenantSqlParser tenantSqlParser = new TenantSqlParser() {
    };
    tenantSqlParser.setTenantHandler(tenantHandler());
    sqlParserList.add(tenantSqlParser);
    // 设置sql解析链
    paginationInterceptor.setSqlParserList(sqlParserList);
    // 设置过滤器链
    paginationInterceptor.setSqlParserFilter(sqlParserFilter());
    return paginationInterceptor;
}
}

2.subList查询结果分页

private void getSelectResIPage(PageDutySelectParam param, IPage<PageDutySelectRes> selectResIPage) {
    //手动组装分页方法
    List<PageDutySelectRes> pageDutySelectRes = getManualPage(param, selectResIPage, getCopyFilterList);
    selectResIPage.setRecords(pageDutySelectRes);
    selectResIPage.setPages(param.getPageNumber());
    selectResIPage.setSize(param.getPageSize());
    selectResIPage.setTotal(getCopyFilterList.size());
}


private List<PageDutySelectRes> getManualPage(PageDutySelectParam param, IPage<PageDutySelectRes> selectResIPage, List<PageDutySelectRes> getCopyFilterList) {
    int start = (param.getPageNumber() - 1) * param.getPageSize();
    int end = param.getPageNumber() * param.getPageSize();
    //看看会不会越界
    if (start > getCopyFilterList.size() - 1) {
        selectResIPage.setRecords(new ArrayList<>());
    }
    if (end > getCopyFilterList.size() - 1) {
        end = getCopyFilterList.size();
    }
    //手动分页设置序号
    for (int i = 0; i < getCopyFilterList.size(); i++) {
        getCopyFilterList.get(i).setNum(i+1);
    }

    List<PageDutySelectRes> pageDutySelectRes = getCopyFilterList.subList(start, end);
    return pageDutySelectRes;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值