现在的位置: 首页 > 综合 > 正文

SSH2分页

2013年10月13日 ⁄ 综合 ⁄ 共 3599字 ⁄ 字号 评论关闭

分页类:

package com.seekLove.service;

import java.util.List;

import com.seekLove.modles.Blog;

 

public class BlogPage {
 private List<Blog> list;
 private int allRow;
 private int totalPage;
 private int currentPage;
 private int pageSize;
 private boolean isFirstPage;
 private boolean isLastPage;
 private boolean hasPreviousPage;
 private boolean hasNextPage;
 public List<Blog> getList() {
  return list;
 }
 public void init(){
  isFirstPage=isFirstPage();
  isLastPage=isLastPage();
  hasNextPage=isHasNextPage();
  hasPreviousPage=isHasPreviousPage();
 }
 public void setList(List<Blog> list) {
  this.list = list;
 }
 public int getAllRow() {
  return allRow;
 }
 public void setAllRow(int allRow) {
  this.allRow = allRow;
 }
 public int getTotalPage() {
  return totalPage;
 }
 public void setTotalPage(int totalPage) {
  this.totalPage = totalPage;
 }
 public int getCurrentPage() {
  return currentPage;
 }
 public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
 }
 public int getPageSize() {
  return pageSize;
 }
 public void setPageSize(int pageSize) {
  this.pageSize = pageSize;
 }
 public boolean isFirstPage() {
  return this.currentPage==1;
 }

 public boolean isLastPage() {
  return this.currentPage==totalPage;
 }

 public boolean isHasPreviousPage() {
  if(currentPage<1) currentPage=1;
  return this.currentPage>1;
 }
 
 public void setHasPreviousPage(boolean hasPreviousPage) {
  this.hasPreviousPage = hasPreviousPage;
 }
 public boolean getHasPreviousPage(){
  return this.hasPreviousPage;
 }
 public boolean isHasNextPage() {
  return  this.currentPage!=this.totalPage;
 }
 
    public static int countTotalPage(int pageSize,int allRow){
     return allRow%pageSize==0?allRow/pageSize:allRow/pageSize+1;
    }
  
      public static int countOffset(int pageSize,int currentPage){
       return pageSize*(currentPage-1);
      }
  
  public static int countCurrentPage(int page){
   return page==0?1:page;
  }
}

 

 

 

 

在接口里写上接口定义

public interface IBlog {

 public int getBlogAll(String hql);
 public List<Blog> getBlogPage(String hql,int star,int length);
 
}

 

接口的实现类:

package com.seekLove.DAOImpl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.seekLove.DAO.IBlog;
import com.seekLove.modles.Blog;
import com.seekLove.modles.BlogComment;
import com.seekLove.service.BlogPage;

@Component
public class BlogImpl implements IBlog {

 private HibernateTemplate hibernateTemplate;
 private BlogCommentImpl blogCommentImpl;
 private BlogPage blogPage;
 
 主要实现代码
 public int getBlogAll(String hql){
  return hibernateTemplate.find(hql).size();
 }
 public List<Blog> getBlogPage(String hql,int star,int length){
  SessionFactory sessionFactory=hibernateTemplate.getSessionFactory();
  Session session=sessionFactory.getCurrentSession();
  session.beginTransaction();
  Query query=session.createQuery(hql);
  query.setFirstResult(star);
  query.setMaxResults(length);
  return (List<Blog>)query.list();
 }

}

 

 

在service层的调用:

public BlogPage getBlogPages(int page,int pageSize){
  
  String hql="from Blog b where b.authourId.id=1 order by b.publishTime desc";
  //String hql="from Blog b where b.authourId.id="+((MemberInfo)session.get("memberInfo")).getId()+" order by b.publishTime desc";
  int allRow=blogImpl.getBlogAll(hql);
     int totalPage=BlogPage.countTotalPage(pageSize, allRow);
     int offset=BlogPage.countOffset(pageSize,page);
     int currentPage=BlogPage.countCurrentPage(page);
  List<Blog> list=blogImpl.getBlogPage(hql, offset,pageSize); 
     blogPage.setList(list);
     blogPage.setAllRow(allRow);
     blogPage.setCurrentPage(currentPage);
     blogPage.setPageSize(pageSize);
     blogPage.setTotalPage(totalPage);
     blogPage.init();
     return blogPage;
 }

 

 

抱歉!评论已关闭.