这里对springboot+pagehelper 整合做一个记录
首先在pom.xml 导入依赖
// An highlighted block
<!--pagehelper 分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.8</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
这里需要导入3个依赖否则分页可能出现问题,(会查询出所有的数据),
再service(最好在service 中) 中进行分页处理
/**
* 加载用户数据,分页
* @param user
* @param pageSize 一页条数
* @param pageNum 第几页
* @return
*/
public templetJson loadList(user user , Integer pageSize, Integer pageNum){
templetJson tem=null;
try{
//分页初始化
Page<user> page = PageHelper.startPage(pageNum, pageSize);
//加载用户列表
List list= userMapper.loadUserList(user);
//传入list就可以了
PageInfo pageInfo = new PageInfo(list);
//处理分页数据(根据需要添加)
tem=utils.loadJsonPage(pageInfo);
}catch (Exception e){
e.printStackTrace();
}
return tem;
}
loadJsonPage() 方法处理layui 分页使用的方法
/**
* 分页方法使用
* @param pageInfo
* @return
*/
public static templetJson loadJsonPage(PageInfo pageInfo){
templetJson templetJson = new templetJson();
if(pageInfo!=null){
}
try{
List list= pageInfo.getList();
int i=(int)pageInfo.getTotal();
templetJson.setCount(i);
templetJson.setCode(0);
templetJson.setMsg("");
templetJson.setData(list);
}catch (Exception e){
templetJson.setCode(1);
}
return templetJson;
}
templetJson bean
package com.example.manage.bean;
/**
* layui 的模板数据bean
*/
public class templetJson {
private Integer code;
private String msg;
private Integer count;
private Object data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}