web项目如何分页?经典分页算法

①新建一个类VO,新建字段 count(一共多少条记录),presentPage(当前在第几页),List<Student> studentLs;(当前页面对象的集合);

public class TeacherPage {
    private Integer count;
    private Integer presentPage;
    private List<Student> studentLs;
}

②service层

@Override
    public R getPage(Integer inset, Integer tiaoshu) {
        StudentPage studentPage=new StudentPage();
        studentPage.setCount(studentDao.getCount());
        studentPage.setStuLs(studentDao.getPage((inset-1)*tiaoshu,tiaoshu));
        studentPage.setPresentPage(inset);
        return R.ok(studentPage);
    }

inset:第几页 tiaoshu:一页有几条

③dao层

<select id="getCount" resultType="Integer">
        select count(id) from tb_student
</select>

<select id="getPage" parameterType="Integer" resultType="com.chen.entity.Student">
        select * from tb_student limit #{inset},#{tiaoshu}
</select>

④前端页面:采用前后端分离模式,通过AJax异步交互得到,其中有个分页算法

Html

<div class="research_content">
					<div class="research_table">
						<table class="table table-bordered">
							<thead>
								<tr style="line-height: 18.2px;">
									<th scope="col">#</th>
									<th scope="col">学号</th>
									<th scope="col">姓名</th>
									<th scope="col">年龄</th>
									<th scope="col">性别</th>
									<th scope="col">电话</th>
									<th scope="col">学院</th>
									<th scope="col">专业</th>
									<th scope="col">年级</th>
									<th scope="col">班级</th>
								</tr>
							</thead>
							<tbody id="tbody">
								<tr style="line-height: 18.2px;">
									<th scope="row">1</th>
									<td>null</td>
									<td>null</td>
									<td>null</td>
									<td>null</td>
									<td>null</td>
									<td>null</td>
									<td>null</td>
									<td>null</td>
									<td>null</td>
								</tr>
							</tbody>
						</table>
					</div>
					<div class="research_grouppage">
						<nav aria-label="Page navigation example" class="pageBlock">
							<ul class="pagination pagination-sm" id="fenye">
								<li class="page-item"><a class="page-link" href="#">上一页</a></li>
								<li class="page-item"><a class="page-link" href="#">1</a></li>
								<li class="page-item"><a class="page-link" href="#">2</a></li>
								<li class="page-item"><a class="page-link" href="#">3</a></li>
								<li class="page-item"><a class="page-link" href="#">下一页</a></li>
							</ul>
						</nav>
					</div>
				</div>

JavaScript

window.onload = function() {
	load(1)

}

function load(a) {
	get("http://localhost:8089/ssmStudent_war_exploded/StudentController/getPage", {
			"inset": a,
			"tiaoshu": 10
		},
		function success(res) {
			$("#tbody").empty();
			res.msg.stuLs.forEach(function(value, index) {
				$("#tbody").append(
					'<tr style="line-height: 18.2px;">' +
					'<th scope="row">' + value.id + '</th>' +
					'<td>' + value.stuNumber + '</td>' +
					'<td>' + value.stuName + '</td>' +
					'<td>' + value.stuAge + '</td>' +
					'<td>' + value.stuSex + '</td>' +
					'<td>' + value.stuPhone + '</td>' +
					'<td>' + value.collegeName + '</td>' +
					'<td>' + value.majorName + '</td>' +
					'<td>' + value.grade + '</td>' +
					'<td>' + value.className + '</td>' +
					'</tr>'
				)
			});
			var page = Math.ceil(res.msg.count / 10);
			var presentPageNext = res.msg.presentPage + 1;
			if (presentPageNext >= page) {
				presentPageNext = page;
			}
			var presentPageBack = res.msg.presentPage - 1;
			if (presentPageBack <= 1) {
				presentPageBack = 1;
			}
			$("#fenye").empty()
			console.log(page)
			if (page == 1 && res.msg.presentPage == 1) {
				$("#fenye").append(
					'<li class="page-item"><a class="page-link" onclick="load(' + 1 + ')" href="#">上一页</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + 1 + ')" href="#">1</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + 1 + ')" href="#">下一页</a></li>'
				);
			} else if (page == 2) {
				$("#fenye").append(
					'<li class="page-item"><a class="page-link" onclick="load(' + presentPageBack + ')" href="#">上一页</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + 1 + ')" href="#">1</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + 2 + ')" href="#">2</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + presentPageNext + ')" href="#">下一页</a></li>'
				);
			} else if (page == 3) {
				'<li class="page-item"><a class="page-link" onclick="load(' + presentPageBack + ')" href="#">上一页</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + 1 + ')" href="#">1</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + 2 + ')" href="#">2</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + 3 + ')" href="#">3</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + presentPageNext + ')" href="#">下一页</a></li>'
			} else if (page == 4) {
				'<li class="page-item"><a class="page-link" onclick="load(' + presentPageBack + ')" href="#">上一页</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + 1 + ')" href="#">1</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + 2 + ')" href="#">2</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + 3 + ')" href="#">3</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + 4 + ')" href="#">4</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + presentPageNext + ')" href="#">下一页</a></li>'
			} else if (page >= 5) {
				if (a <= 2) {
					a = 3
				} else if (a >= page - 2) {
					a = page - 2
				}
				$("#fenye").append(
					'<li class="page-item"><a class="page-link" onclick="load(' + presentPageBack + ')" href="#">上一页</a></li>' +
					'<li class="page-item" onclick="load(' + (a - 2) + ')"><a class="page-link" href="#">' + (a - 2) + '</a></li>' +
					'<li class="page-item" onclick="load(' + (a - 1) + ')"><a class="page-link" href="#">' + (a - 1) + '</a></li>' +
					'<li class="page-item" onclick="load(' + (a) + ')"><a class="page-link" href="#">' + (a) + '</a></li>' +
					'<li class="page-item" onclick="load(' + (a + 1) + ')"><a class="page-link" href="#">' + (a + 1) + '</a></li>' +
					'<li class="page-item" onclick="load(' + (a + 2) + ')"><a class="page-link" href="#">' + (a + 2) + '</a></li>' +
					'<li class="page-item"><a class="page-link" onclick="load(' + presentPageNext + ')" href="#">下一页</a></li>'
				)
			}
		},
		function error(err) {

		})
}

这个ajax是包装过的,主要看分页算法思想

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于基于Web的分页技术,其主要目的是为了展示大量数据时,将数据进行分页展示,以便于用户查找所需数据。下面我来简要介绍一下分页技术的研究与实现。 1. 分页的原理及常用技术: 首先需要了解的是分页的原理,在Web应用程序中,分页的原理是将大量数据分成多个页面进行展示,上一页、下一页、首页和尾页等按钮可以帮助用户在多个页面之间进行快速切换。分页的实现需要分页算法分页显示技术,常用的分页显示技术有两种:前端分页和后端分页。 2. 前端分页技术实现: 前端分页是在客户端进行分页操作,将数据从服务器获取到前端,再通过JS等脚本语言实现分页逻辑。前端分页的优缺点如下: 优点: ①从服务器获取的数据较少,对服务器的负载小; ②对用户的响应速度较快,因为仅需要请求当前页面的数据; ③可以提高用户交互性,网页可以更灵活地对数据进行处理。 缺点: ①前端分页需要将所有数据一次性请求到客户端,如果数据量很大,那么会对网络带宽产生较大负担。 ②分页逻辑只能在客户端进行,无法有效控制网络速度等因素的影响,可能会出现分页错误等问题。 3. 后端分页技术实现: 后端分页是在服务器端进行分页处理,将数据通过SQL语句从数据库中取出,再进行分页处理返回给客户端。后端分页的优缺点如下: 优点: ①后端分页是在服务器端进行分页处理,不需要将所有数据一次性请求到客户端,减少网络带宽消耗。 ②分页逻辑在服务器端进行,可以更好地控制分页效果。 缺点: ①对服务器的负载较大,需要对服务器进行优化,否则可能出现网络响应慢等问题。 ②对用户的响应速度较慢,需要请求所有数据后才能开始分页。 以上就是关于基于Web的分页技术的简要介绍,如果您需要更深入地了解分页技术的实现方法,还需要进一步学习相关知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值