springboot+layui实现分页

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<Book> 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<String, String[]> 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<String, String[]> getParamMap() {
        return paramMap;
    }

    public void setParamMap(Map<String, String[]> 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依赖

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

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<User> 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中配置任何文件

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值