- <!-- 分页插件 -->
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper</artifactId>
- <version>5.2.0</version>
- </dependency>
mybatis-config.xml
- <plugins>
-
- <plugin interceptor="com.github.pagehelper.PageInterceptor">plugin>
- plugins>
springxml
- <property name="plugins">
- <array>
- <bean class="com.github.pagehelper.PageInterceptor">bean>
- array>
- property>
PageHelper.startPage(pageNum, 3);开启分页pageNum当前页 ,3 每页数据数
List
PageInfo
- @Override
- public PageInfo
getEmpPage(Integer pageNum) { - PageHelper.startPage(pageNum, 3);
- List
empsList = empMapper.getEmpsList(); - //获取分页相关数据
- PageInfo
page = new PageInfo<>(empsList, 3); - return page;
- }
路径: @RequestMapping(value = "emplist/page/{pageNum}")pageNum当前页是从前端传过来的。
PageInfo
包括:分页相关信息以及数据库数据,通过model传到前端
PageInfo{
pageNum=1, pageSize=3, size=3, startRow=1, endRow=3, total=18, pages=6,
list=Page {
count=true, pageNum=1, pageSize=3, startRow=0, endRow=3, total=18, pages=6, reasonable=false, pageSizeZero=false
}
[
Emp(empId=1, empName=张三, age=12, gender=男, email=1),
Emp(empId=2, empName=李四, age=34, gender=男, email=2),
Emp(empId=4, empName=张涛, age=24, gender=男, email=2)],
prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=3, navigateFirstPage=1, navigateLastPage=3, navigatepageNums=[1, 2, 3]
}
- /**
- *
- * 分页查询
- */
-
- @RequestMapping(value = "emplist/page/{pageNum}" , method = RequestMethod.GET)
- public String getEmpByPageNum(@PathVariable("pageNum")Integer pageNum, Model model){
-
- PageInfo
pageInfo = empService.getEmpPage(pageNum); -
- model.addAttribute("page", pageInfo);
-
- System.out.println(pageInfo);
- return "emplist";
- }
实现效果
首页pageNum=1

非首页、末页

末页

实现代码:
注意路径拼接和导航页码判断
- <a th:if="${page.hasPreviousPage}" th:href="@{/emplist/page/1}">首页a>
- <a th:if="${page.hasPreviousPage}" th:href="@{'/emplist/page/'+${page.prePage}}">上一页a>
-
- <span th:each="nn:${page.navigatepageNums}">
- <a th:if="${page.pageNum == nn}" style="color: red;" th:href="@{'/emplist/page/'+${nn}}" th:text="'['+${nn}+']'">a>
- <a th:if="${page.pageNum != nn}" th:href="@{'/emplist/page/'+${nn}}" th:text="${nn}">a>
- span>
- <a th:if="${page.hasNextPage}" th:href="@{'/emplist/page/'+${page.nextPage}}">下一页a>
- <a th:if="${page.hasNextPage}" th:href="@{'/emplist/page/'+${page.pages}}">末页a>