首先明確為什么要使用分頁(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 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ì)算總頁(yè)數(shù)公式 int totalPage = (totalCount + 10 -1)/10; return totalPage; } @Override public List<UserBean> getUserListByCurrPage(int currPage) { //通過(guò)當(dāng)前頁(yè)計(jì)算起始索引 int StartIndex = (currPage - 1) * 10; List<UserBean> 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)網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
新聞熱點(diǎn)
疑難解答
圖片精選