导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>net.sf.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
配置Spring-MyBatis.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>
<settings>
<setting name="useGeneratedKeys" value="true"/>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="cacheEnabled" value="true"/>
</settings>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
Controller层
@RequestMapping(value = "/gradeStudentsInfo", produces = "application/json;charset=utf-8")
public ModelAndView getGradeStudents(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
@RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
ModelAndView mv = new ModelAndView();
PagedResult<StudentDTO> pagedResult = adminStudentService.getPageStudents(pageNumber, pageSize);
mv.addObject("pagedResult", pagedResult);
mv.setViewName("/admin/student/studentInfo/queryStudents");
return mv;
}
Service接口
PagedResult<StudentDTO> getPageStudents(Integer pageNumber, Integer pageSize);
Service实现
@Override
public PagedResult<StudentDTO> getPageStudents(Integer pageNumber, Integer pageSize) {
PageHelper.startPage(pageNumber, pageSize);
List<StudentDTO> studentList = adminStudentDao.getPageStudents();
return PageBeanUtil.toPagedResult(studentList);
}
Dao层接口
List<StudentDTO> getPageStudents();
Mapper.xml
<select id="getPageStudents" resultType="StudentDTO">
SELECT
*
FROM
t_student
</select>
分页工具类
百度网盘: https://pan.baidu.com/s/1MmwyBwXSMD3sSKHigBuOLg
前端部分
<div class="admin-main ml-3 mr-3">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">序号</th>
<th scope="col">姓名</th>
<th scope="col">性别</th>
<th scope="col">年龄</th>
<th scope="col">学号</th>
<th scope="col">电话</th>
<th scope="col">邮箱</th>
<th scope="col">创建时间</th>
</tr>
</thead>
<tbody>
<c:forEach items="${pagedResult.dataList }" var="students"
varStatus="status">
<tr>
<td>${status.count}</td>
<td>${students.sname }</td>
<td>${students.sex }</td>
<td>${students.age }</td>
<td>${students.snumber}</td>
<td>${students.phone}</td>
<td>${students.email }</td>
<td><fmt:formatDate value="${students.createDate }"
pattern="yyyy-MM-dd" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div id="kkpager" class="page"></div>
<script type="text/javascript" src="${ctx }/js/page/page.js"></script>
<script type="text/javascript">
var totalPage = "${pagedResult.pages}";
var totalRecords = "${pagedResult.total}";
$(function(){
arrayPage(totalPage,totalRecords,"/admin/gradeStudentsInfo");
})
</script>
page.js
function arrayPage(pages,total,url) {
function getParameter(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null)
return unescape(r[2]);
return null;
}
$(function() {
var totalPage = pages;
var totalRecords = total;
var pageNo = getParameter('pageNumber');
if (!pageNo) {
pageNo = 1;
}
kkpager.generPageHtml({
pno : pageNo,
total : totalPage,
totalRecords : totalRecords,
mode : 'click',
click : function(n) {
this.selectPage(n);
loadMainContent(url, {
pageNumber : n,
pageSize : 3
});
return false;
}
});
});
};
Ajax局部刷新函数
function loadMainContent(url, params) {
$targes.find(".a-message").load("${ctx}" + url, params);
};
需要引入:
<link rel="stylesheet" href="${ctx}/plugins/kkpager/kkpager_orange.css">
<script src="${ctx}/plugins/kkpager/kkpager.js"></script>
下载地址:
http: