第一种分页(用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; }