import java.sql.SQLException;
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.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class SqlQuery extends HibernateDaoSupport {
/**
* 根据sql分页查询
* @param sql
* @param firstRow
* @param maxRow
* @return
*/
public List findPage(String sqlQuery,int first,int max) {
final int firstRow = first;
final int maxRow = max;
final String sql=sqlQuery;
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();
}
});
}
/**
* 根据sql查询前top条记录
* @param sql
* @param top
* @return
*/
public List findTop(String sql, int top) {
HibernateTemplate template = this.getHibernateTemplate();
template.setMaxResults(top);
return template.find(sql);
}
}