Hibernate实现sql中的limit的方法也很简单,原理是创建一个查询之后,给当前查询添加start和limit两个参数,之后执行查询即可。代码如下所示。
public List<KnowledgeExpertArticle> findKnowledgeExpertArticleForSearch(
String keyName, Object value,final int start,final int limit) {
//
log.debug("search knowledgeExpertArticle by :"+value);
try{
final String hql = "form KnowledgeExpertArticle as model where model.'"+keyName+"' like '%"+value+"%' order by model.expertArticleId DESC";
log.info(hql);
List resultList = getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session arg0)
throws HibernateException, SQLException {
Query query = arg0.createQuery(hql);
query.setFirstResult(start);
query.setMaxResults(limit);
return query.list();
}
});
return resultList;
}catch(RuntimeException re){
throw re;
}
}