分页插件
添加依赖
1 2 3 4 5
| <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version> </dependency>
|
配置插件
1 2 3
| <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins>
|
分页插件的使用
几种须知的条件
limit index, pageSize
index
: 当前页的起始索引
pageSize
: 每页显示的数据条数
pageNum
: 当前页的页码
count
:总记录数
totalPage
:总页数 totalPage = count / pageSize
1 2 3 4 5
|
if(count % pagesize != 0){ totalPage += 1; }
|
举例说明:
1 2 3 4 5 6 7
| pageSize = 4 ,pageNum = 1, index= 0 limit 0 ,4 pageSize = 4 ,pageNum = 3, index= 8 limit 8 ,4 pageSize = 4 ,pageNum = 6, index= 20 limit 20 ,4
|
list.forEach(System.out::println);
列表查询
PageInfo中的属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class PageInfo<T> implements Serializable { private static final long serialVersionUID = 1L; private int pageNum; private int pageSize; private int size; private int startRow; private int endRow; private long total; private int pages; private List<T> list; private int prePage; private int nextPage; private boolean isFirstPage; private boolean isLastPage; private boolean hasPreviousPage; private boolean hasNextPage; private int navigatePages; private int[] navigatepageNums; private int navigateFirstPage; private int navigateLastPage;
|
后端
- 开启分页显示
1 2 3 4 5 6 7 8
| @RequestMapping("/list/{pageNum}") public String pageStart(@PathVariable("pageNum") Integer pageNum, Model model){ PageInfo<employee> page = employeeService.Page(pageNum); List<employee> list = page.getList(); model.addAttribute("list",list); model.addAttribute("page",page); return "list"; }
|
- 在service层中开启分页以及获取数据,然后将数据回显到controller层
1 2 3 4 5 6 7 8 9 10 11 12
| @Override public PageInfo<employee> Page(Integer pageNum) { PageHelper.startPage(pageNum,8); List<employee> list = EmpMapper.selectAll(); for(employee li : list){ dept dept = DeptMapper.selectByPrimaryKey(li.getDeptId()); li.setDep(dept); } PageInfo<employee> pageInfo = new PageInfo<>(list,4); return pageInfo; }
|
前端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <div style="text-align: center" > <span th:if="${page.hasPreviousPage}"> <a class="btn btn-sm btn-primary" th:href="@{/list/1}">首页</a> <a class="btn btn-sm btn-primary" th:href="@{'/list/' + ${page.prePage}}">上一页</a> </span> <span th:each="num : ${page.navigatepageNums}"> <a class="btn btn-sm btn-primary" th:if="${page.pageNum==num}" th:href="@{'/list/'+${num}}" th:text="'['+${num}+']'" style="color:#d51313;"></a> <a class="btn btn-sm btn-primary" th:if="${page.pageNum!=num}" th:href="@{'/list/'+${num}}" th:text="${num} "></a> </span> <span th:if="${page.hasNextPage}"> <a class="btn btn-sm btn-primary" th:href="@{'/list/'+${page.nextPage}}">下一页</a> <a class="btn btn-sm btn-primary" th:href="@{'/list/'+${page.pages}}">末页</a> </span> </div>
|
通过超链接发送请求的参数,然后通过与controller请求映射进行匹配,从而将请求发送至后端来处理