mysql分页语句很简单:
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();
}
});
}