hibernate分页技术Word文件下载.docx
- 文档编号:6724344
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:7
- 大小:16.13KB
hibernate分页技术Word文件下载.docx
《hibernate分页技术Word文件下载.docx》由会员分享,可在线阅读,更多相关《hibernate分页技术Word文件下载.docx(7页珍藏版)》请在冰点文库上搜索。
returnlist;
publicListfindByPage(finalStringhql,finalStringvalue,finalintoffset,finalintpageSize)
Listresult=session.createQuery(hql).setParameter(0,value).setFirstResult(offset).setMaxResults(pageSize).list();
publicListfindByPage(finalStringhql,finalObject[]values,finalintoffset,finalintpageSize){
Queryquery=session.createQuery(hql);
for(inti=0;
i<
values.length;
i++)
query.setParameter(i,values[i]);
Listresult=query.setFirstResult(offset).setMaxResults(pageSize).list();
2.定义要分页的实体的Dao接口
如:
publicinterfaceStudentDao{
Studentget(intid);
voidsave(Studentstudent);
voidupdate(Studentstudent);
voiddelete(intid);
voiddelete(Studentstudent);
List<
Student>
findAll();
findAllByPage(intpageNo,intpageSize);
intgetStudentCount();
findStudentByNameAndNumber(StringstuName,StringstuNumber);
3.定义实现类
主要写出两个分页中要用到的方法
publicList<
findAllByPage(intpageNo,intpageSize){
if(pageNo<
1){
returnnull;
intoffset=(pageNo-1)*pageSize;
returnfindByPage("
fromStudent"
offset,pageSize);
publicintgetStudentCount(){
listStudent=this.getHibernateTemplate().find("
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;
listStudent(intpageNo)throwsException;
intaddQuestion(StringquTitle,StringquHard,StringquScore,StringquAnswer,StringquType,StringselectOption,inttypeid)throwsException;
voiddeleteQuestion(intid)throwsException;
Question>
listQuestion(intpageNo)throwsException;
voiddeleteExamtype(inttypeid)throwsException;
intaddExamtype(StringtextName,StringtestTime)throwsException;
Examtype>
getAllExamtype()throwsException;
booleanadminLogin(StringadmName,StringadmPwd)throwsException;
intgetStudentCount()throwsException;
intgetQuestionCount()throwsException;
intgetPageCount(intcount,intpageSize);
StringstudentLogin(StringstuName,StringstuNumber)throwsException;
QuestiongetNextQuestion(List<
Integer>
alreadys,inttypeid)throwsException;
QuestiongetQuestionById(intid)throwsException;
StringgetExamtypeName(inttypeid)throwsException;
;
5.定义实现类
publicintgetPageCount(intcount,intpageSize){
return(count+pageSize-1)/pageSize;
publicintgetStudentCount()throwsException{
returnstudentDao.getStudentCount();
listStudent(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("
).trim().equals("
"
))
pageNo=1;
try{
pageNo=Integer.parseInt(request.getParameter("
).trim());
}catch(Exceptione){
if(pageNo>
pageCount){
pageNo=pageCount;
request.setAttribute("
pageCount"
pageCount);
currentPage"
pageNo);
studentList"
examService.listStudent(pageNo));
returnmapping.findForward("
7.listStudent.jsp
<
tablecellspacing="
0"
cellpadding="
border="
1"
width="
700"
>
tr>
th>
bean:
messagekey="
student.shenfenzheng"
/>
/th>
student.mingzi"
student.banji"
student.xuehao"
student.youjian"
student.dianhua"
student.address"
student.isdelete"
/tr>
c:
forEachitems="
${requestScope.studentList}"
var="
students"
tdalign="
center"
${students.humanId}
/td>
${students.stuName}
${students.stuClassName}
${students.stuNumber}
${students.email}
${students.phone}
${students.address}
ahref="
deleteStudent.do?
delStuid=${students.id}"
onclick='
returnconfirm("
confirm.del.student"
/>
'
target="
message
key="
student.delete"
/a>
/c:
forEach>
br/>
tdcolspan="
7"
align="
第${requestScope.currentPage}页&
nbsp;
共${requestScope.pageCount}页
listStudent.do?
pageNo=1"
首页<
&
logic:
greaterThanvalue="
name="
scope="
request"
pageNo=${requestScope.currentPage-1}"
/logic:
greaterThan>
上一页
lessThanvalue="
${requestScope.pageCount}"
scope="
pageNo=${requestScope.currentPage+1}"
lessThan>
下一页
pageNo=${requestScope.pageCount}"
尾页<
/table>
setFirstResult是起始数据,setMaxResults是查询显示的数据。
如果放在分页程序里边setFirstResult的值应该是(当前页面-1)*每页条数,setMaxResults就是每页的条数了。
关于setMaxResults和setFetchSize的区别:
我的理解是
∙setMaxResults是用来配合数据库生成sql的,在sql里就控制查询的记录数目
∙setFetchSize是控制查询结果的,可能sql已经查询出100条,但是hibernate只取前10条放到返回的List里。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- hibernate 分页 技术
![提示](https://static.bingdoc.com/images/bang_tan.gif)