5.1 准备工作
建立项目,加入jar
建立hibernate.cfg.xml
建立pojo类和对应的映射文件
5.2 建立vo类PageEntity
1 package org.guangsoft.vo; 2 /*** 3 * 4 * 定义vo封装自己需要的数据和数据库不存在映射关系 5 * 6 * ***/ 7 public class PageEntity 8 { 9 private Integer pagenum; // 当前页数10 private Integer pagesize; // 每页数据库量11 private Integer maxpage; // 最大页数12 private Integer rows; // 数据库对应的总行数13 public Integer getPagenum()14 {15 return pagenum;16 }17 public void setPagenum(Integer pagenum)18 {19 this.pagenum = pagenum;20 }21 public Integer getPagesize()22 {23 return pagesize;24 }25 public void setPagesize(Integer pagesize)26 {27 this.pagesize = pagesize;28 }29 public Integer getMaxpage()30 {31 return maxpage;32 }33 public void setMaxpage(Integer maxpage)34 {35 this.maxpage = maxpage;36 }37 public Integer getRows()38 {39 return rows;40 }41 public void setRows(Integer rows)42 {43 this.rows = rows;44 }45 }
5.3定义分页接口
1 package com.bjsxt.dao; 2 import java.util.List; 3 import com.bjsxt.pojo.Student; 4 import org.guangsoft.vo.PageEntity; 5 /*** 6 * 7 * 学生表的crud操作定义 8 * 9 * ***/10 public interface StudentDao11 {12 /***13 * 14 * 定义分页方法15 * 16 * 17 * 18 * ***/19 public ListselectStudentOfPage(PageEntity pe);20 /**21 * 22 * 获得总行数23 * 24 * ***/25 public Long selectCountRows();26 }
5.4建立接口的实现类
1 package org.guangsoft.dao.impl; 2 import java.util.List; 3 import org.hibernate.Criteria; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.cfg.Configuration; 7 import org.guangsoft.dao.StudentDao; 8 import org.guangsoft.pojo.Student; 9 import org.guangsogt.vo.PageEntity;10 public class StudentDaoImpl implements StudentDao11 {12 /**13 * 14 * 通过静态代码块加载配置文件15 * 16 * ****/17 static SessionFactory sf = null;18 static19 {20 // 1 创建Configuration对象,用来加载hibernate的配置文件21 Configuration cfg = new Configuration();22 // 2加载配置文件23 cfg.configure("hibernate.cfg.xml");24 // 3通过cfg构造一个会话工厂对象25 sf = cfg.buildSessionFactory();26 }27 @Override28 public ListselectStudentOfPage(PageEntity pe)29 {30 // TODO Auto-generated method stub31 /*32 * String hql="from Student";33 * 34 * //创建Query对象35 * 36 * Session session=sf.openSession();37 * 38 * Query q=session.createQuery(hql);39 * 40 * //设置分页参数41 * 42 * q.setMaxResults(pe.getPagesize()); //每页数据量43 * 44 * //查询的起始位置45 * 46 * q.setFirstResult((pe.getPagenum()-1)*pe.getPagesize());47 * 48 * 49 * 50 * return q.list();51 */52 Session session = sf.openSession();53 // 创建Criteria对象,用来实现标准的对象查询54 Criteria c = session.createCriteria(Student.class);55 // 设置分页参数56 c.setMaxResults(pe.getPagesize()); // 每页数据量57 // 查询的起始位置58 c.setFirstResult((pe.getPagenum() - 1) * pe.getPagesize());59 return c.list();60 }61 @Override62 public Long selectCountRows()63 {64 // TODO Auto-generated method stub65 // hql语句66 String hql = "select count(sno) from Student";67 Session session = sf.openSession();68 // 如果查询的结果是单行,单列使用uniqueResult()69 Object rows = session.createQuery(hql).uniqueResult();70 return (Long) rows;71 }72 }、
5.5 建立测试类
1 package org.guangsoft.test; 2 import java.util.List; 3 import org.junit.Before; 4 import org.junit.Test; 5 import org.guangsoft.dao.StudentDao; 6 import org.guangsoft.dao.impl.StudentDaoImpl; 7 import org.guangsoft.pojo.Student; 8 import org.guangsoft.vo.PageEntity; 9 public class TestCrud10 {11 // 声明Dao变量12 StudentDao dao;13 /***14 * 15 * init方法在所有加@Test注解的方法执行之前,会自动执行。16 * 17 * ***/18 @Before19 public void init()20 {21 dao = new StudentDaoImpl();22 }23 /***24 * 25 * 分页查询26 * 27 * ***/28 @Test29 public void testSelectStudentBySno()30 {31 PageEntity pe = new PageEntity();32 pe.setPagenum(2);33 pe.setPagesize(2);34 Listlist = dao.selectStudentOfPage(pe);35 for (Student s : list)36 {37 System.out.println("-----" + s.getSno() + "\t" + s.getSname()38 + "\t" + s.getAddress() + "\t" + s.getBirthday());39 }40 }41 /**42 * 43 * 测试获得总行数44 * 45 * ***/46 @Test47 public void testCountRows()48 {49 Long rows = dao.selectCountRows();50 System.out.println(rows);51 }52 }