springBoot集成pageHelper完成后臺(tái)分頁功能的實(shí)現(xiàn)

1.pom依賴的引入

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

2.在項(xiàng)目的入口類application.java中設(shè)置PageHelper

//配置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");    //配置mysql數(shù)據(jù)庫的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }

3.PageBean工具類

package com.fykj.cloud.epaper.entity.bean;
import java.util.List;

/**
 * 分頁bean
 */
public class PageBean<T> {
    // 當(dāng)前頁
    private Integer currentPage = 1;
    // 每頁顯示的總條數(shù)
    private Integer pageSize = 10;
    // 總條數(shù)
    private Integer totalNum;
    // 是否有下一頁
    private Integer isMore;
    // 總頁數(shù)
    private Integer totalPage;
    // 開始索引
    private Integer startIndex;
    // 分頁結(jié)果
    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 PageBean(Integer currentPage, Integer pageSize, Integer totalNum,List<T> list){
        this(currentPage,pageSize, totalNum);
        this.items = list;
    }

    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;
        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 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;
    }
}

4.分頁功能代碼實(shí)現(xiàn)(servicece層)

@Override
    public PageBean<UserBlackList> selectUserBlackListList(Integer currentPage, Integer pageSize, UserBlackList userBlackList) {
        try {
            PageBean<UserBlackList> entity = new PageBean();
            if (null != pageSize) {
                entity.setPageSize(pageSize);
            }
            if (null != currentPage) {
                entity.setCurrentPage(currentPage);
            }
            PageHelper.startPage(entity.getCurrentPage(), entity.getPageSize());
            List<UserBlackList> list = userBlackListMapper.queryModelList(userBlackList);
            Integer count = userBlackListMapper.queryModelListCount(userBlackList);
            entity.setItems(list);
            entity.setTotalNum(count);
            return entity;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 如果有些東西注定不屬于我,那即使片刻擁有,也會(huì)一夜之間消失。 這樣想想,些許釋然。 (圖片來自網(wǎng)絡(luò))
    夜晚安閱讀 152評(píng)論 0 0
  • 題外話:沒想到這么快就迎來了可以寫“大學(xué)回憶”這個(gè)主題 ,還是有種猝不及防,像是被時(shí)間突然推了一把,一晃就跌入了成...
    三分鐘姑娘Holiday閱讀 669評(píng)論 3 3
  • 水底的石頭星星一樣閃爍 幾個(gè)孩子魚一樣游來游去 三兩下脫去了衣服 撲通一下跳了進(jìn)去 居然沒過了頭頂 兩只手慌亂的胡...
    清水濯足閱讀 311評(píng)論 0 0
  • 我們剛認(rèn)識(shí)的時(shí)候,在我需要幫助的時(shí)候,TA總是第一時(shí)間出現(xiàn)回應(yīng)我,滿足我的需要,這讓我感覺到很溫暖、很感動(dòng),我覺得...
    美鳳的打怪日記閱讀 337評(píng)論 0 0

友情鏈接更多精彩內(nèi)容