General tab core ideas:
the last time the query request uh, nothing has changed except the current page.
Home 1234 ... 100 Last Go
MySQL pagination: select * from table limit 3,3
The number of records per page set up their own
current page from the front
of the total number of pages the total number of records per page record number% == 0 Total number of records / number of records per page:? The total number of records / number of records per page +1
overall record the number of database statistics count ()
per home record number = (current page -1) * + 1 the number of records per page
Total 101 records per display 10
the first page: 1-10
second page: 11-20
third page: 21-30
The end of the number of records per page * = current number of records per page
1, a common query method code implementation
Import jar package
Before connecting to the database to see your user name, password, database name is correct
Book.java
Package com.huangyucan.util; public class PageBean { Private int Page =. 1; // p Private int rows = 10; // page size Private int Total = 0; // total number of records Private Boolean the pagination = to true ; // if paging public PageBean () { Super (); } public int the getPage () { return Page; } public void the setPage ( int Page) { this.page = page; } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public void setTotal(String total) { this.total = Integer.parseInt(total); } public boolean isPagination() { return pagination; } public void setPagination(boolean pagination) { this.pagination = pagination; } /** * 获得起始记录的下标 * * @return */ public int getStartIndex() { return (this.page - 1) * this.rows; } @Override public String toString() { return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]"; } }
BookDao.java
package com.huangyucan.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.zking.pageBean.entity.Book; import com.zking.pageBean.util.BaseDao; import com.zking.pageBean.util.BaseEntityDao; import com.zking.pageBean.util.DBAccess; import com.zking.pageBean.util.PageBean; import com.zking.pageBean.util.StringUtils; public class BookDao extends BaseEntityDao<Book>{ // /** // * // * @param book 带条件查询需要用上 // * @param pageBean 分页 // * @return // * @throws SQLException // */ // public List<Book> list(Book book,PageBean pageBean) throws SQLException{ // List<Book> list = new ArrayList<>(); // String sql = "select * from t_mvc_book where true"; // if(StringUtils.isNotBlank(book.getBname())) { // sql += " and bname like '%"+book.getBname()+"%'"; // } //// System.out.println(sql); // Connection con = DBAccess.getConnection(); // PreparedStatement pst = con.prepareStatement(sql); // ResultSet rs = pst.executeQuery(); // while(rs.next()) { // list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price"))); // } // return list; // } // /** // * oop改造后的方法 // * @param book // * @param pageBean // * @return // * @throws SQLException // * @throws IllegalAccessException // * @throws InstantiationException // */ // public List<Book> list(Book book,PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException{ // String sql = "select * from t_mvc_book where true"; // if(StringUtils.isNotBlank(book.getBname())) { // sql += " and bname like '%"+book.getBname()+"%'"; // } // return executeQuery(sql, pageBean, new CallBack() { // @Override // public List<Book> foreach(ResultSet rs) throws SQLException { // List<Book> list = new ArrayList<>(); // while(rs.next()) { // list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price"))); // } // return list; // } // }); SQLException@throws *@return *pageBean@param *Book@param * * use reflection to query the secondary enhanced/ ** }// *@throws InstantiationException * @throws IllegalAccessException */ public List<Book> list(Book book,PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException{ String sql = "select * from t_mvc_book where true"; if(StringUtils.isNotBlank(book.getBname())) { sql += " and bname like '%"+book.getBname()+"%'"; } return executeQuery(sql, pageBean, Book.class); } }
BaseDao
Package com.huangyucan.util; Import the java.sql.Connection; Import java.sql.PreparedStatement; Import the java.sql.ResultSet; Import java.sql.SQLException; Import of java.util.ArrayList; Import java.util.List; Import com.zking.pageBean.entity.Book; public class BaseDao <T> { / ** * reference ajax callback phenomenon of * the caller to handle the result set returned * @author Administrator * * / public abstract class the callBack { public abstractList <T> the foreach (the ResultSet RS) throws SQLException, an InstantiationException is, IllegalAccessException; } / ** * oop thinking of using a method of extracting the original query upward commonness * @param SQL * @param pageBean * @return * @throws SQLException * @throws IllegalAccessException * @throws an InstantiationException is * / public List <T> the executeQuery (String SQL, PageBean pageBean, the callBack callBack) throws SQLException, an InstantiationException is, IllegalAccessException { List<T> list = new ArrayList<>(); Connection con = DBAccess.getConnection(); String countSql = getCountSql(sql); PreparedStatement pst = con.prepareStatement(countSql); ResultSet rs = pst.executeQuery(); if(rs.next()) { pageBean.setTotal(rs.getString(1)); } DBAccess.close(null, pst, rs); String pageSql = getPageSql(sql, pageBean); PreparedStatement psmt =con.prepareStatement (pageSql); the ResultSet RST = psmt.executeQuery (); return callBack.foreach (RST); } / ** * Get tab sql statement * @param sql qualifying query * @param pageBean tab entity class * @return * / Private String getPageSql (String SQL, pageBean pageBean) { return SQL + "limit" pageBean.getStartIndex + () + "," + pageBean.getRows (); } / ** * Get match the query the total number of records sql * @param sql * @return * / private String getCountSql(String sql) { return "select count(*) from ("+ sql +") t"; } }
Page Tools
PageBase
Package com.huangyucan.util; / ** * Tools tab * * / public class PageBean { Private int Page =. 1; // p Private int rows = 10; // page size Private int Total = 0; // total recording number Private Boolean the pagination = to true ; // if paging public PageBean () { Super (); } public int the getPage () { return Page; } public void setPage(int page) { this.page = page; } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public void setTotal(String total) { this.total = Integer.parseInt(total); } public boolean isPagination() { return pagination; } public void setPagination(boolean pagination) { this.pagination = pagination; } /** * 获得起始记录的下标 * * @return */ public int getStartIndex() { return (this.page - 1) * this.rows; } @Override public String toString() { return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]"; } }