日期:2014-05-17  浏览次数:20854 次

新手遇到一个SSH2整合分页问题,请大侠帮帮我
在SSH2整合的时候居然不知道怎么分页,我够失败的,请大侠给我一个SSH2整合分页的案列吗,谢谢啦.............


------解决方案--------------------
public PageSupport loadPageBySql(final String sql, final int currentPage,
final int pageSize) {
PageSupport page = (PageSupport) getHTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql);
int totalCount = query.list().size();
int startIndex = currentPage * pageSize - pageSize;
if (startIndex <= 0) {
startIndex = 0;
}
if (startIndex >= totalCount) {
startIndex = totalCount;
}
query.setFirstResult(startIndex);
query.setMaxResults(pageSize);
List items = query.list();
if (items.isEmpty()) {
items = new ArrayList();
}
session.close();
PageSupport ps = new PageSupport(items, totalCount,pageSize, startIndex);
return ps;
}
});

return page;

}