欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2019年11月12日 09:16:11   作者:0vs1  
分頁查詢在網(wǎng)頁中隨處可見,那原理是什么呢?下面簡單介紹一下基于MySql數(shù)據(jù)庫的limit實(shí)現(xiàn)方法,感興趣的朋友一起看看吧

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

分頁查詢也分為真分頁和假分頁:

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

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

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

下面要介紹的就是真分頁的方法:

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、用于展示分頁數(shù)據(jù)的JavaBean

/**
 * 用于展示分頁數(shù)據(jù)的JavaBean對象
 * @author
 *
 */
import java.util.List;
public class PagenationBean {
  /** 當(dāng)前頁數(shù) */
  private Integer currPage;
  /** 總頁數(shù) */
  private Integer totalPage;
  /** 用于展示的table數(shù)據(jù) */
  private List<UserBean> 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 List<StuBean> getDataList() {
    return dataList;
  }
  public void setDataList(List<StuBean> 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 List<UserBean> getUserListByStartIndex(int StartIndex) { //根據(jù)傳入的limit第一位參數(shù)得到該參數(shù)后面的10條數(shù)據(jù)
    List<UserBean> 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ì)算總頁數(shù)公式
    int totalPage = (totalCount + 10 -1)/10;
    return totalPage;
  }
  @Override
  public List<UserBean> getUserListByCurrPage(int currPage) {
    //通過當(dāng)前頁計(jì)算起始索引
    int StartIndex = (currPage - 1) * 10;
    List<UserBean> userList = isd.getStuListByStartIndex(StartIndex);
    return userList;
  }

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

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

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

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

  總頁數(shù):    totalPage

  起始索引    StartIndex

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

  總頁計(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)翻頁查詢功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • 詳解Mysql取前一天、前一周、后一天等時(shí)間函數(shù)

    詳解Mysql取前一天、前一周、后一天等時(shí)間函數(shù)

    本文給大家介紹Mysql取前一天、前一周、后一天等時(shí)間函數(shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-11-11
  • Mysql8中的無插件方式審計(jì)

    Mysql8中的無插件方式審計(jì)

    這篇文章主要介紹了Mysql8中的無插件方式審計(jì),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • mysql幻讀詳解實(shí)例以及解決辦法

    mysql幻讀詳解實(shí)例以及解決辦法

    MySQL中的幻讀只有在讀的時(shí)候才會發(fā)生,讀這里特指SELECT操作,下面這篇文章主要給大家介紹了關(guān)于mysql幻讀詳解實(shí)例以及解決辦法的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Linux環(huán)境下安裝MySQL8.0的完整步驟

    Linux環(huán)境下安裝MySQL8.0的完整步驟

    數(shù)據(jù)庫想必大家都很熟悉,但是要在服務(wù)器上自己來安裝數(shù)據(jù)庫,還是會出現(xiàn)不少的問題,下面這篇文章主要給大家介紹了關(guān)于在Linux環(huán)境下安裝MySQL8.0的完整步驟,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • MySQL group by和order by如何一起使用

    MySQL group by和order by如何一起使用

    這篇文章主要介紹了MySQL group by和order by如何一起使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • MySql 5.6.14 winx64配置方法(免安裝版)

    MySql 5.6.14 winx64配置方法(免安裝版)

    這篇文章主要介紹了MySql 5.6.14 winx64配置方法(免安裝版)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-08-08
  • MySQL 查看庫中大表信息的幾種方法

    MySQL 查看庫中大表信息的幾種方法

    本文主要介紹了MySQL 查看庫中大表的幾種方法,為了識別可能影響數(shù)據(jù)庫性能的表,下面主要了4種方式,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • 快速解決mysql導(dǎo)出scv文件亂碼、躥行的問題

    快速解決mysql導(dǎo)出scv文件亂碼、躥行的問題

    這篇文章主要介紹了快速解決mysql導(dǎo)出scv文件亂碼、躥行的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 最新評論