一个简洁的分页实现(三)

类别:Java 点击:0 评论:0 推荐:

我开始就是为了在Hibernate中使用分页才设计这个分页实现的,因为使用Hibernate时,查询后的结果被自动封装到一个List中了,所以使用起来特别方便,这里我做了一个比较庸俗的实现,就是查询参数只适合字符串类型,不过大部分查询还真的只是对字符串操作。

package treeroot.util;

import net.sf.hibernate.HibernateException;
import treeroot.common.dao.AbstractBaseDao;

/**
* @author treerot
* @version 1.0
* @since 2004-9-30
*/
public class HibernatePage extends AbstractPage implements Pageable
{
  private String querySql;
  private String countSql;
  private String[] parameters;

  public HibernatePage(int currentPage,String querySql,String countSql,String[] parameters) throws PageException{
    this(currentPage,Pageable.DEFAULT_PAGESIZE,querySql,countSql,parameters);
  }
  public HibernatePage(int currentPage,int pageSize,String querySql,String countSql,String[] parameters) throws PageException
  {
    super(currentPage, pageSize);
    this.querySql = querySql;
    this.countSql = countSql;
    this.parameters = parameters;
    init();
  }
  protected void init() throws PageException
  {
    try{
        this.count = AbstractBaseDao.queryCount(countSql);
       int fromIndex = (this.getCurrentPage() - 1) * this.getPageSize();
       int toIndex = Math.min(this.count, fromIndex + this.getPageSize());
       this.result = AbstractBaseDao.find(this.querySql,this.parameters,fromIndex,toIndex);
    }
    catch (HibernateException e)
    {
       throw new PageException(e.getMessage());
    }
  }
}
这个类的设计并不是很合理的,因为查询只能接受字符串参数,但是如果只需要字符串参数就足够了。另外查询语句必须是JDBC风格的参数(?), 而不是Hibernate风格的(:=),你看过Dao里面的代码就知道为什么了。先看一下如何使用吧(一个Dao中的方法):

public Pageable findByName(String name,int currentPage) throws PageException{
  String countSql="select count(*) from MyClass as c where c.name like ?";
  String querySql="from MyClass as c where c.name like ?";
  String[] parameter=new String[]{name};
  return new HibernatePage(currentPage,countSql,querySql,parameter);
}

这个方法应该是比较简洁的,这里给出queryCount和find的实现,我对Hibernate的了解比较肤浅,所以下面的方法如果有什么不当的地方还望指出,谢谢!

public static int queryCount(String hql, String[] args) throws HibernateException
{

  if (hql == null) throw new NullPointerException();
  Object obj = null;
  Transaction trans = null;
  Session s = null;
  try
  {
    s = HibernateSessionFactory.currentSession();
    trans = s.beginTransaction();
    Query q = s.createQuery(hql);
    if (args != null)
    {
      for (int i = 0; i < args.length; i++){
        q.setString(i, args[i]);
      }
    }
    obj = q.uniqueResult();
    trans.commit();
  }
  catch (HibernateException e)
  {
    if (trans != null)
    {
      try{
        trans.rollback();
      }
      catch (HibernateException ex){//no need to care this Exception }
    }
    throw e;
  }
  return ((Integer) obj).intValue();
}

public static List find(String hql, String[] args, int fromIndex, int toIndex) throws HibernateException
{
  if (hql == null) throw new NullPointerException();
  List l = null;
  Transaction trans = null;
  Session s = null;
  try{
    s = HibernateSessionFactory.currentSession();
    trans = s.beginTransaction();
    Query q = s.createQuery(hql);
    if (args != null){
      for (int i = 0; i < args.length; i++){
        q.setString(i, args[i]);
      }
    }
    if (fromIndex > -1){
       if (toIndex > fromIndex){
         q.setFirstResult(fromIndex);
         q.setMaxResults(toIndex - fromIndex);
       }
       else{
         throw new IndexOutOfBoundsException();
       }
    }
    l = q.list();
    trans.commit();
   }
   catch (HibernateException e){
     if (trans != null){
       try{
         trans.rollback();
       }
       catch (HibernateException ex){ //no need to care this Exception }
     }
     throw e;
   }
   return l;
}

本文地址:http://com.8s8s.com/it/it15030.htm