1、使用SSH框架(配置环境);
2、编写实现的基础类BaseDao:
package com.accp.jboa.dao;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.accp.jboa.utils.PaginationSupport;
public class BaseDao extends HibernateDaoSupport {
/**********省略部分代码*******************/
/** 根据hql查询分页数据 */
public <T> void getPageDatasByHql(final PaginationSupport<T> pageBean,
final String hql) {
this.getPageDatasByHql(pageBean, hql, null);
}
public <T> void getPageDatasByHql(final PaginationSupport<T> pageBean,
final String hql, final Object[] paras) {
super.getHibernateTemplate().execute(new HibernateCallback() {
@SuppressWarnings("unchecked")
public Object doInHibernate(Session ses) throws HibernateException,
SQLException {
Query query = ses.createQuery(hql);
if (paras != null) {
for (int i = 0; i < paras.length; i++) {
query.setParameter(i, paras[i]);
}
}
query.setFirstResult((pageBean.getPageIndex() - 1)
* pageBean.getPageSize());
query.setMaxResults(pageBean.getPageSize());
pageBean.setPageDatas(query.list());
return null;
}
});
}
}
3、编写相应接口和实现,并继承自BaseDao;
4、具体的实现类调用实现不同的分页。