直接用List对象进行分页
来源:互联网 发布:thinkphp直播系统源码 编辑:程序博客网 时间:2024/06/14 15:16
传统的Spring和Hibernate分页,可能使访问不断继续,如果数据量太大,请求过程就会很大,笔者认为,如果改变思路,直接对查询后返回的结果进行分页,这样就不必每次进行页码变化时,都对数据库进行请求,效率大大提高!
但是同时相应的逻辑也变得稍微复杂了,只要把握好业务逻辑,就能进行快速开发!
以下是代码,和大家一起分享:
jsp页面:
<script type="text/javascript">
function pageFirst(count){
var currentPage = count;
if(currentPage >= 1)
{
document.getElementById("pagination").currentPage.value = 1;
document.getElementById("pagination").submit();
}
}
function pageFront(count){
var currentPage = count;
if(currentPage >=1)
{
document.getElementById("pagination").currentPage.value = count;
document.getElementById("pagination").submit();
}
else
{
return;
}
}
function pageNext(count){
var currentPage =count;
var totalPage =${totalPage};
if(currentPage < totalPage)
{
document.getElementById("pagination").currentPage.value =count;
document.getElementById("pagination").submit();
}
else
{
return;
}
}
function pageLast(count){
var currentPage = count;
var totalPage = ${totalPage};
if(currentPage <= totalPage)
{
document.getElementById("pagination").currentPage.value =totalPage;
document.getElementById("pagination").submit();
}
}
function toPage(count){
var currentPage = count;
var totalPage = ${totalPage};
if(currentPage > totalPage)
{
document.getElementById("pagination").currentPage.value=totalPage;
}
if(currentPage<1){
document.getElementById("pagination").currentPage.value=1;
}
document.getElementById("pagination").submit();
}
</script>
<html:form action="/dinner/findHotel.do?method=findMorePassagesAboutRes" method="post" styleId="pagination">
<html:hidden property="totalPage"/>
<html:hidden property="maxRecord"/>
<table width="95%" border="0" align="center" cellpadding="3"
cellspacing="0" class="table3">
<tr>
<td align="right" class="bianju_in">
<div class="body">
[
<a href="javascript:pageFirst(1);">第一页</a>] [
<a href="javascript:pageFront(${currentPage-1});">上一页</a>]|
[
<a href="javascript:pageNext(${currentPage+1});">下一页</a>]
[
<a href="javascript:pageLast(${totalPage});">最后一页</a>]
第
<input name="currentPage" id="currentPage" size="2" value="${currentPage}"/>页
<input type="hidden" name="maxRecord"
value="${maxRecord}" />
<input type="button" value=">>"
onClick="javascript:toPage(document.getElementById('currentPage').value)" />
共 ${totalPage} 页
</div>
</td>
</tr>
</table>
</html:form>
Action页面:
public ActionForward findList(ActionMapping mapping,ActionForm form,HttpservletRequest request,HttpServletresponse response){
TBRestaurantActionForm restaurantForm=(TBRestaurantActionForm)form;
request.setAttribute("recommend",dealWithLeaf(restaurantForm,recommend));
request.setAttribute("totalPage", restaurantForm.getTotalPage());
request.setAttribute("maxRecord",restaurantForm.getMaxRecord());
request.setAttribute("currentPage", restaurantForm.getCurrentPage());
}
public List dealWithLeaf(RestaurantActionForm form,List list){
RestaurantActionForm myform=(RestaurantActionForm)form;
long maxRecord=myform.getMaxRecord();
if(maxRecord%FETCHSIZE==0){ //刚好均匀分配
myform.setTotalPage((int)(maxRecord/FETCHSIZE));
return getFetchSizeByPageNo(myform.getCurrentPage(),list);
}
else{
myform.setTotalPage((int)(maxRecord/FETCHSIZE)+1);
List newlist=new ArrayList();
if(myform.getCurrentPage()<myform.getTotalPage()){
for (int i =(myform.getCurrentPage()-1)*FETCHSIZE; i <=myform.getCurrentPage()*FETCHSIZE-1 ; i++) {
newlist.add(list.get(i));
}
}
else{
for (int i = (myform.getCurrentPage()-1)*FETCHSIZE; i <maxRecord; i++) {
newlist.add(list.get(i));
}
}
return newlist;
}
}
public List getFetchSizeByPageNo(int currentPage,List list){
List newlist=new ArrayList();
for (int i =(currentPage-1)*FETCHSIZE; i <currentPage*FETCHSIZE-1; i++) {
newlist.add(i,list.get(i));
}
return newlist;
}
Dao页面:
public List<TBRestaurant> findTBRestaurantInfo(){
List<TBRestaurant> list=getHiberntateTemplate().find("from TBRestaurant");
return list;
}
- 直接用List对象进行分页
- 对list进行分页
- 将list进行分页
- List转分页对象
- List集合进行界面分页
- 将list集合进行分页
- 通过List集合进行分页
- 直接用datatable分页
- 利用PagedDataSource 对象进行分页
- 使用对象进行分页显示
- 对list对象进行排序
- 对List(或DTO)进行分页查询
- 简单的对list进行分页
- 对List数组进行分页显示
- string对象比能直接进行修改
- 直接使用泛型类中的方法来进行分页
- 用ibatis 进行 分页
- 对list中的对象进行排序
- Linux Program Tools (Linux Binutils)
- 12.4.9 多文件上传
- 12.5 小结
- LINQ 学习笔记-2
- JAVA如何调用DOS命令(转贴 笔记)
- 直接用List对象进行分页
- 国际:程序员V.S.编程语言:你上“贼船”了吗?
- 世界第一张互联网虚拟大脑结构图
- dup和dup2函数
- LINQ 学习笔记-3
- 基于MFC的截屏和以位图保存之的自编函数
- SQL创建数据库失败解决办法:(Collation '' is not valid.)
- 信用证,打包贷款
- 委托(3)委托链