1、oracle的分页
PageMode类:
- public class PageModel {
- private List list;
- private int totalRecords;
- private int pageSize;
- private int pageNo;
- /**
- * 总页数
- * @return
- */
- public int getTotalPages() {
- return (this.totalRecords + this.pageSize - 1)/this.pageSize;
- }
- /**
- * 取得首页
- * @return
- */
- public int getTopPageNo() {
- return 1;
- }
- /**
- * 上一页
- * @return
- */
- public int getPreviousPageNo() {
- if (this.pageNo <= 1){
- return 1;
- }
- return this.pageNo - 1;
- }
- /**
- * 下一页
- * @return
- */
- public int getNextPageNo() {
- if (this.pageNo >= this.getBottomPageNo()) {
- return this.getBottomPageNo();
- }
- return this.pageNo + 1;
- }
- /**
- * 尾页
- * @return
- */
- public int getBottomPageNo() {
- return this.getTotalPages();
- }
- public List getList() {
- return list;
- }
- public void setList(List list) {
- this.list = list;
- }
- public int getTotalRecords() {
- return totalRecords;
- }
- public void setTotalRecords(int totalRecords) {
- this.totalRecords = totalRecords;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getPageNo() {
- return pageNo;
- }
- public void setPageNo(int pageNo) {
- this.pageNo = pageNo;
- }
- }
Oracle分页,SQL语句3层嵌套,Mysql则使用limit完成,更加的方便
- select * from
- (
- select rownum rn, t.* from
- (
- select * from t_user where user_id<>'root' order by user_id
- ) t where rownum <=?
- ) where rn>?;
具体分页方法:
- /**
- * 分页查询
- * @param pageNo
- * @param pageSize
- * @return
- */
- public PageModel findAllUser(int pageNo, int pageSize) {
- StringBuffer sbSql = new StringBuffer();
- sbSql.append("select * from ")
- .append("(")
- .append("select rownum rn, t.* from ")
- .append("(")
- .append("select * from t_user where user_id<>'root' order by user_id ")
- .append(") t where rownum <=?")
- .append(") where rn>?");
- Connection conn = null;
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- PageModel pageModel = null;
- try {
- conn = DB.getConnection();
- pstmt = conn.prepareStatement(sbSql.toString());
- pstmt.setInt(1, pageNo * pageSize);
- pstmt.setInt(2, (pageNo -1)*pageSize);
- rs = pstmt.executeQuery();
- List userList = new ArrayList();
- while (rs.next()) {
- User user = new User();
- user.setUserId(rs.getString("user_id"));
- user.setUserName(rs.getString("user_name"));
- user.setPassword(rs.getString("password"));
- user.setContactTel(rs.getString("contact_tel"));
- user.setEmail(rs.getString("email"));
- user.setCreateDate(rs.getTimestamp("create_date"));
- userList.add(user);
- }
- pageModel = new PageModel();
- pageModel.setPageSize(pageSize);
- pageModel.setPageNo(pageNo);
- pageModel.setList(userList);
- pageModel.setTotalRecords(getTotalRecords(conn));
- }catch(SQLException e) {
- e.printStackTrace();
- }finally {
- DB.close(rs);
- DB.close(pstmt);
- DB.close(conn);
- }
- return pageModel;
- }
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/678353,如需转载请自行联系原作者