《2022年分页查询代码 .pdf》由会员分享,可在线阅读,更多相关《2022年分页查询代码 .pdf(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Struts2+Spring+Hibernate分页查询日期:2012-03-15 来源 : 熊佳佳 分享至 : base href= 百通网后台 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 12 页 - - - - - - - - - !- - function skip() var currentPage=document.getElementById(textfield).value; document.locat ion=admin/scanNews.acti
2、on?currentPage=+currentPage; function nextPage() document.location=admin/scanNews.action?currentPage=; function prePage() document.location=admin/scanNews.action?currentPage=; 新闻 ID 添加时间 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 12 页 - - - - - - - - - 新闻标题
3、 添加人 新闻类型 编辑 删除 编辑 a onclick=if(confirm( 删除后不可恢复,确实要删除该信息?)return true;return false; href=ad 共 $recordSum 条记录 $PageSum 页当前位于第 $currentPage 页名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 12 页 - - - - - - - - - 首页 a href=admin/scanNews.action?currentPage= class=
4、pageInfoChars尾页 GO package com.baitw.utils.entity; import java.util.List; /* * * 分页查询实体 * * */ public class PageBean /*=*/ private List resultList;/结果集private int recordSUM;/总记录数private int pageSUM;/总页数private int currentPage;/当前页private int pageSize;/页记录数private boolean isFirstPage;/是否是第一页private b
5、oolean isLastPage;/是否是最后一页private boolean hasPreviousPage;/是否有上一页private boolean hasNextPage;/是否有下一页名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 12 页 - - - - - - - - - /*= 初始化=*/ public void init() this.isFirstPage = isFirstPage(); this.isLastPage = isLastPag
6、e(); this.hasPreviousPage = isHasPreviousPage(); this.hasNextPage = isHasNextPage(); /*= 总页数=*/ public stat ic int countTotalPage(int pageSize,int recordSUM) int totalPage = recordSUM % pageSize = 0 ? recordSUM/pageSize : recordSUM/pageSize+1; return totalPage; /*= 当前页开始记录号=*/ public static int coun
7、tOffset (final int pageSize,final int currentPage) final int offset = pageSize*(currentPage-1); return offset; /*= 当期页=*/ public static int countCurrentPage(int page) final int curPage = (page=0?1:page); return curPage; /*= 判断当前页的状态=*/ public boolean isFirstPage() return currentPage = 1; public bool
8、ean isLastPage() return currentPage = pageSUM; public boolean isHasPreviousPage() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 12 页 - - - - - - - - - return currentPage != 1; public boolean isHasNextPage() return currentPage != pageSUM; /*=Get/Set方法=*/ public
9、 List getResultList() return resultList; public void setResultList(List resultList) this.resultList = resultList; public int getRecordSUM() return recordSUM; public void setRecordSUM(int recordSUM) this.recordSUM = recordSUM; public int getPageSUM() return pageSUM; public void setPageSUM(int pageSUM
10、) this.pageSUM = pageSUM; public int getCurrentPage() return currentPage; public void setCurrentPage(int currentPage) this.currentPage = currentPage; public int getPageSize() return pageSize; public void setPageSize(int pageSize) this.pageSize = pageSize; /*=*/ package com.baitw.service.impl; 名师资料总结
11、 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 12 页 - - - - - - - - - import java.util.List; import com.baitw.dao.PageDao; import com.baitw.service.PageService; import com.baitw.utils.entity.PageBean; /* * * 分页查询 Service * * */ public class PageServiceImpl implements
12、 PageService public PageBean TabbedBrowsing(PageDao pageDao, int pageSize, int currentPage, String sql) / TODO Auto-generated method stub int recordSum = pageDao.getAllRowCount(sql);/总记录数int pageSum = PageBean.countTotalPage(pageSize, recordSum);/总页数final int currentRecordIndex = PageBean.countOffse
13、t(pageSize, currentPage);/当前页开始记录final int resultMaxSize = pageSize;/每页记录数final int page = PageBean.countCurrentPage(currentPage);/当前页List list = pageDao.QueryForPage(sql, currentRecordIndex, resultMaxSize);/一页 的记录/ 把分页信息保存到Bean 中PageBean pageBean = new PageBean(); pageBean.setPageSize(pageSize); pa
14、geBean.setCurrentPage(page); pageBean.setRecordSUM(recordSum); pageBean.setPageSUM(pageSum); pageBean.setResultList(list); pageBean.init(); return pageBean; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 12 页 - - - - - - - - - / 新闻分页查询public List QueryForNews(S
15、tring hql, int currentRecordIndex, int resultMaxSize) / TODO Auto-generated method stub List list=new ArrayList(); SimpleDateFormat format=new SimpleDateFormat(yyyy-MM-dd); Session session=HibernateSessionFactory.getSession(); Query query=session.createQuery(hql); query.setFirstResult(currentRecordI
16、ndex); query.setMaxResults(resultMaxSize); list=query.list(); List resultList=new ArrayList(); for(Iterator iter=list.iterator();iter.hasNext();) TNews n=(TNews) iter.next(); MNews m=new MNews(); m.setId(n.getId()+); m.setAuthor(n.getTUser().getFulln ame(); m.setCreatedate(format.format(n.getCreatet
17、ime (); m.setState(n.getState().toString(); m.setTitle(n.getTitle(); m.setType(getNewsType(n.getType(); resultList.add(m); session.close(); return resultList; package com.baitw.struts.action; import java.util.ArrayList; import java.util.List; import .baitw.hibernate.CreateActivityForDB; import .bait
18、w.hibernate.CreateNewsForDB; import com.baitw.dao.PageDao; import com.baitw.service.PageService; import com.baitw.utils.entity.PageBean; import com.opensymphony.xwork2.ActionSupport; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 12 页 - - - - - - - - - /* * * 新
19、闻分页查询 * * */ public class ScanNews extends ActionSupport / 通过 applicationContext.xml配置文件注入 UserService的值private PageService pageService;/PAGE服务类private int currentPage; /当第几页private PageBean pageBean;/包含分布信息的 bean private PageDao pageDao;/用户 PageDao 实现private String hql;/查询 SQL private int pageSize;
20、/每页记录数private List pageNumber;/分页页码private int recordSum;/总记录数private int PageSum;/总页数List resultList=new ArrayList();/查询结果public int getRecordSum() return recordSum; public void setRecordSum(int recordSum) this.recordSum = recordSum; public int getPageSum() return PageSum; public void setPageSum(in
21、t pageSum) PageSum = pageSum; public int getPageSize() return pageSize; public void setPageSize(int pageSize) this.pageSize = pageSize; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 12 页 - - - - - - - - - public List getResultList() return resultList; public v
22、oid setResultList(List resultList) this.resultList = resultList; public PageService getPageService() return pageService; public void setPageService(PageService pageService) this.pageService = pageService; public int getCurrentPage() return currentPage; public void setCurrentPage(int currentPage) thi
23、s.currentPage = currentPage; public PageBean getPageBean() return pageBean; public void setPageBean(PageBean pageBean) this.pageBean = pageBean; public PageDao getPageDao() return pageDao; public void setPageDao(PageDao pageDao) this.pageDao = pageDao; public String getHql() return hql; 名师资料总结 - - -
24、精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 12 页 - - - - - - - - - public void setHql(String hql) this.hql = hql; public List getPageNumber() return pageNumber; public void setPageNumber(List pageNumber) this.pageNumber = pageNumber; Override public String execut e() t
25、hrows Exception / TODO Auto-generated method stub pageBean = pageService.TabbedBrowsing(pageDao, pageSize, currentPage, hql); resultList=pageDao.QueryForNews(hql, getFirstNum()-1, pageSize); pageNumber=pageDao.getPageNumber(hql, pageSize); recordSum=pageDao.getAllRowCount(hql); PageSum=pageNumber.si
26、ze(); return news_List; private int getFirstNum() if(currentPage=1) return 1; else return (currentPage-1)*pageSize+1; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 12 页 - - - - - - - - - http:/ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 12 页 - - - - - - - - -