In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
First of all, it is clear why you want to use paging query, because the data is huge, the query can not all be displayed on the page, if all displayed on the page, it will also cause slow query speed, so paging query solves the problem of ① data query; ② performance optimization, etc. (other problems are welcome to add).
Paging queries are also divided into true and false pages:
True paging: direct paging based on the data found in the database, the advantage is that changing the database data will not affect the query results, the disadvantage is that the speed is slightly slower.
False paging: encapsulate all the data queried into the list collection cache, and the presentation layer method is called to execute. Because the data is encapsulated as a collection and placed in memory, it is faster, but the disadvantage is that after the database changes, there will be a mismatch.
The two kinds of pages have their own advantages and disadvantages, friends according to the specific situation to use it.
The following is the method of true paging:
1. Establish JavaBean
Import java.io.Serializable;/** * user entity class * @ author * * / public class UserBean implements Serializable {/ * * user ID*/ private int id; / * * user name * / 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. JavaBean for displaying paged data
/ * JavaBean object used to display paged data * @ author * * / import java.util.List;public class PagenationBean {/ * current number of pages * / private Integer currPage; / * * Total number of pages * / private Integer totalPage; / * * table data used for display * / private List 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 getDataList () {return dataList;} public void setDataList (List dataList) {this.dataList = dataList;}}
3. Dao layer implementation class
@ Override public int getTotalCount () {/ / calculate the total number of data entries 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 getUserListByStartIndex (int StartIndex) {/ / get the following 10 data List userList = new ArrayList (); UserBean userBean= null; this.setConnection (); int totalCount = 0 based on the first bit parameter of limit passed in; 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 layer implementation class
Private IUserDao isd = new UserDaoImpl (); @ Override public int getTotalPage () {/ / get the number of data entries int totalCount = isd.getTotalCount (); / / calculate the total number of pages int totalPage = (totalCount + 10-1) / 10; return totalPage;} @ Override public List getUserListByCurrPage (int currPage) {/ / calculate the starting index int StartIndex = (currPage-1) * 10 from the current page; List userList = isd.getStuListByStartIndex (StartIndex) Return userList;}
5. Put the queried data into the page and display it on OK.
In the above method, paging displays 10 pieces of data, and the calculation and analysis are as follows:
Total number of data entries: totalCount
Number of entries per page: pageSize
Total pages: totalPage
Initial index StartIndex
Current number of pages currPage
Total page calculation formula:
TotalCount pageSize
If the remainder is 0-> totalPage=totalCount / pageSize
If the remainder is not 0-> totalPage=totalCount / pageSize + 1
It is concluded that totalPage = (totalCount + pageSize-1) / pageSize
Summary
The above is the MySql page turning query function introduced by the editor to you. I hope it will be helpful to you. If you have any questions, please leave a message for me. The editor will reply you in time. Thank you very much for your support to the website!
If you think this article is helpful to you, you are welcome to reprint it, please indicate the source, thank you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.