其他分享
首页 > 其他分享> > 纯后台简单实现分页并返回JSON格式

纯后台简单实现分页并返回JSON格式

作者:互联网

1、实体层

package com.eccap.es.model;

import java.util.List;

/**
 * 分页信息
 * 
 * @author chaohui.yan
 * @date 2019-06-10
 *
 */
public class Pagefenye<T> {
    // 你页面需要展示的集合
    private List<T> list;
    // 总页数
    private int totalPage;

    // 当前页
    private int currentPage;
    // 每页显示的数目 (为了防止硬编码可以写在配置文件中)
    private int pageSize;

    public Pagefenye(int totalNum, int currentPage, int pageSize, int pageNo) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        /*this.pageNo = pageNo;*/

        // 计算总页数
        if (totalNum % this.pageSize == 0)
            this.totalPage = totalNum / this.pageSize;
        else
            this.totalPage = (totalNum / this.pageSize) + 1;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

}

2、Dao层select * from 表名 查询

3、service层

    @Autowired
    private EsEntPermitManager esEntPermitManager;
    
    public Pagefenye<T> findAllUsersByPage(int currentPage, int pageSize) {
        List<T> users =“调用查询方法”;//esEntPermitManager.fenye();
        Pagefenye<T> pageInfo = new Pagefenye<>(users.size(), currentPage, pageSize, 5);
        
        //判断是否是最后一页,如果最后一页就显示最后一页的几条,不是最后一页就显示pageSize条
        if(currentPage==pageInfo.getTotalPage())
            pageInfo.setList(users.subList((currentPage-1)*pageSize, users.size()));
        else {
            pageInfo.setList(users.subList((currentPage-1)*pageSize, (currentPage-1)*pageSize+pageSize));
        }
        return pageInfo;
    }

4、controller层

    public String Pagefenye() {
                //接收当前页
        String pageNo = this.getParameter("pageNo");
                //String 转 int
        int isd = Integer.parseInt(pageNo);
        String stg = null;
                //调用业务层方法并传入当前页,每页展示多少条
        Pagefenye<T> list = pagefenyeManager.findAllUsersByPage(isd,100);
        try {    
                       //获取查询结果转为json并展示空值
            stg = JSON.toJSONString(list,SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
            System.out.println(stg);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return stg.toString();
    }        

 

标签:totalPage,分页,pageSize,int,list,JSON,后台,currentPage,public
来源: https://www.cnblogs.com/yanchaohui/p/10996433.html