3驳癌、mybatis分頁簡單實(shí)現(xiàn)

從開始學(xué)習(xí)到現(xiàn)在,一直逃不過分頁役听,學(xué)習(xí)總結(jié)如下。
設(shè)計(jì)思路
要有一個(gè)對象貫穿前后端表窘,包含了當(dāng)前頁面典予,分頁總條數(shù),每頁條數(shù)等乐严。

流程:頁面提交當(dāng)前頁數(shù)currentPage-->controller創(chuàng)建一個(gè)Page對象瘤袖,將這個(gè)值設(shè)置到currentPage上,傳給service-->service調(diào)用dao江场,查詢當(dāng)前需要查詢的總條數(shù)扼鞋,設(shè)置到totalNumber,通過這兩個(gè)參數(shù)計(jì)算總頁數(shù)筐高,和數(shù)據(jù)庫查詢用到的兩個(gè)參數(shù)dbIndex占婉,dbEnd泡嘴,Page對象傳給dao-->根據(jù)dbIndex,dbEnd分頁查詢逆济,返回查詢結(jié)果-->對象通過controller返回到頁面酌予,頁面根據(jù)Page顯示總條數(shù),當(dāng)前頁數(shù)奖慌,總頁數(shù)等抛虫。

注意:分頁查詢最好加order by,否則按照數(shù)據(jù)庫默認(rèn)排序简僧,不能保證每次排序方式都是相同的建椰,查詢結(jié)果會出問題。

Page類
根據(jù)當(dāng)前查詢的總條數(shù)totalNumber岛马, 每頁顯示幾條pageNumber棉姐,準(zhǔn)備顯示哪一頁currentPage,計(jì)算出總共有多少頁totalNumber蛛枚, 和數(shù)據(jù)庫查詢時(shí)需要的兩個(gè)參數(shù)dbIndex谅海,dbEnd

/**
 * 分頁對應(yīng)的實(shí)體類
 */
public class Page {
    /**
     * 總條數(shù)
     */
    private int totalNumber;
    /**
     * 當(dāng)前第幾頁
     */
    private int currentPage;
    /**
     * 總頁數(shù)
     */
    private int totalPage;
    /**
     * 每頁顯示條數(shù)
     */
    private int pageNumber = 10;
    /**
     * 數(shù)據(jù)庫中l(wèi)imit的參數(shù),從第幾條開始缺钠帧(dbIndex, dbNumber配合在mysql中使用)
     */
    private int dbIndex;
    /**
     * 數(shù)據(jù)庫中l(wèi)imit的參數(shù)扭吁,一共取多少條
     */
    private int dbNumber;
    
    // 取到第幾條(dbIndex, dbEnd配合在oracle中使用)
    private int dbEnd;
    
    
    /**
     * 根據(jù)當(dāng)前對象中屬性值計(jì)算并設(shè)置相關(guān)屬性值
     */
    public void count() {
        // 計(jì)算總頁數(shù)
        int totalPageTemp = this.totalNumber / this.pageNumber;
        int plus = (this.totalNumber % this.pageNumber) == 0 ? 0 : 1;
        totalPageTemp = totalPageTemp + plus;
        if(totalPageTemp <= 0) {
            totalPageTemp = 1;
        }
        this.totalPage = totalPageTemp;
        
        // 設(shè)置當(dāng)前頁數(shù)
        // 總頁數(shù)小于當(dāng)前頁數(shù),應(yīng)將當(dāng)前頁數(shù)設(shè)置為總頁數(shù)
        if(this.totalPage < this.currentPage) {
            this.currentPage = this.totalPage;
        }
        // 當(dāng)前頁數(shù)小于1設(shè)置為1
        if(this.currentPage < 1) {
            this.currentPage = 1;
        }
        
        // 設(shè)置limit的參數(shù)
        this.dbIndex = (this.currentPage - 1) * this.pageNumber;
        this.dbNumber = this.pageNumber;
        this.dbEnd = this.dbIndex + this.dbNumber;
    }

    public int getTotalNumber() {
        return totalNumber;
    }

    public void setTotalNumber(int totalNumber) {
        this.totalNumber = totalNumber;
        this.count();
    }

    public int getCurrentPage() {
        return currentPage;
    }

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

    public int getTotalPage() {
        return totalPage;
    }

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

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
        this.count();
    }

    public int getDbIndex() {
        return dbIndex;
    }

    public void setDbIndex(int dbIndex) {
        this.dbIndex = dbIndex;
    }

    public int getDbNumber() {
        return dbNumber;
    }

    public void setDbNumber(int dbNumber) {
        this.dbNumber = dbNumber;
    }

    public int getDbEnd() {
        return dbEnd;
    }

    public void setDbEnd(int dbEnd) {
        this.dbEnd = dbEnd;
    }

    @Override
    public String toString() {
        return "Page [totalNumber=" + totalNumber + ", currentPage="
                + currentPage + ", totalPage=" + totalPage + ", pageNumber="
                + pageNumber + ", dbIndex=" + dbIndex + ", dbNumber="
                + dbNumber + ", dbEnd=" + dbEnd + "]";
    }
}

jsp

<input type="hidden" name="currentPage" id="currentPage" value="${PAGE.currentPage}"/>
<div class=''>
    共 <b>${PAGE.totalNumber}</b> 條
    <c:if test="${PAGE.currentPage != 1}">
        <a href="javascript:changeCurrentPage('${SHEETID}', '1')" class='first'>首頁</a>
        <a href="javascript:changeCurrentPage('${SHEETID}', '${PAGE.currentPage-1}')" class='pre'>上一頁</a>
    </c:if>
    當(dāng)前第<span>${PAGE.currentPage}/${PAGE.totalPage}</span>頁
    <c:if test="${PAGE.currentPage != PAGE.totalPage}">
        <a href="javascript:changeCurrentPage('${SHEETID}', '${PAGE.currentPage+1}')" class='next'>下一頁</a>
        <a href="javascript:changeCurrentPage('${SHEETID}', '${PAGE.totalPage}')" class='last'>末頁</a>
    </c:if>
    跳至 <input id="currentPageText" type='text' value='${PAGE.currentPage}' class='allInput w28' style="width: 3%;"/> 頁 
    <a href="javascript:changeCurrentPage('${SHEETID}', $('#currentPageText').val())" class='go'>GO</a>
</div>

<script type="text/javascript">
    //修改當(dāng)前頁碼盲镶,調(diào)用后臺重新查詢
    function changeCurrentPage(sheetid, currentPage) {
        $("#currentPage").val(currentPage);
        var currentPageVal = $("#currentPage").val();
        console.log("currentPageVal:" + currentPageVal);
        // 獲取tabIndex
        var TABINDEX = $('#TABINDEX').val();
        window.location.href = '<%=basePath %>data/findRequireListBySheetId/' + sheetid + '/' + currentPageVal + '/' + TABINDEX;
    }
</script>

controller 創(chuàng)建Page對象侥袜,設(shè)置當(dāng)前頁數(shù)

@RequestMapping(value = "/findRequireListBySheetId/{sheetId}/{currentPage}/{tabIndex}", method=RequestMethod.GET)
public String findRequireListBySheetId(@PathVariable String sheetId, 
        @PathVariable String currentPage, @PathVariable String tabIndex, Map<String, Object> model) {
    // 創(chuàng)建分頁對象
    Page page = new Page();
    Pattern pattern = Pattern.compile("[0-9]{1,9}");
    // 如果當(dāng)前頁為空,或者不符合正則表達(dá)式溉贿,就設(shè)為第1頁
    if(currentPage == null ||  !pattern.matcher(currentPage).matches()) {
        page.setCurrentPage(1);
    } else {
        page.setCurrentPage(Integer.valueOf(currentPage));
    }
    // 獲取requirelist列表
    List<Map<String, String>> requireList = dataService.findRequireListBySheetIdPage(sheetId, page);
    model.put(ReturnConstant.FIND_REQUIRE_LIST_BY_SHEETID_PAGE, requireList);
    model.put(ReturnConstant.PAGE, page);
    model.put(ReturnConstant.SHEETID, sheetId);
    model.put(ReturnConstant.TABINDEX, tabIndex);
    logger.info("當(dāng)前tabIndex:" + tabIndex);
    return "/requireList";
}

service 調(diào)用dao

public List<Map<String, String>> findRequireListBySheetIdPage(String sheetId, Page page) {
    Map<String,Object> map = new HashMap<String, Object>();
    if("null".equals(sheetId)) {
        map.put("sheetId", null);
    } else {
        map.put("sheetId", sheetId);
    }
    map.put("page", page);
    // 分頁查詢并返回結(jié)果
    return requireDao.findRequireListBySheetIdPage(map);
}

RequireMapping.xml 沒有加分頁枫吧,分頁邏輯在攔截器中實(shí)現(xiàn),見下一節(jié)宇色。有排序九杂。

<select id="findRequireListBySheetIdPage" parameterType="java.util.Map" resultType="java.util.HashMap" >
    select t1.*, ermuser.realname from ermuser join (
      select ermrequire.*, ermusersheet.userid  from ermrequire join ermusersheet on 
      (ermrequire.sheetid = ermusersheet.sheetid) 
      <choose>
        <when test="sheetId != null">
            WHERE ermrequire.sheetid = #{sheetId} order by to_number(serialno)
        </when>
        <otherwise>
            order by to_date(ermrequire.requireaccepttime, 'yyyy/mm/dd')
        </otherwise>
      </choose>
    ) t1 on (ermuser.userid = t1.userid)
</select>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市宣蠕,隨后出現(xiàn)的幾起案子例隆,更是在濱河造成了極大的恐慌,老刑警劉巖抢蚀,帶你破解...
    沈念sama閱讀 223,126評論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件镀层,死亡現(xiàn)場離奇詭異,居然都是意外死亡皿曲,警方通過查閱死者的電腦和手機(jī)唱逢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評論 3 400
  • 文/潘曉璐 我一進(jìn)店門吴侦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人坞古,你說我怎么就攤上這事备韧。” “怎么了绸贡?”我有些...
    開封第一講書人閱讀 169,941評論 0 366
  • 文/不壞的土叔 我叫張陵盯蝴,是天一觀的道長。 經(jīng)常有香客問我听怕,道長捧挺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,294評論 1 300
  • 正文 為了忘掉前任尿瞭,我火速辦了婚禮闽烙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘声搁。我一直安慰自己黑竞,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,295評論 6 398
  • 文/花漫 我一把揭開白布疏旨。 她就那樣靜靜地躺著很魂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪檐涝。 梳的紋絲不亂的頭發(fā)上遏匆,一...
    開封第一講書人閱讀 52,874評論 1 314
  • 那天,我揣著相機(jī)與錄音谁榜,去河邊找鬼幅聘。 笑死,一個(gè)胖子當(dāng)著我的面吹牛窃植,可吹牛的內(nèi)容都是我干的帝蒿。 我是一名探鬼主播,決...
    沈念sama閱讀 41,285評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼巷怜,長吁一口氣:“原來是場噩夢啊……” “哼葛超!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起延塑,我...
    開封第一講書人閱讀 40,249評論 0 277
  • 序言:老撾萬榮一對情侶失蹤巩掺,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后页畦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,760評論 1 321
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡研儒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,840評論 3 343
  • 正文 我和宋清朗相戀三年豫缨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了独令。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,973評論 1 354
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡好芭,死狀恐怖燃箭,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情舍败,我是刑警寧澤招狸,帶...
    沈念sama閱讀 36,631評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站邻薯,受9級特大地震影響裙戏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜厕诡,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,315評論 3 336
  • 文/蒙蒙 一累榜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧灵嫌,春花似錦壹罚、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至绪穆,卻和暖如春辨泳,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背霞幅。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評論 1 275
  • 我被黑心中介騙來泰國打工漠吻, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人司恳。 一個(gè)月前我還...
    沈念sama閱讀 49,431評論 3 379
  • 正文 我出身青樓途乃,卻偏偏與公主長得像,于是被迫代替她去往敵國和親扔傅。 傳聞我的和親對象是個(gè)殘疾皇子耍共,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,982評論 2 361

推薦閱讀更多精彩內(nèi)容