SpringBoot下使用PageHelper分頁插件

引入依賴

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.6</version>
        </dependency>

配置

@SpringBootApplication
public class ThreesillyApplication {

    public static void main(String[] args) {
        SpringApplication.run(ThreesillyApplication.class, args);
    }

    //配置mybatis的分頁插件pageHelper
    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("reasonable","true");
        properties.setProperty("dialect","mysql");
        pageHelper.setProperties(properties);
        return pageHelper;
    }

}

 新建一個分頁Bean

package com.snow.threesilly.entity;

import java.util.List;

/**
 * 分頁Bean
 *
 * @author snow
 *
 * @since 2018年2月16日
 *
 * @version 1.0
 */
public class PageBean<T> {

    //當前頁
    private Integer currentPage = 1;
    //每頁顯示的總條數
    private Integer pageSize = 10;
    //總條數
    private Integer totalNum;
    //是否有下一頁
    private Integer isMore;
    //總頁數
    private Integer totalPage;
    //開始索引
    private Integer startIndex;
    //分頁結果
    private List<T> items;

    public PageBean() {
        super();
    }

    public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) {
        super();
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalNum = totalNum;
        this.totalPage = (this.totalNum + this.pageSize-1) / this.pageSize;
        this.startIndex = (this.currentPage - 1) * this.pageSize;
        this.isMore = this.currentPage >= this.totalPage ? 0 : 1;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

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

    public Integer getPageSize() {
        return pageSize;
    }

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

    public Integer getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(Integer totalNum) {
        this.totalNum = totalNum;
    }

    public Integer getIsMore() {
        return isMore;
    }

    public void setIsMore(Integer isMore) {
        this.isMore = isMore;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

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

    public Integer getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(Integer startIndex) {
        this.startIndex = startIndex;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }
}

使用(service層)

    public List<LiveTv> queryAll(LiveTv liveTv) {
        //該語句需在查詢語句前面才能生效,接收的參數爲起始頁和每頁顯示數
        PageHelper.startPage(liveTv.getCurrentPage(), liveTv.getPageSize());
        //查詢數據
        List<LiveTv> liveList = liveTvMapper.queryAll(liveTv);
        //進行分頁。起始頁、每頁數、數據總數
        PageBean<LiveTv> pb = new PageBean<>(liveTv.getCurrentPage(), liveTv.getPageSize(), liveList.size());
        pb.setItems(liveList);
        return pb.getItems();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章