setFirstResult(int firstResult) 设置返回结果从第几条开始 ---- 索引从0开始
setMaxResults(int maxResults) 设置本次返回结果记录条数
hibernateHql.cfg.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 指定数据库所用到的驱动 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- 指定数据库链接的url,hibernate链接的数据库名 -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">scott</property>
<!-- 指定连接数据库的用户口令 -->
<property name="connection.password">123156</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!--格式化sql -->
<property name="format_sql ">true</property>
<!-- 打印sql 控制台-->
<property name="show_sql">true</property>
<!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 根据需要自动创建数据库表 -->
<property name="hbm2ddl.auto">update</property>
<!--关联小配置文件-->
<mapping resource="cn/day03hql/dynamic/entity/Emp.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
实体类
public class Emp {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Integer sal;
private Integer comm;
private Integer deptno;
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {return ename;}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public Integer getSal() {
return sal;
}
public void setSal(Integer sal) {
this.sal = sal;
}
public Integer getComm() {
return comm;
}
public void setComm(Integer comm) {
this.comm = comm;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
}
小配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 映射文件开始 -->
<hibernate-mapping package="cn.day03hql.dynamic.entity">
<!--表名称-->
<class name="Emp" table="EMP" schema="scott">
<!--列名-->
<id name="empno" column="EMPNO">
<!--主键生成的策略 native:自动生成主键字段-->
<generator class="native"></generator>
</id>
<property name="ename" column="ENAME"></property>
<property name="job" column="JOB"></property>
<property name="mgr" column="MGR"></property>
<property name="hiredate" column="HIREDATE"></property>
<property name="sal" column="SAL"></property>
<property name="comm" column="COMM"></property>
<property name="deptno" column="DEPTNO"></property>
</class>
</hibernate-mapping>
测试类
//分页查询
@Test
public void test02(){
//读取大配置文件
Configuration cfg=new Configuration().configure("hibernateHql.cfg.xml");
//session工厂
SessionFactory factory=cfg.buildSessionFactory();
//session对象
Session session = factory.openSession();
String hql="from Emp order by empno";
Query query = session.createQuery(hql);
int pageIndex=2;
int pageSize=3;
query.setFirstResult((pageIndex-1)*pageSize);
query.setMaxResults(pageSize);
/* query.setFirstResult(0);
query.setMaxResults(5);*/
List<Emp> list = query.list();
for (Emp item:list){
System.out.println(item.getEname());
}
}