ssh分页2

1、分页的实体Bean

@Repository
public class PageList {
@Autowired//注释调用service
private ToolService toolservice;
@Autowired
private Fenye fenye;
private int nowpage = 1;//当前页
private int countpage = 1;//总页数
private int countList = 1;//总记录数
private int begin = 0;//开始条数
private int pageNum = 4;//每一页显示条数


public List getpageList(String hql,String nowpage){
  int n = 1;
  try{
    n = Integer.parseInt(nowpage);
  }catch(Exception e){
   n = 1;
  }
  
  int listnum = this.toolservice.getPageNum(hql);
     this.setNowpage(n);
     this.setCountList(listnum);
       this.returnNowpage();
  List list = this.toolservice.getPageList(hql, this.begin, this.pageNum);
     return list;
}
/**
  * 得到返回的页数
  */
public void returnNowpage(){
  if(this.nowpage >= this.countpage){
   if(this.countpage> 1){
    this.begin = (this.countpage - 1) * this.pageNum;
    this.nowpage = this.countpage;
   } else {
    this.begin = 0;
    this.nowpage = 1;
   }
  } else {
   if (this.nowpage <= 1) {
    this.begin = 0;
    this.nowpage = 1;
   } else {
     this.begin = (this.nowpage - 1) * this.pageNum;

   }
  }
 
}

public ToolService getToolservice() {
  return toolservice;
}
public void setToolservice(ToolService toolservice) {
  this.toolservice = toolservice;
}
public int getNowpage() {
  return nowpage;
}
public void setNowpage(int nowpage) {
  this.nowpage = nowpage;
}

public Fenye getFenye() {
  return fenye;
}
public void setFenye(Fenye fenye) {
  this.fenye = fenye;
}

public int getCountpage() {
  return countpage;
}
public void setCountpage(int countpage) {
  this.countpage = countpage;
}
public int getCountList() {
  return countList;
}
public void setCountList(int countList) {
  this.countList = countList;
  this.countpage = (this.countList -1)/this.pageNum + 1;//计算总页数
}

public int getBegin() {
  return begin;
}
public void setBegin(int begin) {
  this.begin = begin;
}
public int getPageNum() {
  return pageNum;
}
public void setPageNum(int pageNum) {
  this.pageNum = pageNum;
}

}

2、DAO层的方法

A:

public List findTermEmploy(String hql, int begin,int last) {
 
  Session session = this.hibernatetemplate.getSessionFactory().openSession();
  List list1 = session.createQuery(hql).setFirstResult(begin).setMaxResults(last).list();
  List list = new ArrayList();
  int sessionclose = 0;//控制session是否关闭
   for(int i=0;i<list1.size();i++){
    Employee employee = (Employee) list1.get(i);
    list.add(list1.get(i));
    sessionclose++;
   }
   if(sessionclose >= list1.size()){
    session.close();
   }

  return list;
}

B:

public List findByPage(final String hql, final Object value,final int offset, final int pageSize) {

  List list=hibernateTemplate
  .executeFind(new HibernateCallback()
  {

   public Object doInHibernate(org.hibernate.Session session)
     throws HibernateException, SQLException {
    List result=session.createQuery(hql)
    .setParameter(0, value)
    .setFirstResult(offset)
    .setMaxResults(pageSize)
    .list();
    return result;
   }
  });
 
  return list;

}

3、在Action里面用注释的方式注入分页Bean

调用它的的getpageList(String hql,String nowpage)方法就好了,传入HQL语句和要寻找的页码

4、在JSP页面里,

        上一页:nowpage =${nowpage + 1}

        下一页:nowpage =${nowpage - 1}

         首 页:nowpage = 1;

        尾   页:nowpage = pageCount;//总页数

你可能感兴趣的:(DAO,bean,Hibernate,jsp,ssh)