您的位置:首页 > 数据库

Java SSH Hibernate通过代码实现从数据库中随机取出指定条数的随机数据

2017-10-18 00:00 676 查看
/**
* 根据提供的数量获取随机的问题列表
* @param totel
* @return
*/
public List getRadomQuestionList(final Integer tot){
List res = null;
final Integer totel = tot == null ? new Integer(1) : tot;
res = baseDao.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session s) {
Criteria c = s.createCriteria(Question.class);
int resSize =c.list().size();
Random r=new Random();
int n = resSize - totel.intValue();
return c.setFirstResult(r.nextInt(n)+1).setMaxResults(totel.intValue()).list();
}
});
if(null != res && res.size() > 0){
for (int i = 0; i < res.size(); i++) {
Question ques = (Question)res.get(i);
ques = this.getRadomQuestionAndAnswerByQuestion(ques);
}
}
return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: