hibernate 分页查询

来源:互联网 发布:品茗网络计划逻辑 编辑:程序博客网 时间:2024/06/16 01:14

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

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 blockif (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 stubint 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; }}   


原创粉丝点击