- 首先在POM中导入PageHelper的包
<!--分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.1</version>
</dependency>
- 其次在mybatis-config.xml中配置插件
<!-- 插件配置 -->
<plugins>
<!-- 分页插件 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
- controller中如何调用 (后面会付上PageModel ,Page,CommonResult的结构)
@RequestMapping(value = "${adminPath}/pg/pagelist", method = RequestMethod.POST)
@ResponseBody
public CommonResult list(PageModel page,HttpServletRequest req) {
PageHelper.startPage((page.getStart()/page.getLength()+1),page.getLength()); //第一个参数为当前页码,第二个为每页的条数
List<Memo> list = memoService.findByUserId("1"); //之后的第一个查询方法
//PageInfo<Memo> pageInfo = new PageInfo<Memo>(list);
Page<Memo> pageBean = new Page<Memo>(new PageInfo<Memo>(list),req);
return new CommonResult().Success(pageBean);
}
其中PageModel
public class PageModel {
/**
* 查询次数
*/
private Integer draw;
/**
* 当前页数据开始位置
*/
private Integer start;
/**
* 当前页显示数据数量
*/
private Integer length;
public Integer getDraw() {
return draw;
}
public void setDraw(Integer draw) {
this.draw = draw;
}
public Integer getStart() {
return start;
}
public void setStart(Integer start) {
this.start = start;
}
public Integer getLength() {
return length;
}
public void setLength(Integer length) {
this.length = length;
}
/**
* 获取当前页数
* @return
*/
public int getPageNum(){
if(start == null){
return 0;
}
return start/getPageSize();
}
/**
* 获取每页显示数量
* @return
*/
public int getPageSize(){
if(length == null || length <=0){
return 10;
}
return length;
}
@Override
public String toString() {
return "PageModel [draw=" + draw + ", start=" + start + ", length="
+ length + "]";
}
}
其中Page
public class Page<T> {
/**
* <p>Title: </p>
* <p>Description: 构造函数,传入一个com.github.pagehelper.PageInfo对象</p>
*
* @param pageInfo
*/
public Page(PageInfo<T> pageInfo,HttpServletRequest req) {
try {
this.setDraw(req.getParameter("draw"));//从request中获取的draw在发回去
this.setPageSize(pageInfo.getPageSize());
this.setRecordsTotal(pageInfo.getTotal());
this.setRecordsFiltered(pageInfo.getTotal());
this.setData(pageInfo.getList());
} catch (Exception e) {
e.printStackTrace();
}
}
// 当前页
private String draw;
// 总页数
private Integer total;
// 每页显示多少
private Integer pageSize;
// 总记录数
private long recordsTotal;
private long recordsFiltered;
// 记录
private List<T> data;
public String getDraw() {
return draw;
}
public void setDraw(String draw) {
this.draw = draw;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public long getRecordsTotal() {
return recordsTotal;
}
public void setRecordsTotal(long recordsTotal) {
this.recordsTotal = recordsTotal;
}
public long getRecordsFiltered() {
return recordsFiltered;
}
public void setRecordsFiltered(long recordsFiltered) {
this.recordsFiltered = recordsFiltered;
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
}
其中CommonResult
/**
* 公共返回类
*
* created 2019-08-21
*
* @author Administrator
*
*/
public class CommonResult implements Serializable {
/**
*
*/
private static final long serialVersionUID = 9155256956679191062L;
public static final String MES_TEXT_ERROR = "失败";
public static final String MES_TEXT_SUCCESS = "成功";
public static final String MES_TEXT_EXCEPTION = "异常";
public static final String MES_TEXT_SESSIONOUT = "超时";
/**
* 类型
*/
public enum Type {
/** 成功 */
SUCCESS,
/** 失败 */
ERROR,
/** 异常 */
EXCEPTION,
/** 超时 */
SESSIONOUT,
}
/**
* 返回类型
*/
private Type type;
/**
* 返回数据
*/
private Object data;
/**
* 返回信息
*/
private String mes;
/**
* 获取返回类型
*
* @return
*/
public Type getType() {
return type;
}
/**
* 设置返回类型
*
* @param type
*/
public void setType(Type type) {
this.type = type;
}
/**
* 获取返回数据
*
* @return
*/
public Object getData() {
return data;
}
/**
* 设置返回数据
*
* @param data
*/
public void setData(Object data) {
this.data = data;
}
/**
* 获取返回信息
*
* @return
*/
public String getMes() {
return mes;
}
/**
* 设置返回信息
*
* @param mes
*/
public void setMes(String mes) {
this.mes = mes;
}
/**
* 构造函数
*/
public CommonResult(){
}
/**
* 构造函数
* @param type 返回类型
* @param mes 返回消息
*/
public CommonResult(Type type, String mes) {
this.type = type;
this.mes = mes;
}
/**
* 成功返回
*
* @param data
* 数据
* @param mes
* 消息
* @return
*/
public CommonResult Success(Object data, String mes) {
this.type = Type.SUCCESS;
this.data = data;
this.mes = mes;
return this;
}
/**
* 成功返回
*
* @param data
* 数据
* @return
*/
public CommonResult Success(Object data) {
this.type = Type.SUCCESS;
this.data = data;
this.mes = MES_TEXT_SUCCESS;
return this;
}
/**
* 成功返回
*
* @return
*/
public CommonResult Success() {
this.type = Type.SUCCESS;
this.mes = MES_TEXT_SUCCESS;
return this;
}
/**
* 返回失败
*
* @param mes
* 消息
* @return
*/
public CommonResult Error(String mes) {
this.type = Type.ERROR;
this.mes = mes;
return this;
}
/**
* 返回失败
*
* @return
*/
public CommonResult Error() {
this.type = Type.ERROR;
this.mes = MES_TEXT_ERROR;
return this;
}
/**
* 返回异常
*
* @param mes
* 消息
* @return
*/
public CommonResult Exception(String mes) {
this.type = Type.EXCEPTION;
this.mes = mes;
return this;
}
/**
* 返回异常
*
* @return
*/
public CommonResult Exception() {
this.type = Type.EXCEPTION;
this.mes = MES_TEXT_EXCEPTION;
return this;
}
/**
* 返回超时
*
* @param mes
* 消息
* @return
*/
public CommonResult SessionOut(String mes) {
this.type = Type.SESSIONOUT;
this.mes = mes;
return this;
}
/**
* 返回超时
*
* @return
*/
public CommonResult SessionOut() {
this.type = Type.SESSIONOUT;
this.mes = MES_TEXT_SESSIONOUT;
return this;
}
}
3.前台datatable的使用,这里只写一下和数据相关的,如果不会使用的,可以查看一下原文档
oTable = $('#dataTables').dataTable({
"bSort" : false,
"dom" : 't<"bottom"ilp><"clear">',//分页样式排序
"searching": false,
"bAutoWidth": false,
"serverSide": true,//服务器端获取数据
"paging": true,//开启分页
lengthMenu: [ 10, 20, 50 ],//自定义分页长度
ajax: {
"url": "/ob/admin/pg/pagelist",
"type": "POST",
"data": function (d) {
//删除多余请求参数
for(var key in d){
if(key.indexOf("columns")==0||key.indexOf("order")==0||key.indexOf("search")==0){ //以columns开头的参数删除
delete d[key];
}
}
$.extend(d,{
"title":$("#title").val(),
"author":$("#author").val(),
"categoryId":$("#category").attr("data-id"),
"tagId":$("#tag").attr("data-id")
}); //给d扩展参数},
"dataType" : "json",
"dataFilter": function (json) {//json是服务器端返回的数据
json = JSON.parse(json);
var returnData = {};
// returnData.draw = json.data.draw;
returnData.recordsTotal = json.data.recordsTotal;//返回数据全部记录
returnData.recordsFiltered = json.data.recordsTotal;//后台不实现过滤功能,每次查询均视作全部结果
returnData.data = json.data.data;//返回的数据列表
currentPage = json.data.recordsFiltered;//当前页数
return JSON.stringify(returnData);//这几个参数都是datatable需要的,必须要
}
},
"columns": [
{ "data": "id" },
{ "data": "id" },
{ "data": null }//操作
],
columnDefs: [{
// 指定第最后一列
targets: -1,
render: function(data, type, row, meta) {
return '<a class="J_menuItem details" href="/archive/view?id='+ row.id+'" >详情</a>'+
'<a target="_blank" class="details" href="/detail/'+ row.id+'.html" > 预览</a>'+
'<a id = "delete" class="delete authority_user" onclick="delItem(\'' + row.id + '\')">删除</a>';
}
}],
"searching" : false,//搜索框
ordering:false, //排序
"drawCallback": function (settings, data) {
//getPermissions();
}
});