Hibernate的分页查询

用的是

query.setFirstResult();

query.setMaxResults();

两种方法来限定查询的范围。且不分数据库,对MySQL和Oracle都适用。分页查询时,先按正常的语句查,最后加上这两句就可以了,如:

public ArrayList getFaultPage(int currPage, int pageSize,
			Map map) {

		Session session = null;
		String name = map.get("name");
		Query query = null;
		if (name == null) {
			// 无条件查询
			session = sessionFactory.openSession();
			String hql = "from Fault f where f.statue=:statue";
			query = session.createQuery(hql);
			query.setParameter("statue", "待维修");
		} else {
			session = sessionFactory.openSession();
			String hql = "from Fault f where (f.devices.typeclass=:name or "
					+ "f.devices.address=:name or f.devices.brandname=:name or "
					+ "f.devices.buytime=:name or f.devices.classfiy=:name or "
					+ "f.devices.idnum=:name or f.devices.statue=:name or "
					+ "f.applytime=:name) and f.statue=:statue";
			query = session.createQuery(hql);
			query.setParameter("name", name);
			query.setParameter("statue", "待维修");
		}
		int b = (currPage - 1) * pageSize;
		query.setFirstResult(b);
		query.setMaxResults(pageSize);
		
		
		ArrayList list = (ArrayList) query.list();

		session.close();
		return list;
	}

你可能感兴趣的:(ORM框架Hibernate)