HQL和QBC的Query,Criteria接口都提供分页查询设置,SQL检索方式使用所对应的数据库分页方式进行分页,
oracle用rownum,mysql用limit.
主要靠以下方法:
setFirstResult(firstResult):从什么对象位置开始检索,默认参数值为0
setMaxResults(maxResults):最多检出的数据条数,有多少检出多少,但是不能超过最大检出数据,设定的检出数据上限值。
1.HQL分页查询实例:
package com.lanhuigu.hibernate.test; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.lanhuigu.hibernate.entity.Customer; public class TestHQL { public static void main(String[] args) throws Exception{ Configuration cfg = new Configuration().configure(); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tr = session.beginTransaction(); //1.创建Query对象与 Query query = session.createQuery("from Customer order by name desc");//降序,desc不可以去掉,去掉就升序了 //2.命名参数动态绑定查询条件 //query.setString("v_name", "test"); //3.分页 query.setFirstResult(0);//从什么位置开始,默认为0 query.setMaxResults(1);//最多检出的条数 //4.执行SQL List list = query.list(); //5.输出结果 for (int i = 0;i<list.size();i++) { Customer customer = (Customer) list.get(i); System.out.println(customer.getName()); } //6.事务提交 tr.commit(); //7.关闭session session.close(); } }执行结果:
Hibernate: select customer0_.ID as ID1_0_, customer0_.NAME as NAME2_0_, customer0_.EMAIL as EMAIL3_0_, customer0_.PASSWORD as PASSWORD4_0_, customer0_.PHONE as PHONE5_0_, customer0_.ADDRESS as ADDRESS6_0_, customer0_.SEX as SEX7_0_, customer0_.IS_MARRIED as IS8_0_, customer0_.DESCRIPTION as DESCRIPT9_0_, customer0_.IMAGE as IMAGE10_0_, customer0_.BIRTHDAY as BIRTHDA11_0_, customer0_.REGISTERED_TIME as REGISTE12_0_, customer0_.HOME_PROVINCE as HOME13_0_, customer0_.HOME_CITY as HOME14_0_, customer0_.HOME_STREET as HOME15_0_, customer0_.HOME_ZIPCODE as HOME16_0_, customer0_.COMP_PROVINCE as COMP17_0_, customer0_.COMP_CITY as COMP18_0_, customer0_.COMP_STREET as COMP19_0_, customer0_.COMP_ZIPCODE as COMP20_0_ from CUSTOMERS customer0_ order by customer0_.NAME desc limit ? test
实例解释:
从0开始检索,最多检出1条。
2.QBC分页查询实例:
package com.lanhuigu.hibernate.test; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Expression; import org.hibernate.criterion.Order; import com.lanhuigu.hibernate.entity.Customer; public class TestQBC { public static void main(String[] args) throws Exception{ Configuration cfg = new Configuration().configure(); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tr = session.beginTransaction(); //1.创建Criteria对象 Criteria criteria = session.createCriteria(Customer.class); //2.Expression静态方法绑定查询条件,创建的每个Criterion实例就是一个条件 /*Criterion criterion = Expression.eq("name", "test"); criteria.add(criterion);*/ //3.排序 //criteria.addOrder(Order.asc("name"));//升序 criteria.addOrder(Order.desc("name"));//降序 //4.分页 criteria.setFirstResult(0);//从什么位置开始,默认为0 criteria.setMaxResults(1);//最多检出的条数 //5.执行SQL List list = criteria.list(); //6.输出结果 for (int i = 0;i<list.size();i++) { Customer customer = (Customer) list.get(i); System.out.println(customer.getName()); } //7.事务提交 tr.commit(); //8.关闭session session.close(); } }实例解释:
从0开始检索,最多检出1条
总结:
在执行结果控制台打印的sql中,结尾部分可以看到limit分页的身影,因为我用的mysql数据库。
分页的实质最后还是靠数据库。在以上执行的sql中,limit后面可以看到一个'?'(limit ?),因为我设定从0开始检索,数据库默认也是从0开始的,
所以,limit就只有一个参数,第一个参数默认为0。
但是,当我将setFirstResult(1)设置成1,表示从1开始检索时,这个时候再次执行,结果如下:
Hibernate: select customer0_.ID as ID1_0_, customer0_.NAME as NAME2_0_, customer0_.EMAIL as EMAIL3_0_, customer0_.PASSWORD as PASSWORD4_0_, customer0_.PHONE as PHONE5_0_, customer0_.ADDRESS as ADDRESS6_0_, customer0_.SEX as SEX7_0_, customer0_.IS_MARRIED as IS8_0_, customer0_.DESCRIPTION as DESCRIPT9_0_, customer0_.IMAGE as IMAGE10_0_, customer0_.BIRTHDAY as BIRTHDA11_0_, customer0_.REGISTERED_TIME as REGISTE12_0_, customer0_.HOME_PROVINCE as HOME13_0_, customer0_.HOME_CITY as HOME14_0_, customer0_.HOME_STREET as HOME15_0_, customer0_.HOME_ZIPCODE as HOME16_0_, customer0_.COMP_PROVINCE as COMP17_0_, customer0_.COMP_CITY as COMP18_0_, customer0_.COMP_STREET as COMP19_0_, customer0_.COMP_ZIPCODE as COMP20_0_ from CUSTOMERS customer0_ order by customer0_.NAME desc limit ?, ? hha明显能看到生成sql后limit后面有两个问号(limit ?, ?),就是用来接受我设置的分页的值,不是0,就不能按默认处理。