MySql實現(xiàn)翻頁查詢功能
首先明確為什么要使用分頁查詢,因為數(shù)據(jù)龐大,查詢不可能全部顯示在頁面上,如果全部顯示在頁面上,也會造成查詢速度慢的情況,所以分頁查詢解決了①數(shù)據(jù)查詢;②性能優(yōu)化,等(其他問題歡迎補充)的問題。
分頁查詢也分為真分頁和假分頁:
真分頁:基于數(shù)據(jù)庫查出的數(shù)據(jù)直接分頁顯示,優(yōu)點是改變數(shù)據(jù)庫數(shù)據(jù)不會影響查詢結(jié)果,缺點是速度稍慢。
假分頁:將所有數(shù)據(jù)查詢出的數(shù)據(jù),封裝到list集合緩存中,表現(xiàn)層方法調(diào)用執(zhí)行。由于將數(shù)據(jù)封裝為集合放入了內(nèi)存中,所以速度較快,但缺點是數(shù)據(jù)庫改變后,會出現(xiàn)不匹配的情況。
兩種分頁各有優(yōu)缺點,小伙伴們視具體情況使用吧。
下面要介紹的就是真分頁的方法:
1、建立JavaBean
import java.io.Serializable; /** * 用戶實體類 * @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層實現(xiàn)類
@Override public int getTotalCount() { //計算總的數(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層實現(xiàn)類
private IUserDao isd = new UserDaoImpl(); @Override public int getTotalPage() { //得到數(shù)據(jù)據(jù)條數(shù) int totalCount = isd.getTotalCount(); //計算總頁數(shù)公式 int totalPage = (totalCount + 10 -1)/10; return totalPage; } @Override public List<UserBean> getUserListByCurrPage(int currPage) { //通過當(dāng)前頁計算起始索引 int StartIndex = (currPage - 1) * 10; List<UserBean> userList = isd.getStuListByStartIndex(StartIndex); return userList; }
5、將查詢出的數(shù)據(jù)放入頁面展示就OK了。
以上方法中,分頁顯示的是10條數(shù)據(jù),計算分析如下:
數(shù)據(jù)總條數(shù): totalCount
每頁顯示條數(shù): pageSize
總頁數(shù): totalPage
起始索引 StartIndex
當(dāng)前頁數(shù) currPage
總頁計算公式:
totalCount % pageSize
如果余數(shù)為0 ——> totalPage=totalCount / pageSize
如果余數(shù)不為0 ——> totalPage=totalCount / pageSize +1
得出結(jié)論:totalPage = (totalCount + pageSize -1)/pageSize
總結(jié)
以上所述是小編給大家介紹的MySql實現(xiàn)翻頁查詢功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!