濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > MySql實(shí)現(xiàn)翻頁(yè)查詢功能

MySql實(shí)現(xiàn)翻頁(yè)查詢功能

熱門標(biāo)簽:常州網(wǎng)絡(luò)外呼系統(tǒng)開(kāi)發(fā) 銷售語(yǔ)音電話機(jī)器人 外呼系統(tǒng)電銷受騙 安徽ai電話電銷機(jī)器人有效果嗎 在哪里申請(qǐng)400電話 400電話申請(qǐng)信用卡 巫師三血與酒地圖標(biāo)注 萊西市地圖標(biāo)注 走過(guò)哪個(gè)省地圖標(biāo)注

首先明確為什么要使用分頁(yè)查詢,因?yàn)閿?shù)據(jù)龐大,查詢不可能全部顯示在頁(yè)面上,如果全部顯示在頁(yè)面上,也會(huì)造成查詢速度慢的情況,所以分頁(yè)查詢解決了①數(shù)據(jù)查詢;②性能優(yōu)化,等(其他問(wèn)題歡迎補(bǔ)充)的問(wèn)題。

分頁(yè)查詢也分為真分頁(yè)和假分頁(yè):

  真分頁(yè):基于數(shù)據(jù)庫(kù)查出的數(shù)據(jù)直接分頁(yè)顯示,優(yōu)點(diǎn)是改變數(shù)據(jù)庫(kù)數(shù)據(jù)不會(huì)影響查詢結(jié)果,缺點(diǎn)是速度稍慢。

  假分頁(yè):將所有數(shù)據(jù)查詢出的數(shù)據(jù),封裝到list集合緩存中,表現(xiàn)層方法調(diào)用執(zhí)行。由于將數(shù)據(jù)封裝為集合放入了內(nèi)存中,所以速度較快,但缺點(diǎn)是數(shù)據(jù)庫(kù)改變后,會(huì)出現(xiàn)不匹配的情況。

  兩種分頁(yè)各有優(yōu)缺點(diǎn),小伙伴們視具體情況使用吧。

下面要介紹的就是真分頁(yè)的方法:

1、建立JavaBean

import java.io.Serializable;
/**
 * 用戶實(shí)體類
 * @author 
 *
 */
public class UserBean implements Serializable {
  /**用戶ID*/
  private int id;
  /**用戶名字*/
  private String name;
  public UserBean() {
  }
  public UserBean(int id, String name) {
    this.id = id;
    this.name = name;
  }
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  @Override
  public String toString() {
    return "UserBean [id=" + id + ", name=" + name + "]";
  }
}

2、用于展示分頁(yè)數(shù)據(jù)的JavaBean

/**
 * 用于展示分頁(yè)數(shù)據(jù)的JavaBean對(duì)象
 * @author
 *
 */
import java.util.List;
public class PagenationBean {
  /** 當(dāng)前頁(yè)數(shù) */
  private Integer currPage;
  /** 總頁(yè)數(shù) */
  private Integer totalPage;
  /** 用于展示的table數(shù)據(jù) */
  private ListUserBean> dataList;
  public Integer getCurrPage() {
    return currPage;
  }
  public void setCurrPage(Integer currPage) {
    this.currPage = currPage;
  }
  public Integer getTotalPage() {
    return totalPage;
  }
  public void setTotalPage(Integer totalPage) {
    this.totalPage = totalPage;
  }
  public ListStuBean> getDataList() {
    return dataList;
  }
  public void setDataList(ListStuBean> dataList) {
    this.dataList = dataList;
  }
}

3、dao層實(shí)現(xiàn)類

 

 @Override
  public int getTotalCount() { //計(jì)算總的數(shù)據(jù)條數(shù)
    this.setConnection();
    int totalCount = 0;
    try {
      ps = con.prepareStatement("select count(*) from t_user");
      rs = ps.executeQuery();
      if (rs.next()) {
        totalCount = rs.getInt(1);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      this.closeConnection();
    }
    return totalCount;
  }
  @Override
  public ListUserBean> getUserListByStartIndex(int StartIndex) { //根據(jù)傳入的limit第一位參數(shù)得到該參數(shù)后面的10條數(shù)據(jù)
    ListUserBean> userList = new ArrayList>();
    UserBean userBean= null;
    this.setConnection();
    int totalCount = 0;
    try {
      ps = con.prepareStatement("select * from t_user limit ? , 10");
      ps.setInt(1, StartIndex);
      rs = ps.executeQuery();
      while (rs.next()) {
        userBean= new StuBean();
        userBean.setId(rs.getInt("id"));
        userBean.setName(rs.getString("name"));
        stuList.add(userBean);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      this.closeConnection();
    }    
    return userList;
  }  

4、service層實(shí)現(xiàn)類

  

private IUserDao isd = new UserDaoImpl();
  @Override
  public int getTotalPage() {
    //得到數(shù)據(jù)據(jù)條數(shù)
    int totalCount = isd.getTotalCount();
    //計(jì)算總頁(yè)數(shù)公式
    int totalPage = (totalCount + 10 -1)/10;
    return totalPage;
  }
  @Override
  public ListUserBean> getUserListByCurrPage(int currPage) {
    //通過(guò)當(dāng)前頁(yè)計(jì)算起始索引
    int StartIndex = (currPage - 1) * 10;
    ListUserBean> userList = isd.getStuListByStartIndex(StartIndex);
    return userList;
  }

5、將查詢出的數(shù)據(jù)放入頁(yè)面展示就OK了。

以上方法中,分頁(yè)顯示的是10條數(shù)據(jù),計(jì)算分析如下:

   數(shù)據(jù)總條數(shù):  totalCount

  每頁(yè)顯示條數(shù): pageSize

  總頁(yè)數(shù):    totalPage

  起始索引    StartIndex

  當(dāng)前頁(yè)數(shù)    currPage

  總頁(yè)計(jì)算公式:

     totalCount % pageSize

      如果余數(shù)為0 ——> totalPage=totalCount / pageSize

         如果余數(shù)不為0 ——> totalPage=totalCount / pageSize +1

    得出結(jié)論:totalPage = (totalCount + pageSize -1)/pageSize

總結(jié)

以上所述是小編給大家介紹的MySql實(shí)現(xiàn)翻頁(yè)查詢功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

您可能感興趣的文章:
  • 通過(guò)MySQL優(yōu)化Discuz!的熱帖翻頁(yè)的技巧
  • JAVA/JSP學(xué)習(xí)系列之八(改寫(xiě)MySQL翻頁(yè)例子)
  • JAVA/JSP學(xué)習(xí)系列之六(MySQL翻頁(yè)例子)
  • MySQL查詢條件中in會(huì)用到索引嗎
  • MySQL中查詢某一天, 某一月, 某一年的數(shù)據(jù)代碼詳解
  • MySQL聯(lián)表查詢的簡(jiǎn)單示例
  • 解決MySQl查詢不區(qū)分大小寫(xiě)的方法講解
  • mysql實(shí)現(xiàn)查詢數(shù)據(jù)并根據(jù)條件更新到另一張表的方法示例

標(biāo)簽:陽(yáng)江 煙臺(tái) 河北 果洛 赤峰 黃石 鞍山 來(lái)賓

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySql實(shí)現(xiàn)翻頁(yè)查詢功能》,本文關(guān)鍵詞  MySql,實(shí)現(xiàn),翻頁(yè),查詢功能,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySql實(shí)現(xiàn)翻頁(yè)查詢功能》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于MySql實(shí)現(xiàn)翻頁(yè)查詢功能的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    宁陵县| 密云县| 平度市| 土默特右旗| 崇明县| 陆良县| 文化| 锡林浩特市| 抚松县| 广河县| 朝阳市| 海林市| 绥芬河市| 额济纳旗| 星座| 盐亭县| 彰武县| 吴忠市| 县级市| 宁河县| 应城市| 辽中县| 铜陵市| 梁山县| 祥云县| 河北区| 苍山县| 喜德县| 龙州县| 蓬莱市| 眉山市| 南部县| 个旧市| 南宫市| 墨竹工卡县| 若羌县| 什邡市| 乐安县| 安西县| 永川市| 航空|