MySql實(shí)現(xiàn)翻頁查詢功能
首先明確為什么要使用分頁查詢,因?yàn)閿?shù)據(jù)龐大,查詢不可能全部顯示在頁面上,如果全部顯示在頁面上,也會(huì)造成查詢速度慢的情況,所以分頁查詢解決了①數(shù)據(jù)查詢;②性能優(yōu)化,等(其他問題歡迎補(bǔ)充)的問題。
分頁查詢也分為真分頁和假分頁:
真分頁:基于數(shù)據(jù)庫查出的數(shù)據(jù)直接分頁顯示,優(yōu)點(diǎn)是改變數(shù)據(jù)庫數(shù)據(jù)不會(huì)影響查詢結(jié)果,缺點(diǎn)是速度稍慢。
假分頁:將所有數(shù)據(jù)查詢出的數(shù)據(jù),封裝到list集合緩存中,表現(xiàn)層方法調(diào)用執(zhí)行。由于將數(shù)據(jù)封裝為集合放入了內(nèi)存中,所以速度較快,但缺點(diǎn)是數(shù)據(jù)庫改變后,會(huì)出現(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對(duì)象
* @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)翻頁查詢功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
MySQL 5.6.36 Windows x64位版本的安裝教程詳解
這篇文章主要介紹了MySQL 5.6.36 Windows x64位版本的安裝教程詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-05-05
MySQL重啟之后無法寫入數(shù)據(jù)的問題排查及解決
客戶在給系統(tǒng)打補(bǔ)丁之后需要重啟服務(wù)器,數(shù)據(jù)庫在重啟之后,read_only 的設(shè)置與標(biāo)準(zhǔn)配置 文件中不一致,導(dǎo)致主庫在啟動(dòng)之后無法按照預(yù)期寫入,所以本文給大家介紹了MySQL重啟之后無法寫入數(shù)據(jù)的問題排查及解決,需要的朋友可以參考下2024-05-05
mysql查詢結(jié)果實(shí)現(xiàn)多列拼接查詢
本文主要介紹了mysql查詢結(jié)果實(shí)現(xiàn)多列拼接查詢,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
在Windows主機(jī)上定時(shí)備份遠(yuǎn)程VPS(CentOS)數(shù)據(jù)的批處理
我想在自己的 Windows7 下每天/周運(yùn)行一次備份,就有了這個(gè)小工具2012-05-05
MySQL?bit類型增加索引后查詢結(jié)果不正確案例解析
這篇文章主要介紹了MySQL?bit類型增加索引后查詢結(jié)果不正確案例淺析,我們先創(chuàng)建表student_attend,初始化一些數(shù)據(jù),這篇文章的測試環(huán)境為MySQL 8.0.35社區(qū)版,具體內(nèi)容介紹跟隨小編一起學(xué)習(xí)吧2024-06-06

