简介
PageHelper是一款好用的开源免费的Mybatis第三方分页插件,点击访问
后端(SSM项目)
注:这里只给出了关键部分代码,分页操作是通过对获取到的List集合进行二次封装实现的,具体请注意看Service层与Contoller层代码,其他代码未变动,按照业务逻辑正常编写即可
导入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency>
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
|
添加配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="plugins"> <set> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <props> <prop key="helperDialect">mysql</prop> </props> </property> </bean> </set> </property> </bean>
|
实体类
1 2 3 4 5 6 7 8 9 10 11
| @Data @AllArgsConstructor @NoArgsConstructor public class Student { private int id; private String name; private int age; private String sex; private String cls; private String num; }
|
mapper接口
1 2 3 4
| public interface StudentMapper { List<Student> findStudentList(); }
|
Service层
1 2 3 4
| public interface StudentService { PageInfo findStudentList(int pageNum,int pageSize); }
|
1 2 3 4 5 6 7
| public PageInfo findStudentList(int pageNum,int pageSize) { PageHelper.startPage(pageNum,pageSize); List<Student> list = studentMapper.findStudentList(); PageInfo pageInfo = new PageInfo(list); return pageInfo; }
|
Controller层
1 2 3 4 5 6 7 8
| @RequestMapping("/list/{pageNum}/{pageSize}") @ResponseBody
public PageInfo list(Model model, @PathVariable int pageNum, @PathVariable int pageSize) { PageInfo pageInfo = studentService.findStudentList(pageNum,pageSize); return pageInfo; }
|
PageInfo类的常见属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| 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 = false;
private boolean isLastPage = false;
private boolean hasPreviousPage = false;
private boolean hasNextPage = false;
private int navigatePages;
private int[] navigatepageNums;
private int navigateFirstPage;
private int navigateLastPage; }
|
前端
引入资源文件
1 2
| <link href="https://cdn.usebootstrap.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"> <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
|
HTML部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <div class="row clearfix"> <div class="col-md-12 column"> <table class="table table-hover table-striped" style='vertical-align: middle;text-align: center;'> <thead> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>班级</th> <th>学号</th> <th>操作</th> </tr> </thead>
<tbody id="tbody"> </tbody> </table> </div> </div>
<div class="row"> <nav aria-label="..." class="col-md-3 offset-md-9 mt-3"> <ul class="pagination" id="pageNum"> </ul> </nav> </div>
|
Javascript部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| <script> $(function () { getPageList(1,5);
let navigateFirstPage;
let navigateLastPage;
let pageNum;
let nextPage;
let prePage;
let navigatepageNums; });
function getPageList(pageNum,pageSize) { console.log("查询第" + pageNum + "页数据,每页" + pageSize + "条");
$.get({ url:"${pageContext.request.contextPath}/student/list/" + pageNum + "/" + pageSize, data:{}, success: function (data) { myTable(data); }, error: function () { alert("失败"); } }); }
function myTable(data) { navigateFirstPage = data.navigateFirstPage; navigateLastPage = data.navigateLastPage pageNum = data.nowPage; nextPage = data.nextPage; prePage = data.prePage; navigatepageNums = data.navigatepageNums;
let arr = data.list;
$("#tbody").html(""); for(let i =0; i<arr.length ;i++) { let html = "<tr>" + "<td>" + arr[i].id +"</td>" + "<td>" + arr[i].name +"</td>" + "<td>" + arr[i].age +"</td>" + "<td>" + arr[i].sex +"</td>" + "<td>" + arr[i].cls +"</td>" + "<td>" + arr[i].num +"</td>" + "<td>" + '<a class="btn btn-primary" href="<c:url value="/student/edit/'+ arr[i].id +'" />" style="margin-right: 10px">修改</a>' + '<a class="btn btn-danger" href="<c:url value="/student/delete/'+ arr[i].id +'" />">删除</a>' + "</td>" + "</tr>" $("#tbody").append(html); }
$("#pageNum").html(""); for(let i=0; i<navigatepageNums.length ;i++) { nowPage = navigatepageNums[i];
let pageNum = '<li class="page-item">' + '<button class="page-link" onclick="getPageList($(this).text(),5)">' + nowPage + '</button>' + "</li>";
let pageStart = '<li class="page-item">' + '<button class="page-link" onclick="getPageList(navigateFirstPage,5)">首页</button>' + "</li>";
let pageEnd = '<li class="page-item">' + '<button class="page-link" onclick="getPageList(nextPage,5)">下一页</button>' + "</li>";
if(i == 0) { pageNum = pageStart + pageNum; } if(i == navigatepageNums.length -1) { pageNum = pageNum + pageEnd; } $("#pageNum").append(pageNum); }
$("li.page-item")[data.pageNum].className += ' active'; } </script>
|