一、在數據庫中存儲的數據量特別大時(百萬級以上),使用數據庫分頁技術,將已經分頁好的數據存儲在集合中返回給用戶
1 /** 2 * 查詢雇員(根據條件組合查詢-數據庫分頁) 3 * @param emp 4 * @param pageSize 每頁顯示條數 5 * @param pageNum 當前頁碼 6 * @return 7 */ 8 public List<Emp> getEmp(Emp emp, int pageSize, int pageNum) { 9 try { 10 11 String sql = "select * from ( select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,e.deptno,d.dname,d.loc,m.ename mgrname,rownum rn " 12 + " from emp e,dept d,emp m " 13 + " where e.mgr = m.empno(+) and e.deptno = d.deptno "; 14 15 16 //綁定查詢條件---start--------------------------------------------------- 17 //雇員編號 18 int empno= emp.getEmpno(); 19 if(empno > 0) 20 { 21 sql += " and e.empno like '%" + empno +"%' "; 22 } 23 //雇員姓名 24 String ename = emp.getEname(); 25 if(ename != null && !"".equals(ename)) 26 { 27 sql += " and e.ename like '%" + ename +"%' ";//模糊查詢 28 } 29 //職位 30 String job = emp.getJob(); 31 if(job != null && !"".equals(job)) 32 { 33 sql += " and e.job = '" + job +"' "; 34 } 35 //部門編號 36 int deptno = emp.getDept().getDeptno(); 37 if(deptno > 0) 38 { 39 sql += " and e.deptno = " + deptno; 40 } 41 //綁定查詢條件---end--------------------------------------------------- 42 43 sql += " and rownum <= "+ (pageSize * pageNum) +" ) where rn >= " + ((pageNum-1) * pageSize +1); 44 45 System.out.println(sql); 46 47 stat = conn.prepareStatement(sql); 48 49 rs = stat.executeQuery(); 50 51 List<Emp> empList = new ArrayList<Emp>(); 52 53 while(rs.next()) 54 { 55 Emp temp = new Emp(); 56 temp.setEmpno(rs.getInt("empno")); 57 temp.setEname(rs.getString("ename")); 58 temp.setJob(rs.getString("job")); 59 60 //上級主管(復合屬性) 61 Emp mgr = new Emp(); 62 mgr.setEmpno(rs.getInt("mgr")); 63 mgr.setEname(rs.getString("mgrname")); 64 //...... 65 temp.setMgr(mgr); 66 67 temp.setHiredate(rs.getDate("hiredate")); 68 temp.setSalary(rs.getDouble("sal")); 69 temp.setComm(rs.getDouble("comm")); 70 71 //部門(復合屬性) 72 Dept dept = new Dept(); 73 dept.setDeptno(rs.getInt("deptno")); 74 dept.setDname(rs.getString("dname")); 75 dept.setLocation(rs.getString("loc")); 76 temp.setDept(dept); 77 78 empList.add(temp); 79 } 80 return empList; 81 82 } catch (SQLException e) { 83 e.printStackTrace(); 84 } 85 return null; 86 } 87 88 }
二、當取得的數據不多時,推薦使用基於結果集的分頁技術,這樣可以盡量少的訪問數據庫
(1)建立基於結果集分頁的工具類(重要)
1 package Util; 2 3 import java.util.List; 4 5 import model.User; 6 7 public class PageUtil { 8 //總頁數 9 private int pageCount; 10 //總行數 11 private int rowCount; 12 //當前頁數,默認情況下為1 13 private int pageNum = 1; 14 //每頁條數,默認情況下為5 15 private int pageSize = 5; 16 //開始索引(重要) 17 private int beginIndex; 18 //結束索引(重要) 19 private int endIndex; 20 21 //構造函數 22 public PageUtil(List<User> user){ 23 //為總行數賦值 24 this.rowCount = user.size(); 25 //算出總頁數,並賦值 26 if(this.rowCount%this.pageSize==0){ 27 this.pageCount = this.rowCount/this.pageSize; 28 }else { 29 this.pageCount = this.rowCount/this.pageSize+1; 30 } 31 setPageNum(this.pageNum); 32 } 33 34 public int getPageCount() { 35 return pageCount; 36 } 37 38 public void setPageCount(int pageCount) { 39 this.pageCount = pageCount; 40 } 41 42 public int getRowCount() { 43 return rowCount; 44 } 45 46 public void setRowCount(int rowCount) { 47 this.rowCount = rowCount; 48 } 49 50 public int getPageNum() { 51 return pageNum; 52 } 53 54 public void setPageNum(int pageNum) { 55 this.pageNum = pageNum; 56 //計算開始索引 57 this.beginIndex = (this.pageNum-1)*this.pageSize; 58 //計算結束索引 59 this.endIndex = this.pageNum*this.pageSize; 60 if(this.endIndex>this.rowCount){ 61 this.endIndex = this.rowCount; 62 } 63 } 64 65 public int getPageSize() { 66 return pageSize; 67 } 68 69 public void setPageSize(int pageSize) { 70 this.pageSize = pageSize; 71 } 72 73 public int getBeginIndex() { 74 return beginIndex; 75 } 76 77 public void setBeginIndex(int beginIndex) { 78 this.beginIndex = beginIndex; 79 } 80 81 public int getEndIndex() { 82 return endIndex; 83 } 84 85 public void setEndIndex(int endIndex) { 86 this.endIndex = endIndex; 87 } 88 89 90 }
(2)dao層中通過方法取得滿足條件的所有待選行,通過服務層返回給servlet,在servlet中調用分頁工具類,取得開始索引和結束索引,在使用list的sublist方法將選中的候選行返回給用戶
1 String pageNum = request.getParameter("pageNum"); 2 HttpSession session = request.getSession(); 3 //當不指定具體是哪一頁時 4 if(pageNum==null||"".equals(pageNum)){ 5 //1.取得參數 6 String username = request.getParameter("username"); 7 String sex = request.getParameter("sex"); 8 String cert_type = request.getParameter("cert_type"); 9 String cert = request.getParameter("cert"); 10 String user_type = request.getParameter("user_type"); 11 System.out.println("servlet:"+username); 12 //2.封裝進對象 13 User user = new User(); 14 user.setUsername(username); 15 user.setSex(sex); 16 //如果對象需要轉化為int,那么為了防止使用Interger.valueof()方法時出現空指針情況,必須先進行判斷是否為空 17 if(cert_type != null && !"".equals(cert_type)) 18 { 19 user.setCert_type(Integer.valueOf(cert_type)); 20 } 21 user.setCert(cert); 22 if(user_type != null && !"".equals(user_type)) 23 { 24 user.setUser_type(Integer.valueOf(user_type)); 25 } 26 27 //3.調用服務層方法取得結果集 28 List<User> users = SelectUserService.Instance().getUser(user); 29 //4.對結果集進行分頁操作 30 PageUtil pu = new PageUtil(users); 31 List<User> selectUsers = users.subList(pu.getBeginIndex(), pu.getEndIndex()); 32 //結果集存入session中,可以讓下次指定頁數分頁時不用再重新用服務層 33 session.setAttribute("users", users); 34 //分頁好的結果存入session中 35 session.setAttribute("selectUsers", selectUsers); 36 //分頁信息存入session 37 session.setAttribute("pu", pu);
(3)當用戶選中某一頁時,取出事先存在session中的全部候選行,在調用存在session中的分頁工具類對象,進行指定當前頁數,再取得選中行
1 else { 2 //指定某一頁時,調用分頁工具類修改頁面 3 PageUtil pu = (PageUtil) session.getAttribute("pu"); 4 List<User> users = (List<User>) session.getAttribute("users"); 5 pu.setPageNum(Integer.valueOf(pageNum)); 6 List<User> selectUsers = users.subList(pu.getBeginIndex(), pu.getEndIndex()); 7 session.setAttribute("selectUsers", selectUsers); 8 session.setAttribute("pu", pu); 9 10 }