hibernate 分页查询

下面例子中 实现了分页查询  另外一些方法封装好的  没有实现  有兴趣的  可以实现下  其他的功能

pageInfoBean

public class PagerInfoBean {

	private int totalNum;// 总条数
	private int firstResult = 0;
	private int maxResults = 20;
	private int totalPage;

	/**
	 * 总页数
	 * 
	 * @return
	 */
	public int getTotalPage() { 
		if (totalNum == 0) {    
			return 0;     
		} else {     
			return (totalNum + maxResults - 1) / maxResults;
		}  
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}

	public int getFirstResult() {
		return firstResult;
	}

	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public int getMaxResults() {
		return maxResults;
	}

	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}

}


BasicDAO

public interface BasicDAO { 

	public boolean saveOrUpdate(Object o);

	/**
	 * Method queryData 查询数据
	 * @param hql HQL查询语句
	 * @return List 集合
	 */
	public List<?> searchData(String hql);
	
	@SuppressWarnings("unchecked")
	public Object get(Class clz, Serializable id);

	@SuppressWarnings("unchecked")
	public boolean delete(Class clz, Serializable id);
	
	public int getRowCount(String hql);
}

BasicDAOImpl

import java.io.Serializable;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import pojo.HibernateSessionFactory;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class BasicDAOImpl implements BasicDAO {
	private SessionFactory sessionFactory ; 
	public SessionFactory getSessionFactory() {
		return sessionFactory;   
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/** 
	 * @author 
	 * @param o
	 *            要保存的对象
	 * @param boolean
	 *            返回结果
	 */ 
	public boolean saveOrUpdate(Object o) {
		Session session = sessionFactory.openSession();
		Transaction t = null; 
		try {
			t = session.beginTransaction();
			session.saveOrUpdate(o);
			t.commit();
			return true;
		} catch (HibernateException e) {
			if (t != null) {
				t.rollback();
			}
			e.printStackTrace();
			return false;
		}finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
	}

	public List<?> searchData(String hql) {
		Session session = sessionFactory.openSession();
		List<?> list = null;
		try {
			Query q = session.createQuery(hql);
			list = q.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally {
			if (session != null) {  
				if (session.isOpen()) {
					session.close(); 
				}
			}
		}
		return list;
	}
	/**
	 * 分页查询
	 * @param hql
	 * @param pager
	 * @return
	 */
	public List<?> searchData(String hql, PagerInfoBean pager) {
		SessionFactory sessionFactory =HibernateSessionFactory.getSessionFactory();
		Session session = sessionFactory.openSession(); 
		List<?> list = null;
		try {
			Query q = session.createQuery(hql);
			list = q.setFirstResult(pager.getFirstResult()).setMaxResults(
					pager.getMaxResults()).list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public boolean delete(Class clz, Serializable id) {
		boolean result = false;
		Session session = sessionFactory.openSession();
		Transaction t = null;
		try {
			t = session.beginTransaction();
			if (this.get(clz, id)!=null) {
				session.delete(this.get(clz, id));
			}			
			t.commit();
			result = true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if (null != t) {
				t.rollback();
			}
			result = false;
			e.printStackTrace();
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
		return result;
	}
	@SuppressWarnings("unchecked")
	public Object get(Class clz, Serializable id) {
		Object o = null;
		Session session = sessionFactory.openSession();
		try {
			o = session.get(clz, id);
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
		return o;
	}

	/**
	 * 总记录数
	 */
	public int getRowCount(String hql) {  
		// TODO Auto-generated method stub
		int rowCount=0;
		Session session = sessionFactory.openSession();
		try {
			Query q = session.createQuery(hql);
			if (q.uniqueResult()!=null) {
				rowCount = Integer.parseInt(q.uniqueResult().toString());
			}			
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
		return rowCount;
	}

}

BranchInfoDAOImpl

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.HibernateSessionFactory;
import pojo.TblArea;

public class BranchInfoDAOImpl extends BasicDAOImpl{
	
	public static void main(String[] args) {    
		PagerInfoBean pagers =new PagerInfoBean();
		pagers.setFirstResult(3);
        pagers.setMaxResults(9);
        pagers.setTotalNum(10);
		BranchInfoDAOImpl branchInfoDAOImpl =new BranchInfoDAOImpl();
		List<TblArea> branchList=branchInfoDAOImpl.getBranchInfo(pagers); 
		for (TblArea tblArea : branchList) {
			String areaId =tblArea.getAreaId();
			String areaName=tblArea.getAreaName();
			System.out.println("areaId"+areaId); 
			System.out.println("areaName"+areaName);
		}
	}     
	/**
	 * 分页查询
	 */
	public List<TblArea> getBranchInfo(PagerInfoBean pager) {
		String hql = "from TblArea";   
		List<TblArea> branchList = (List<TblArea>) super.searchData(hql,pager); 
		return branchList; 
	}
}   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值