java后端分页方案

第一种分页(用sql分页)

public static int[] getStartEndPage(int page, int limit) {

    int startPage = (page - 1) * limit + 1;
    int endPage = page * limit;
    int[] startEndPage = {startPage, endPage};
    return startEndPage;

}

//计算起始页 int[] startEndPage = PageUtils.getStartEndPage(page, limit); startPage = startEndPage[0]; endPage = startEndPage[1];

Select * from (select ROWNUM rn, t.* from (select * from table) t) where rn between startPage and endPage

第二种分页 (先查询所有,在截取)

public TableResultResponse<ClamcOperatingDataMana> selectDataManaList(Integer page, Integer limit, String queryStr) { List<ClamcOperatingDataMana> list = dataManaBiz.selectDataManaList(queryStr);

    Integer total = list.size();
    Integer maxCount = limit * page;
    Integer minCount = limit * (page - 1);
    if (maxCount > list.size()) {
        maxCount = list.size();
    }
    list = list.subList(minCount, maxCount);
    TableResultResponse<ClamcOperatingDataMana> tableResultResponse = new TableResultResponse<>(total, list);
    tableResultResponse.setMessage("success");
    return tableResultResponse;

}

第三种分页(用pageheper插件)

pom <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.4</version> </dependency>

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration> <!-- 配置分页插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <property name="dialect" value="Oracle"/> </plugin> </plugins>

</configuration>

application.yml mybatis: mapper-locations: classpath:mapper/**/*.xml type-aliases-package: com.clamc.entity config-location: classpath:builder/SqlMapConfig.xml //扫描MyBatis全局变量配置

public PageInfo<Doc> selectDocByPage1(int currentPage, int pageSize) { PageHelper.startPage(currentPage, pageSize); List<Doc> docs = docMapper.selectByPageAndSelections(); PageInfo<Doc> pageInfo = new PageInfo<>(docs); return pageInfo; }

转载于:https://my.oschina.net/u/3668344/blog/2978877

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值