hibernate分页技术.docx
《hibernate分页技术.docx》由会员分享,可在线阅读,更多相关《hibernate分页技术.docx(7页珍藏版)》请在冰豆网上搜索。
hibernate分页技术
使用扩展HibernateDaoSupport实现分页技术总结
1.定义MyHibernateDaoSupport扩展HibernateSupport
mportjava.sql.SQLException;
importjava.util.List;
importorg.hibernate.HibernateException;
importorg.hibernate.Query;
importorg.hibernate.Session;
importorg.springframework.orm.hibernate3.HibernateCallback;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
publicclassMyHibernateDaoSupportextendsHibernateDaoSupport{
publicListfindByPage(finalStringhql,finalintoffset,finalintpageSize)
{
Listlist=this.getHibernateTemplate().executeFind(newHibernateCallback()
{
publicObjectdoInHibernate(Sessionsession)
throwsHibernateException,SQLException{
Listresult=session.createQuery(hql).setFirstResult(offset).setMaxResults(pageSize).list();
returnresult;
}
}
);
returnlist;
}
publicListfindByPage(finalStringhql,finalStringvalue,finalintoffset,finalintpageSize)
{
Listlist=this.getHibernateTemplate().executeFind(newHibernateCallback()
{
publicObjectdoInHibernate(Sessionsession)
throwsHibernateException,SQLException{
Listresult=session.createQuery(hql).setParameter(0,value).setFirstResult(offset).setMaxResults(pageSize).list();
returnresult;
}
}
);
returnlist;
}
publicListfindByPage(finalStringhql,finalObject[]values,finalintoffset,finalintpageSize){
Listlist=this.getHibernateTemplate().executeFind(newHibernateCallback()
{
publicObjectdoInHibernate(Sessionsession)
throwsHibernateException,SQLException{
Queryquery=session.createQuery(hql);
for(inti=0;i {
query.setParameter(i,values[i]);
}
Listresult=query.setFirstResult(offset).setMaxResults(pageSize).list();
returnresult;
}
}
);
returnlist;
}
}
2.定义要分页的实体的Dao接口
如:
publicinterfaceStudentDao{
Studentget(intid);
voidsave(Studentstudent);
voidupdate(Studentstudent);
voiddelete(intid);
voiddelete(Studentstudent);
ListfindAll();
ListfindAllByPage(intpageNo,intpageSize);
intgetStudentCount();
ListfindStudentByNameAndNumber(StringstuName,StringstuNumber);
}
3.定义实现类
主要写出两个分页中要用到的方法
publicListfindAllByPage(intpageNo,intpageSize){
if(pageNo<1){
returnnull;
}
intoffset=(pageNo-1)*pageSize;
returnfindByPage("fromStudent",offset,pageSize);
}
publicintgetStudentCount(){
ListlistStudent=this.getHibernateTemplate().find("fromStudent");
returnlistStudent.size();
}
4.定义Service接口
publicinterfaceExamService{
intSTUDENT_PAGE_SIZE=3;
intQUESTION_PAGE_SIZE=3;
intaddStudent(StringstuNumber,Stringname,StringclassName,StringhumanId,Stringemail,Stringaddress,Stringphone)throwsException;
voiddeleteStudent(intid)throwsException;
ListlistStudent(intpageNo)throwsException;
intaddQuestion(StringquTitle,StringquHard,StringquScore,StringquAnswer,StringquType,StringselectOption,inttypeid)throwsException;
voiddeleteQuestion(intid)throwsException;
ListlistQuestion(intpageNo)throwsException;
voiddeleteExamtype(inttypeid)throwsException;
intaddExamtype(StringtextName,StringtestTime)throwsException;
ListgetAllExamtype()throwsException;
booleanadminLogin(StringadmName,StringadmPwd)throwsException;
intgetStudentCount()throwsException;
intgetQuestionCount()throwsException;
intgetPageCount(intcount,intpageSize);
StringstudentLogin(StringstuName,StringstuNumber)throwsException;
QuestiongetNextQuestion(Listalreadys,inttypeid)throwsException;
QuestiongetQuestionById(intid)throwsException;
StringgetExamtypeName(inttypeid)throwsException;;
}
5.定义实现类
publicintgetPageCount(intcount,intpageSize){
return(count+pageSize-1)/pageSize;
}
publicintgetStudentCount()throwsException{
returnstudentDao.getStudentCount();
}
publicListlistStudent(intpageNo)throwsException{
returnstudentDao.findAllByPage(pageNo,STUDENT_PAGE_SIZE);
}
6.ListStudentAction.java
intstudentCount=examService.getStudentCount();
ActionMessageserrors=newActionMessages();
if(studentCount<1)
{
errors.add("studentCount",newActionMessage("studentCount.null"));
mapping.findForward("success");
}
intpageCount=examService.getPageCount(studentCount,examService.STUDENT_PAGE_SIZE);
intpageNo;
if(request.getParameter("pageNo")==null||request.getParameter("pageNo").trim().equals(""))
{
pageNo=1;
}
try{
pageNo=Integer.parseInt(request.getParameter("pageNo").trim());
}catch(Exceptione){
pageNo=1;
}
if(pageNo>pageCount){
pageNo=pageCount;
}
request.setAttribute("pageCount",pageCount);
request.setAttribute("currentPage",pageNo);
request.setAttribute("studentList",examService.listStudent(pageNo));
returnmapping.findForward("success");
7.listStudent.jsp
messagekey="student.shenfenzheng"/> |
messagekey="student.mingzi"/> |
messagekey="student.banji"/> |
messagekey="student.xuehao"/> |
messagekey="student.youjian"/> |
messagekey="student.dianhua"/> |
messagekey="student.address"/> |
messagekey="student.isdelete"/> |
---|
forEachitems="${requestScope.studentList}"var="students">
${students.humanId}
${students.stuName}
${students.stuClassName}
${students.stuNumber}
${students.email}
${students.phone}
${students.address}
delStuid=${students.id}"
onclick='returnconfirm("messagekey="confirm.del.student"/>");'target="center">message
key="student.delete"/>
|
forEach>
第${requestScope.currentPage}页 共${requestScope.pageCount}页
pageNo=1">首页
greaterThanvalue="1"name="currentPage"scope="request">
pageNo=${requestScope.currentPage-1}">
greaterThan>
上一页
greaterThanvalue="1"name="currentPage"scope="request">
greaterThan>
lessThanvalue="${requestScope.pageCount}"name="currentPage"
scope="request">
pageNo=${requestScope.currentPage+1}">
lessThan>
下一页
lessThanvalue="${requestScope.pageCount}"name="currentPage"
scope="request">
lessThan>
pageNo=${requestScope.pageCount}">尾页
|
setFirstResult是起始数据,setMaxResults是查询显示的数据。
如果放在分页程序里边setFirstResult的值应该是(当前页面-1)*每页条数,setMaxResults就是每页的条数了。
关于setMaxResults和setFetchSize的区别:
我的理解是
∙setMaxResults是用来配合数据库生成sql的,在sql里就控制查询的记录数目
∙setFetchSize是控制查询结果的,可能sql已经查询出100条,但是hibernate只取前10条放到返回的List里。