1.实现分页前导入分页切面
@Component
@Aspect
public class PageAspect {
/**
* 环绕通知,作用在对象上
* @param args
* @return
* @throws Throwable
*/
@Around("execution(* *..*Service.*Pager(..))")
public Object invoke(ProceedingJoinPoint args) throws Throwable{
// 获取目标方法传递过来的参数集合
Object[] params=args.getArgs();
// 如果说,传过来的参数有pageBean,那么就进行分页
PageBean pageBean=null;
for (Object param:params){
if(param instanceof PageBean){
pageBean=(PageBean) param;
break;
}
}
if (pageBean!=null &&pageBean.isPagination()){
PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
}
/* List books = bookMapper.queryByNamePager(book);*/
Object proceed = args.proceed(params);
if (pageBean!=null &&pageBean.isPagination()){
PageInfo pageInfo = new PageInfo((List) proceed);
pageBean.setTotal(pageInfo.getTotal()+"");
}
return proceed;
}
}
2.导入PageBea页面
public class PageBean {
private int page = 1;// 页码
private int rows = 5;// 页大小
private int total = 0;// 总记录数
private boolean pagination = true;// 是否分页
// 保存上次查询的参数
private Map paramMap;
// 保存上次查询的url
private String url;
public void setRequest(HttpServletRequest request) {
String page = request.getParameter("page");
String rows = request.getParameter("limit");
String pagination = request.getParameter("pagination");
this.setPage(page);
this.setRows(rows);
this.setPagination(pagination);
this.setUrl(request.getRequestURL().toString());
this.setParamMap(request.getParameterMap());
}
public PageBean() {
super();
}
public Map getParamMap() {
return paramMap;
}
public void setParamMap(Map paramMap) {
this.paramMap = paramMap;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public void setPage(String page) {
if(StringUtils.isNotBlank(page)) {
this.page = Integer.parseInt(page);
}
}
public int getRows() {
return rows;
}
public void setRows(String rows) {
if(StringUtils.isNotBlank(rows)) {
this.rows = Integer.parseInt(rows);
}
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
if(StringUtils.isNotBlank(total)) {
this.total = Integer.parseInt(total);
}
}
public boolean isPagination() {
return pagination;
}
public void setPagination(boolean pagination) {
this.pagination = pagination;
}
public void setPagination(String pagination) {
if(StringUtils.isNotBlank(pagination) && "false".equals(pagination)) {
this.pagination = Boolean.parseBoolean(pagination);
}
}
/**
* 最大页
* @return
*/
public int getMaxPage() {
int max = this.total/this.rows;
if(this.total % this.rows !=0) {
max ++ ;
}
return max;
}
/**
* 下一页
* @return
*/
public int getNextPage() {
int nextPage = this.page + 1;
if(nextPage > this.getMaxPage()) {
nextPage = this.getMaxPage();
}
return nextPage;
}
/**
* 上一页
* @return
*/
public int getPreviousPage() {
int previousPage = this.page -1;
if(previousPage < 1) {
previousPage = 1;
}
return previousPage;
}
/**
* 获得起始记录的下标
*
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
}
}
3.导入pom.xml依赖
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
4.Controller业务逻辑层
@ResponseBody
@RequestMapping("/UserList")
public Map UserList(HttpServletRequest request){
Map result = new HashMap();
User user=new User();
PageBean pageBean=new PageBean();
pageBean.setRequest(request);
List users = userService.UserLisetPager(user, pageBean);
result.put("data",users);
result.put("count",pageBean.getTotal());//总页数
result.put("code","0");//必须写
return result;
}
最后一步就是在js page: false //改为true
layui.use(['element', 'table', 'form'], function () {
var $ = layui.jquery;
var element = layui.element; //Tab的切换功能,切换事件监听等,需要依赖element模块
var table = layui.table;
var form = layui.form;
var data_table = "data_table";
table.render({
elem: '#' + data_table
, url: 'UserList'
, toolbar: '#topBtns'
// ,defaultToolbar:[] 设置这个还是会留空白
, id: data_table
, title: '博客类别数据表'
, page: true //改为true
, cols: [[ //表头
{type: 'checkbox', fixed: 'left'},
{field: 'uid', title: 'ID', width: 80, sort: true, fixed: 'left', totalRowText: '合计:'},
{field: 'userName', title: '用户名', width: 180},
{field: 'password', title: '密码', width: 290, sort: true},
{field: 'realName', title: '真实名', width: 90, sort: true, totalRow: true},
{field: 'salt', title: '描述', width: 290, sort: true, totalRow: true},
{fixed: 'right', title: '操作', toolbar: '#lineBtns'}
]]
按照上面的步骤走就能实现springboot+layui分页,不需要在application.yml中配置任何文件