/**
* TOP查询
* @param sql String
* @param top int
* @return List
*/
public List findTop(String sql, int top) {
HibernateTemplate ht = this .getHibernateTemplate();
ht.setMaxResults(top);
return ht.find(sql);
}
/**
* 分页查询
* @param sql String
* @param firstRow int
* @param maxRow int
* @return List
*/
public List findPage( final String sql, final int firstRow, final int maxRow) {
return this .getHibernateTemplate().executeFind( new HibernateCallback(){
public Object doInHibernate(Session session) throws SQLException,
HibernateException {
Query q = session.createQuery(sql);
q.setFirstResult(firstRow);
q.setMaxResults(maxRow);
return q.list();
}
});
}
* TOP查询
* @param sql String
* @param top int
* @return List
*/
public List findTop(String sql, int top) {
HibernateTemplate ht = this .getHibernateTemplate();
ht.setMaxResults(top);
return ht.find(sql);
}
/**
* 分页查询
* @param sql String
* @param firstRow int
* @param maxRow int
* @return List
*/
public List findPage( final String sql, final int firstRow, final int maxRow) {
return this .getHibernateTemplate().executeFind( new HibernateCallback(){
public Object doInHibernate(Session session) throws SQLException,
HibernateException {
Query q = session.createQuery(sql);
q.setFirstResult(firstRow);
q.setMaxResults(maxRow);
return q.list();
}
});
}
本文介绍了一种使用Hibernate进行数据库分页查询和获取指定数量记录的方法。通过具体代码示例展示了如何设置查询的起始行数及最大返回行数,从而实现高效的数据检索。

被折叠的 条评论
为什么被折叠?



