hibernate hql语句实现随机抽取10条数据库数据
1个回答
展开全部
public void randResultSet() {
HashSet<Integer> set = new HashSet<Integer>();
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session s = sf.openSession();
List<user> list = s.createQuery("from user").list();
int maxSize = list.size();
while (true) {
int randNumber = (int) (Math.random() * maxSize + 1) - 1;
set.add(randNumber);
if (set.size() >= 10) {
break;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int a : set) {
user u = (user) list.get(a);
System.out.println(u.getName());
}
}
HashSet<Integer> set = new HashSet<Integer>();
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session s = sf.openSession();
List<user> list = s.createQuery("from user").list();
int maxSize = list.size();
while (true) {
int randNumber = (int) (Math.random() * maxSize + 1) - 1;
set.add(randNumber);
if (set.size() >= 10) {
break;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int a : set) {
user u = (user) list.get(a);
System.out.println(u.getName());
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询