侧边栏壁纸
博主头像
三生石博主等级

梦想还是有的,就算做咸鱼,也要做最咸的。|

  • 累计撰写 31 篇文章
  • 累计创建 55 个标签
  • 累计收到 7 条评论

Java分页查询page类

三生石
2021-07-02 / 1 评论 / 0 点赞 / 123 阅读 / 3,503 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-04-07,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Java分页查询page

1.创建page实现类和想要分页数据的实现类

public class PageUtil <T>{
    private Integer pageIndex; //页码
    private Integer pageSize; //页容量
    private Integer total;   //总条数
    private Integer totalPage; //总页数
    private Integer beginIndex; //起始位置
    private T rows;         //数据集

    public PageUtil(Integer pageIndex, Integer pageSize, Integer total) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.total = total;
        this.totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize +1;
        this.beginIndex = (pageIndex -1)*pageSize;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

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

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

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

    public Integer getBeginIndex() {
        return beginIndex;
    }

    public void setBeginIndex(Integer beginIndex) {
        this.beginIndex = beginIndex;
    }

    public T getRows() {
        return rows;
    }

    public void setRows(T rows) {
        this.rows = rows;
    }
}

想要分页的数据实现类

public class NewsCategory {
    private Long id;
    private String name;
    private Date createDate;

    @Override
    public String toString() {
        return "NewsCategory{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", createDate=" + createDate +
                '}';
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

2.Dao层

在Dao添加两个方法,分别是要获取数据的总数和一个分页的查询,也要在接口上添加。

/***
     * 获取新闻分类数量
     * @return
     */
    public Integer getTotal(){
        getConnection();
        String sql = "select count(id) from news_category";
        Integer total = null;
        rs = executeQuery(sql , null);
        try {
            while(rs.next()){
                total = rs.getInt("count(id)");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        close();
        return total;
    }

    /***
     * 分页查询数据
     * @param beginIndex    起始位置
     * @param pageSize      页容量
     * @return              符合条件的数据集
     */
    public List<NewsCategory> queryNewsPage(Integer beginIndex , Integer pageSize){
        getConnection();
        String sql = "select id,name,createDate from news_category limit ?,?";
        Object[] params = {beginIndex,pageSize};
        rs = executeQuery(sql,params);
        List<NewsCategory> newsCategories = new ArrayList<>();
        try {
            while(rs.next()){
                NewsCategory newsCategory = new NewsCategory();
                newsCategory.setId(rs.getLong("id"));
                newsCategory.setName(rs.getString("name"));
                newsCategory.setCreateDate(rs.getDate("createDate"));
                newsCategories.add(newsCategory);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        close();
        return newsCategories;
    }

3.service层

service层添加一个方法,就是分页功能方法,参数是页码和页容量,接口添加。

public PageUtil<List<NewsCategory>> page(Integer pageIndex, Integer pageSize){
        pageIndex = pageIndex == null ? 1:pageIndex;
        pageSize = pageSize == null ? 5 :pageSize;
        //查找条数
        Integer total = newsCategoryDao.getTotal();
        PageUtil<List<NewsCategory>> pageUtil = new PageUtil<>(pageIndex,pageSize,total);
        //开始分页查找
        List<NewsCategory> newsCategories = newsCategoryDao.queryNewsPage(pageUtil.getBeginIndex(),pageUtil.getPageSize());
        pageUtil.setRows(newsCategories);
        return pageUtil;
    }

4测试

public class test1 {
    public static void main(String[] args) {
        NewsCategoryService newsCategoryService = new NewsCategoryServiceImpl();
        PageUtil<List<NewsCategory>> page = newsCategoryService.page(1,3);
        System.out.println("总页数"+page.getTotalPage());
        System.out.println("当前页数据");
        for (NewsCategory row : page.getRows()){
            System.out.println(row);
        }
    }
}
0

评论区