hibernate+pageBean实现分页dao层功能代码

今天闲来无事,摆弄了一下分页,突然发现很多代码长时间不用就生梳了,虽然有些基础,但没有一篇整合的。这里还是简单示例,主要是以后自己翻着看不用百度找啊找找不到一篇想要的

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!

 

posted @ 2017-01-22 16:39  sincoolvip  阅读(2335)  评论(0编辑  收藏  举报