Hibernate- 分页查询

01.搭建环境

02.分页查询

package com.gordon.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.gordon.domain.Book;
import com.gordon.utils.HibernateUtil;

/**
 * 分页查询
 * @author Administrator
 */
public class TestDemo3 {
	/**
	 * 分页查查询
	 * 查询结果:
		Hibernate: 
		    select
		        book0_.id as id1_0_,
		        book0_.name as name2_0_,
		        book0_.price as price3_0_,
		        book0_.publisher_id as publishe4_0_ 
		    from
		        t_book book0_ 
		    order by
		        book0_.price desc limit ?
		云计算技术及性能优化
		架构探险:轻量级微服务架构(下册)
		中国冰雪梦
	 */
	@Test
	public void run1() {
		Session session = HibernateUtil.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		String hql = "from Book b order by b.price desc";
		Query query = session.createQuery(hql);
		
		query.setFirstResult(0);// 从哪一个对象开始查询,起始位置为0
		query.setMaxResults(3);// 查询出几个对象
		
		List<Book> list = query.list();
		for (Book book : list) {
			System.err.println(book.getName());
		}
		
		transaction.commit();
	}
}
原文地址:https://www.cnblogs.com/hfultrastrong/p/7421343.html