通用分页
Posted omji0030
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通用分页相关的知识,希望对你有一定的参考价值。
核心思路:将上一次 的查询请求再发送一次,这一次改变页码
PageBean
private int page; 页数
private int rows; 行数
private boolean pagination; 是否分页
private int total; 总行数;
package com.page; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.List; import com.dao.impl.BaseDao; public class PageDao<E> extends BaseDao public List<Boolean> executeQueryPage(String sql, PageBean pageBean, ICallBack<E> iCallBack) Connection con = null; PreparedStatement ps = null; ResultSet rs = null; try // 得到sql的总行数 con = getcon(); ps = con.prepareStatement(sql); rs = ps.executeQuery(); pageBean.setTotal(rs.getString(1)); close(ps, rs); // 调用分页sql语句 ps = con.prepareStatement(getPageSql(sql, pageBean)); rs = ps.executeQuery(); return (List<Boolean>) iCallBack.forEach(rs); catch (Exception e) throw new RuntimeException(e); finally close(con, ps, rs); /** * 返回总行数 * * @param sql * @return */ private String getCountSql(String sql) return "select count (*) from (" + sql + ") t1"; /** * 返回分页语句 * * @param sql * @param pageBean * @return */ private String getPageSql(String sql, PageBean pageBean) int page = pageBean.getPage(); int rows = pageBean.getRows(); return sql + "limit" + (page - 1) * rows + "." + rows;
1 package com.dao.impl; 2 3 import java.io.InputStream; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.util.Properties; 9 10 /** 11 * 连接数据库操作基本类 12 * 13 * @author xyls 14 * 15 */ 16 17 public class BaseDao 18 19 private static Properties properties = null; 20 static 21 if (properties == null) 22 InputStream is = BaseDao.class.getResourceAsStream("/config.properties"); 23 properties = new Properties(); 24 try 25 properties.load(is); 26 Class.forName(properties.getProperty("driver")); 27 catch (Exception e) 28 throw new RuntimeException(e); 29 30 31 32 33 /** 34 * 获取数据库连接对象 35 * 36 * @return 37 */ 38 public Connection getcon() 39 try 40 return DriverManager.getConnection(properties.getProperty("url"), properties.getProperty("user"), 41 properties.getProperty("pwd")); 42 43 catch (Exception e) 44 throw new RuntimeException(e); 45 46 47 48 /** 49 * 关闭数据库的连接 50 * 51 * @param con 52 * @param ps 53 */ 54 public void close(Connection con, PreparedStatement ps) 55 close(con, ps, null); 56 57 58 /** 59 * 关闭数据库的连接 60 * 61 * @param con 62 * @param ps 63 */ 64 public void close(PreparedStatement ps, ResultSet rs) 65 close(null, ps, rs); 66 67 68 /** 69 * 关闭数据库的连接 70 * 71 * @param con 72 * @param ps 73 * @param rs 74 */ 75 public void close(Connection con, PreparedStatement ps, ResultSet rs) 76 try 77 if (rs != null) 78 rs.close(); 79 80 if (ps != null) 81 ps.close(); 82 83 if (con != null) 84 con.close(); 85 86 catch (Exception e) 87 throw new RuntimeException(e); 88 89 90 91 public int a(String sql, Object... objects) 92 Connection con = null; 93 PreparedStatement ps = null; 94 try 95 con = getcon(); 96 ps = con.prepareStatement(sql); 97 for (int i = 0; i < objects.length; i++) 98 if (objects[i] != null) 99 ps.setObject((i + 1), objects[i]); 100 101 102 return ps.executeUpdate(); 103 catch (Exception e) 104 throw new RuntimeException(e); 105 finally 106 close(con, ps); 107 108 109
1 package com.page; 2 3 /** 4 * 分页工具类 5 * 6 */ 7 public class PageBean 8 9 private int page = 1;// 页码 10 11 private int rows = 10;// 页大小 12 13 private int total = 0;// 总记录数 14 15 private boolean pagination = true;// 是否分页 16 17 public PageBean() 18 super(); 19 20 21 public int getPage() 22 return page; 23 24 25 public void setPage(int page) 26 this.page = page; 27 28 29 public int getRows() 30 return rows; 31 32 33 public void setRows(int rows) 34 this.rows = rows; 35 36 37 public int getTotal() 38 return total; 39 40 41 public void setTotal(int total) 42 this.total = total; 43 44 45 public void setTotal(String total) 46 this.total = Integer.parseInt(total); 47 48 49 public boolean isPagination() 50 return pagination; 51 52 53 public void setPagination(boolean pagination) 54 this.pagination = pagination; 55 56 57 /** 58 * 获得起始记录的下标 59 * 60 * @return 61 */ 62 public int getStartIndex() 63 return (this.page - 1) * this.rows; 64 65 66 @Override 67 public String toString() 68 return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]"; 69 70 71
1 package com.dao.impl; 2 3 import java.sql.ResultSet; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import com.dao.IBookDao; 8 import com.entity.BookBean; 9 import com.page.ICallBack; 10 import com.page.PageBean; 11 import com.page.PageDao; 12 13 public class BookDaoImpl extends PageDao<BookBean> implements IBookDao 14 15 @Override 16 public List<Boolean> findBookListByBookNamePage(PageBean pageBean, BookBean Bean) 17 StringBuffer sb=new StringBuffer(); 18 sb.append("select * from t_book"); 19 if (Bean.getBookName().length()>0) 20 sb.append(" where bookname like ‘%"+Bean.getBookName()+"%‘"); 21 22 return executeQueryPage(sb.toString(), pageBean, new ICallBack<BookBean>() 23 List<BookBean> booklist=new ArrayList<>(); 24 @Override 25 public List<BookBean> forEach(ResultSet rs) throws Exception 26 while(rs.next()) 27 BookBean b=new BookBean(); 28 b.setId(rs.getInt("id")); 29 b.setBookName(rs.getString("bookname")); 30 b.setPrice(rs.getDouble("price")); 31 b.setBooktype(rs.getInt("booktype")); 32 b.setCreateDate(rs.getDate("createdate")); 33 booklist.add(b); 34 35 return booklist; 36 37 38 ); 39 40 41 @Override 42 public int add(BookBean bookBean) 43 // TODO Auto-generated method stub 44 return executeQueryPage(sql, pageBean, iCallBack) 45 46 47
以上是关于通用分页的主要内容,如果未能解决你的问题,请参考以下文章