今天閑來無事,擺弄了一下分頁,突然發現很多代碼長時間不用就生梳了,雖然有些基礎,但沒有一篇整合的。這里還是簡單示例,主要是以后自己翻着看不用百度找啊找找不到一篇想要的
1、PageBean實體類,一頁出的內容全都有
1 package entity; 2 3 import java.util.List; 4 5 /** 6 * 定義一個分頁對象 7 * @author 0 8 * 9 */ 10 public class PageBean<T> { 11 private int pageNo;//當前頁碼 12 private int totalPageCount;//總頁碼 13 private int totalCount;//總條數 14 private int pageSize=3;//每頁顯示條數 15 private int upPageNo;//上一頁 16 private int nextPageNo;//下一頁 17 //一頁返回的數據集合 18 private List<?> list; 19 public int getPageNo() { 20 return pageNo; 21 } 22 public void setPageNo(int pageNo) { 23 //如果當前頁碼大於0,才設置當前頁碼值 24 if(pageNo>0){ 25 this.pageNo=pageNo; 26 } 27 } 28 public int getTotalPageCount() { 29 return totalPageCount; 30 } 31 public void setTotalPageCount(int totalPageCount) { 32 if(this.getTotalCount()%this.pageSize==0){ 33 this.totalPageCount=this.getTotalCount()/this.pageSize; 34 }else if(this.getTotalCount()%this.pageSize >0){ 35 this.totalPageCount=this.getTotalCount()/this.pageSize +1; 36 }else{ 37 this.totalPageCount = 0; 38 } 39 } 40 public int getTotalCount() { 41 return totalCount; 42 } 43 public void setTotalCount(int totalCount) { 44 this.totalCount = totalCount; 45 } 46 public int getPageSize() { 47 return pageSize; 48 } 49 public void setPageSize(int pageSize) { 50 this.pageSize = pageSize; 51 } 52 public int getUpPageNo() { 53 return upPageNo; 54 } 55 //對上一頁進行判斷 56 public void setUpPageNo(int upPageNo) { 57 //如果當前頁>1 58 if(this.pageNo>1){ 59 this.upPageNo = this.pageNo-1; 60 } 61 } 62 63 public int getNextPageNo() { 64 return nextPageNo; 65 } 66 //對下一頁進行判斷 67 public void setNextPageNo(int nextPageNo) { 68 //如果當前頁>0且小於總頁數,則可以有下一頁 69 if(this.pageNo>0 && this.pageNo < this.totalPageCount){ 70 this.upPageNo = this.pageNo+1; 71 } 72 } 73 74 public List<?> getList() { 75 return list; 76 } 77 public void setList(List<?> list) { 78 this.list = list; 79 } 80 81 82 83 }
2、dao層實現類,這里框架用的hibernate。hibernate作dao層的數據處理,真是太方便了
1 package dao.impl; 2 3 4 import java.util.List; 5 6 import org.hibernate.Query; 7 import org.hibernate.Session; 8 9 import util.HibernateSessionFactory; 10 11 import dao.InfoMationDao; 12 import entity.Info; 13 import entity.PageBean; 14 15 public class InfoMationDaoImpl implements InfoMationDao { 16 17 public PageBean<Info> getInfoByPage(int pageNo, String where) { 18 Session session=HibernateSessionFactory.getSession(); 19 PageBean<Info> pagebean=new PageBean<Info>(); 20 21 try { 22 23 //求數據總量 24 int totalCount = ((Number) session.createQuery("select count(*) as count from Info where 1=1 "+where).uniqueResult()).intValue() ; 25 //總數、當前頁、總頁數 26 pagebean.setTotalCount(totalCount); 27 pagebean.setPageNo(pageNo); 28 pagebean.setTotalPageCount(totalCount/pagebean.getPageSize()); 29 pagebean.setUpPageNo(pageNo-1); 30 pagebean.setNextPageNo(pageNo+1); 31 32 //建立查詢 33 Query query = session.createQuery("from Info where 1=1 "+where); 34 //設置起始行pageSize*pageNo,(pageNo-1)*pageSize 35 query.setFirstResult((pageNo-1)*pagebean.getPageSize()); 36 //設置每頁條數 37 query.setMaxResults(pagebean.getPageSize()); 38 List<Info> infolist = query.list(); 39 pagebean.setList(infolist); 40 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 return pagebean; 45 } 46 47 /* public static void main(String[] args) { 48 InfoMationDao im=new InfoMationDaoImpl(); 49 int pageno=1; 50 String where=""; 51 PageBean<Info> pb = im.getInfoByPage(pageno, where); 52 System.out.println(pb.getList().size()); 53 }*/ 54 }
so easy!
