mysql分页语句很简单:
select * from table limit 开始索引,查寻数量;
当用hibernate时:
Query query = session.createQuery("from table limit 10,20");
这样有错,hibernate不能直接在hql里写limit,hibernate不识别limit,
解决如下:
select * from table limit 开始索引,查寻数量;
当用hibernate时:
Query query = session.createQuery("from table limit 10,20");
这样有错,hibernate不能直接在hql里写limit,hibernate不识别limit,
解决如下:
public List<User> getUserById(final int userId,final int maxCount,final int firstResult) throws Exception {
final String hql = "from User where userId=? ";
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session) throws HibernateException, SQLException {
final Query query = session.createQuery(hql);
query.setParameter(0, userId);
query.setMaxResults(maxCount);
query.setFirstResult(firstResult);
return query.list();
}
});
}

本文介绍如何在Hibernate中实现分页查询,替代MySQL的LIMIT指令。通过具体的Java代码示例展示了如何设置查询参数,如每页显示记录数和起始记录的位置。
766

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



