//每页显示10条记录,显示书籍信息的第二页 public static void main(String[] args){ Session session=HibernateSessionFactory.getSession(); String hql="from Books", Query query=session.createQuery(hql); int pageSize=10; int index=2; //起始位置(页码-1)*页大小 query.setFirstResult((index-1)*pageSize); //页大小 query.setMaxResults(pageSize); List<Books> list=query.list(); for(Books books:list){ System.out.println(books.getTitle()); } HibernateSessionFactory.closeSession(); } //子查询 查询出从来没有购买任何书籍的用户
public static void main(String[] args){ Session session=HibernateSessionFactory.getSession(); String hql="select u from Users u where u not in(select o.users from Orders o)"; Query query=session.createQuery(hql); List<Users> list=query.list(); for(Users user:list){ System.out.println(user.getName()); } HibernateSessionFactory.closeSession(); }
// 在HQL中,子查询必须出现在where子句中,并且必须包含在一对括号中。如果查询订购了两次以上的客户的hql语句
String hql="select u from Users u where(select count(*) from u.orderses)>2";
hibernate数据库分页查询
最新推荐文章于 2018-06-09 11:21:09 发布