技术标签: javaweb mybatis框架 mysql jar intellij-idea mvc java-ee
这篇博客是用来记录自己所理解的分页功能,后续还会有补充,如果对内容有任何疑问,欢迎一起交流学习。
分页就是让页面可以显示指定条数的数据。通过分页功能,可以更容易的管理数据,查看数据。
通过上述描述,就可以想到,大体的分页方式就是,通过各种查询,各种处理,然后将该指定条数的数据发送到前台进行显示。
逻辑分页
先把所有数据都查出来,然后通过Java程序处理将指定条数据发送到前端进行显示。SQL语句简单,但不适合数据量大的分页。
物理分页
利用SQL语句查询到指定条数据,发送到前端进行显示。当数据量较大时使用物理分页效率更高,但SQL语句较为复杂。
分页工具类里定义了一些用于分页的属性和方法。主要是通过分页工具类来实现分页功能的。
package com.dao;
public class PageUtil {
// 每页显示的条数
private int pageSize;
// 总共的条数
private int recordCount;
// 当前页面
private int currentPage;
//总页数
public int pageCount;
//上一页的最后一条记录
private int start;
//当前页的最后一条记录
private int end;
// 获取上一页的最后一条记录
public int getStart() {
start=(currentPage - 1) * pageSize;
return start;
}
//为mysql写的 select * from table limit start,end;
//limit是限制查询从start+1开始,最多查询end条数据
public int getEnd() {
end=pageSize;
return end;
}
// 构造方法
public PageUtil(int pageSize, int recordCount, int currentPage) {
this.pageSize = pageSize;
this.recordCount = recordCount;
setCurrentPage(currentPage);
}
// 构造方法
public PageUtil(int pageSize, int recordCount) {
this(pageSize, recordCount, 1);
}
public PageUtil() {
super();
// TODO Auto-generated constructor stub
}
// 总页数
public int getPageCount() {
// 总条数/每页显示的条数=总页数
int size = recordCount / pageSize;
// 最后一页的条数
int mod = recordCount % pageSize;
// 看需不需要多余的页,也就是最后一页
if (mod != 0)
size++;
this.pageCount=recordCount == 0 ? 1 : size;
return this.pageCount;
}
// 上一页的最后一条记录数。包含,起始索引为0
public int getFromIndex() {
// System.out.println("from index:"+(currentPage-1) * pageSize);
return (currentPage - 1) * pageSize;
}
// 本页的最后一条记录数。不包含
public int getToIndex() {
// System.out.println("to index:"+Math.min(recordCount, currentPage *
// pageSize));
return Math.min(recordCount, currentPage * pageSize);
}
// 得到当前页
public int getCurrentPage() {
return currentPage;
}
// 设置当前页
public void setCurrentPage(int currentPage) {
int validPage = currentPage <= 0 ? 1 : currentPage;
validPage = validPage > getPageCount() ? getPageCount() : validPage;
this.currentPage = validPage;
}
// 得到每页显示的条数
public int getPageSize() {
return pageSize;
}
// 设置每页显示的条数
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
// 得到总共的条数
public int getRecordCount() {
return recordCount;
}
// 设置总共的条数
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
}
最重要的三个参数:记录总数、页面大小、当前页号
通过记录总数就可以知道一共有多少条数据
通过页面大小就可以知道一共有多少个页面
通过当前页号+页面大小,就可以知道当前页有哪些内容,前一页有哪些内容,后一页有哪些内容
案例:对查询到的部门信息进行分页,每页显示3条数据。前台页面 DepartmentPage.jsp
,后台页面 SearchDepartmentPageServlet
。
DepartmentPage.jsp
作用:SearchDepartmentPageServlet
作用:<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set value="${pageContext.request.contextPath}" var="path"></c:set>
<html>
<head>
<title>Title</title>
<%--引入jQuery库--%>
<script src="${path}/js/jquery.js"></script>
<%--定义js脚本,向后台传递当前页号和部门名称--%>
<script>
function formSubmit(currentPage) {
$('input[name="currentPage"]').val(currentPage);
$('form:first').submit();
}
</script>
</head>
<body>
<%--通过查询来显示指定数据--%>
<form action="${path}/SearchDepartmentPageServelt">
<label>部门名称</label>
<%--查询条件--%>
<input type="text" name="departmentName" value="${param.departmentName}">
<%--当前页数--%>
<input type="hidden" name="currentPage" value="1">
<input type="submit" value="查找">
</form>
<%--要显示的部门信息--%>
<table>
<tr>
<th>部门编号</th>
<th>部门名称</th>
<th>部门地址</th>
</tr>
<%--这里根据后台的查询数据循环生成表格里的数据--%>
<c:forEach items="${departments}" var="department">
<tr>
<th>${department.departmentId}</th>
<th>${department.departmentName}</th>
<th>${department.locationName}</th>
</tr>
</c:forEach>
</table>
<%--数据显示--%>
<div>
<label>共${pageUtil.recordCount}条数据,每页显示${pageUtil.pageSize}条数据,共${pageUtil.pageCount}页,当前是第${pageUtil.currentPage}页</label>
</div>
<%--用来进行页面跳转--%>
<%--第一次查询后就已经获取到pageUtil对象,然后就可以使用pageUtil对象里的属性了--%>
<div>
<a href="#" onclick="formSubmit(1)" id="first">首页</a>
<a href="#" onclick="formSubmit(${
pageUtil.currentPage - 1})" id="prev">上一页</a>
<a href="#" onclick="formSubmit(${
pageUtil.currentPage + 1})" id="next">下一页</a>
<a href="#" onclick="formSubmit(${
pageUtil.pageCount})" id="last">尾页</a>
<label>跳转到</label>
<%--输入框填写页数--%>
<input type="text" id="pageNum">
<%--根据输入框中的页数,进行跳转--%>
<input type="button" value="Go" onclick="formSubmit($('#pageNum').val())">
</div>
</body>
</html>
package com.servlets;
import com.dao.PageUtil;
import com.service.DepartmentService;
import com.vo.Department;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet(name = "SearchDepartmentPageServlet", value = "/SearchDepartmentPageServlet")
public class SearchDepartmentPageServlet extends HttpServlet {
private DepartmentService departmentService = new DepartmentService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 从jsp页面接收到的参数
String departmentName = request.getParameter("departmentName");
int currentPage = Integer.parseInt(request.getParameter("currentPage"));
// 查询到所有的部门信息
List<Department> departmentList = departmentService.selectByDepartmentName(departmentName);
// 创建页面划分对象
PageUtil pageUtil = new PageUtil(3, departmentList.size(), currentPage);
// 创建一个部门集合,用来存放要显示的部门信息。(之前是将获取到的集合对象直接转发,现在要从对象中获取指定条数的对象)
List<Department> departments = new ArrayList<>();
for (int i = pageUtil.getFromIndex(); i < pageUtil.getToIndex(); i++) {
departments.add(departmentList.get(i));
}
// 将部门和页面工具对象请求转发到指定的页面
request.setAttribute("departments", departments);
request.setAttribute("pageUtil", pageUtil);
request.getRequestDispatcher("DepartmentPage.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
// 通过名字查询部门
public List<Department> selectByDepartmentName(String departmentName){
String sql = "SELECT * FROM departments WHERE DEPARTMENT_NAME LIKE ?";
Object[] objects = {
"%"+departmentName+"%"};
RowMapper<Department> rm = (rs)->{
Department department = new Department();
try {
department.setDepartmentId(rs.getInt("DEPARTMENT_ID"));
department.setDepartmentName(rs.getString("DEPARTMENT_NAME"));
department.setLocationName(rs.getString("LOCATION_NAME"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return department;
};
return Dbutil.executeQuery(sql, objects, rm);
}
private DepartmentDao departmentDao = new DepartmentDao();
// 通过名字查询部门
public List<Department> selectByDepartmentName(String departmentName){
return departmentDao.selectByDepartmentName(departmentName);
}
DepartmentPage.jsp
作用:SearchDepartmentPageServlet
作用:<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set value="${pageContext.request.contextPath}" var="path"></c:set>
<html>
<head>
<title>Title</title>
<%--引入jQuery库--%>
<script src="${path}/js/jquery.js"></script>
<%--定义js脚本,向后台传递当前页号和部门名称--%>
<script>
function formSubmit(currentPage) {
$('input[name="currentPage"]').val(currentPage);
$('form:first').submit();
}
</script>
</head>
<body>
<%--通过查询来显示指定数据--%>
<form action="${path}/SearchDepartmentPageServelt2">
<label>部门名称</label>
<%--查询条件--%>
<input type="text" name="departmentName" value="${param.departmentName}">
<%--当前页数--%>
<input type="hidden" name="currentPage" value="1">
<input type="submit" value="查找">
</form>
<%--要显示的部门信息--%>
<table>
<tr>
<th>部门编号</th>
<th>部门名称</th>
<th>部门地址</th>
</tr>
<%--这里根据后台的查询数据循环生成表格里的数据--%>
<c:forEach items="${departments}" var="department">
<tr>
<th>${department.departmentId}</th>
<th>${department.departmentName}</th>
<th>${department.locationName}</th>
</tr>
</c:forEach>
</table>
<%--数据显示--%>
<div>
<label>共${pageUtil.recordCount}条数据,每页显示${pageUtil.pageSize}条数据,共${pageUtil.pageCount}页,当前是第${pageUtil.currentPage}页</label>
</div>
<%--用来进行页面跳转--%>
<%--第一次查询后就已经获取到pageUtil对象,然后就可以使用pageUtil对象里的属性了--%>
<div>
<a href="#" onclick="formSubmit(1)" id="first">首页</a>
<a href="#" onclick="formSubmit(${
pageUtil.currentPage - 1})" id="prev">上一页</a>
<a href="#" onclick="formSubmit(${
pageUtil.currentPage + 1})" id="next">下一页</a>
<a href="#" onclick="formSubmit(${
pageUtil.pageCount})" id="last">尾页</a>
<label>跳转到</label>
<%--输入框填写页数--%>
<input type="text" id="pageNum">
<%--根据输入框中的页数,进行跳转--%>
<input type="button" value="Go" onclick="formSubmit($('#pageNum').val())">
</div>
</body>
</html>
package com.servlets;
import com.dao.PageUtil;
import com.service.DepartmentService;
import com.vo.Department;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "SearchDepartmentPageServelt2", value = "/SearchDepartmentPageServelt2")
public class SearchDepartmentPageServelt2 extends HttpServlet {
private DepartmentService departmentService = new DepartmentService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String departmentName = request.getParameter("departmentName");
int currentPage = Integer.parseInt(request.getParameter("currentPage"));
PageUtil pageUtil = new PageUtil(3, departmentService.selectByDepartmentName(departmentName).size(), currentPage);
List<Department> departments = departmentService.selectLimitDepartment(departmentName, pageUtil.getStart(), pageUtil.getEnd());
request.setAttribute("pageUtil", pageUtil);
request.setAttribute("departments", departments);
request.getRequestDispatcher("DepartmentPage.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
// 通过名字查询部门
public List<Department> selectByDepartmentName(String departmentName){
String sql = "SELECT * FROM departments WHERE DEPARTMENT_NAME LIKE ?";
Object[] objects = {
"%"+departmentName+"%"};
RowMapper<Department> rm = (rs)->{
Department department = new Department();
try {
department.setDepartmentId(rs.getInt("DEPARTMENT_ID"));
department.setDepartmentName(rs.getString("DEPARTMENT_NAME"));
department.setLocationName(rs.getString("LOCATION_NAME"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return department;
};
return Dbutil.executeQuery(sql, objects, rm);
}
// *****************这个是重点*****************
// 通过名字查询部门
public List<Department> selectLimitDepartment(String departmentName, int start, int end){
String sql = "SELECT * FROM departments WHERE DEPARTMENT_NAME LIKE ? limit ?, ?";
Object[] objects = {
"%"+departmentName+"%", start, end};
RowMapper<Department> rm = (rs)->{
Department department = new Department();
try {
department.setDepartmentId(rs.getInt("DEPARTMENT_ID"));
department.setDepartmentName(rs.getString("DEPARTMENT_NAME"));
department.setLocationName(rs.getString("LOCATION_NAME"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return department;
};
return Dbutil.executeQuery(sql, objects, rm);
}
private DepartmentDao departmentDao = new DepartmentDao();
// 通过名字查询部门
public List<Department> selectByDepartmentName(String departmentName){
return departmentDao.selectByDepartmentName(departmentName);
}
// 通过名字查询部门
public List<Department> selectLimitDepartment(String departmentName, int start, int end){
return departmentDao.selectLimitDepartment(departmentName, start, end);
}
这里没有使用前端页面来展示分页后的内容,而是通过在控制台打印查询到的分页信息来显示分页内容。下面用两个案例来介绍分页功能。
这里使用的是物理分页,需要做三件事来实现分页:
①从数据库中查出要显示数据的总记录数
②从数据库中查出要显示的数据集合
③分页工具类对象指定页面大小、获取①中的页面总数、指定当前是第几页即可
// 查询到记录总数,就是一共有多少条班级信息
int selectCount();
// 根据分页工具类进行分页,获取到分页后要显示的班级信息
List<Clazz> selectPage(@Param("page") PageUtil pageUtil);
<!--查询数据库一共有多少条记录-->
<select id="selectCount" resultType="int">
select count(*) from clazz
</select>
<!--物理分页-->
<select id="selectPage" resultType="com.bean.Clazz">
select <include refid="base_info"></include> from clazz
limit #{page.start}, #{page.end}
</select>
@Test
public void testSelectPage() {
// 这里输出分页信息
int count = this.clazzService.selectCount();
PageUtil pageUtil = new PageUtil(3, count, 3);
System.out.println("共"+pageUtil.pageCount+"页");
System.out.println("当前是第"+pageUtil.getCurrentPage()+"页");
System.out.println("共有"+pageUtil.getRecordCount()+"条数据");
// 这里输出分页后的班级信息
List<Clazz> clazzes = this.clazzService.selectPage(pageUtil);
for (int i=0; i<clazzes.size(); i++) {
Clazz clazz = clazzes.get(i);
System.out.println(clazz.getId() + "\t" + clazz.getClassname());
}
}
这里也是用的是物理分页,需要做三件事来实现分页:
①查询满足条件的记录总数
②查询满足条件的要显示的数据集合
③分页工具类对象中指定页面大小、记录总数、当前页数
// 根据条件查询学生的数量(不需要使用爱好做条件),查询总记录数
int selectCount(Student student);
// 根据条件查询学生的信息,实现分页
List<Student> selectPage(@Param("student") Student student, @Param("page") PageUtil page);
<!--根据条件查询出记录总数-->
<select id="selectCount" resultType="int">
select count(*) from student
<where>
<if test="name != null">
name like "%"#{name}"%"
</if>
<if test="sex != null">
and sex = #{sex}
</if>
<if test="birthday != null">
and birthday = #{birthday}
</if>
<if test="age != null and age > 0">
and age = #{age}
</if>
<if test="classid != null and classid > 0">
and classid = #{classid}
</if>
</where>
</select>
<!--根据条件查询出要显示的数据集合-->
<select id="selectPage" resultMap="baseMap">
select <include refid="Base_Column_List"></include> from student
<where>
<if test="student.name != null">
name like "%"#{student.name}"%"
</if>
<if test="student.sex != null">
and sex = #{student.sex}
</if>
<if test="student.birthday != null">
and birthday = #{student.birthday}
</if>
<if test="student.age != null and student.age > 0">
and age = #{student.age}
</if>
<if test="student.classid != null and student.classid > 0">
and classid = #{student.classid}
</if>
</where>
limit #{page.start}, #{page.end}
</select>
public void testSelectPage() {
// 自定义查询条件
Student student = new Student();
student.setName("a");
student.setSex("man");
// 输出分页信息
int count = this.studentService.selectCount(student);
PageUtil pageUtil = new PageUtil(3, count, 2);
System.out.println("共"+pageUtil.pageCount+"页");
System.out.println("当前是第"+pageUtil.getCurrentPage()+"页");
System.out.println("共有"+pageUtil.getRecordCount()+"条数据");
// 输出要显示的数据集合
List<Student> students = this.studentService.selectPage(student, pageUtil);
for (int i=0; i < students.size(); i++) {
System.out.println(students.get(i).getName() + "\t" + students.get(i).getSex());
}
}
以上就是对分页功能的记录。需要分清「逻辑分页」和「物理分页」联系和区别。逻辑分页SQL语句可能相对简单一点,但不适用于数据量极大的情况。数据量较大的时候,需要使用物理分页。重点理解分页工具类,然后结合我提供的思路和核心代码,基本上就可以实现分页功能了。
文章浏览阅读698次。#pragma once #include <deque>#include <condition_variable>template <typename T>class MsgList { public: void add(const T& msg) { std::unique_lock<std::mutex> lock(mutex); queue.
文章浏览阅读551次,点赞8次,收藏8次。重点:项目和文档是本人近期原创所作!程序可以将水表图片里面的数据进行深度学习,提取相关信息训练,lw1.3万字重复15%,可以直接上交那种!具体和看下面的目录。python水表识别,图像识别深度学习 CNN,Opencv,Keras。_水表 深度学习 识别
文章浏览阅读288次。遥感图像方面的人工智能数据集数据集类别常用数据集目标检测数据集DSTL 卫星图像数据集;RSOD-Dataset 数据集;NWPUVHR-10地理遥感数据集图像分割数据集Inria AerialImage Labeling Dataset 遥感图像数据集遥感图像分类数据集UCMerced Land-Use Data Set 土地遥感数据集_群智感知 图像数据集
文章浏览阅读2.9k次,点赞3次,收藏11次。如何在pycharm中安装opencv_opencv_python安装镜像
文章浏览阅读595次,点赞2次,收藏8次。我的小站SSM项目需要用来管理依赖,所以我们需要先配置好,配置很容易,我就不演示了。首先,我们新建项目,勾选,选择模板,然后创建。这里耐心等待下载完成。可以看到,这里没用相关的文件夹。我们直接在文件夹上右键新建文件夹,下面会显示一个,直接创建就可以。此时,我们按照规范来,创建一个包。项目结构多种多样,比如三层架构啥的,按照你的需求来。我这里就稍微演示一下。这里这些结构都是可以自己按照规范命名,结构也有很多,分层架构方法也有很多,这里权当借鉴一下。我这里整合了一份依赖,如需使用可按照自己需求和对于版本进_idea创建ssm web项目
文章浏览阅读3.2k次。2022年-2023年中职网络安全web渗透任务整理合集_server2280 中职组
文章浏览阅读1.3k次,点赞27次,收藏19次。我原本以为是很简单的事情,但是没想到实际做起来还是有很多的基础知识点的。我们平常写TabControl的时候,可能都很习惯了直接写TabControl+TabItem。但是TabControl负责了什么布局,TabItem负责了什么布局,我们都不知道。在《深入浅出WPF》中,我们可以看到TabControl属于ItemsControl我们去看看控件模板样式副本。WPF的xaml的优点是每个控件都是单独的逻辑,耦合低。缺点是写起来麻烦,每次改动约等于重新写一个新的。通过增加自己的工作量来降低了耦合我们可以看_wpf 使用tabcontrol如何给切换的页面增加滚动条
文章浏览阅读2.1k次,点赞24次,收藏47次。Apache Jmeter常用插件下载及安装_jmeter插件下载
文章浏览阅读5.9k次,点赞6次,收藏18次。实际上Mybatis的整合过程像极了我们程序员的一生。在SpringBoot 整合Mybatis之前,我们回忆回忆以前 MyBatis 单独使用时,myBatis 核心配置文件要配置数据源、事务、连接数据库账号、密码....是的全是这货一个人干,都要亲力亲为。这就是我们的低谷期myBatis 与 spring 整合的时候,配置数据源、事务、连接数据库的账号什么的都交由 spring 管理就行,就不用什么都自己管理自己去干。这就是我们春风得意的时候,事业有着落...再后来,Spring_springboot2.1.5整合mybatis不需要配置mapper-locations
文章浏览阅读162次。原标题:颤抖吧 iOS, Android 8.0正式发布!如果现在选一个最好用的手机操作系统,多数人还是认为 iOS。不过最近几年,苹果和安卓的竞争越来越激烈,苹果的优势也越来越小。眼看 Android 8.0 就要来了,下面就让我们扒一扒 Android 8.0 到底有哪些更新? 后台限制机制,从此告别卡顿安卓手机比较坑爹的一个地方就是后台越多应用,就会越卡顿,导致用户需要偶尔清理后台,一定程度..._苹果刷安卓8
文章浏览阅读344次。如果不使用halcon引擎,直接调用lines_gauss虽然内存会飙升,但是属于图片占用的内存还是会立刻被释放,但是如果在halcon引擎中,这个就会释放很慢,如果连续处理图片,你的内存就会“爆炸”!一个6M的图片通过halcon进行加载,大约会消耗200M的内存,如果等待GC回收,而你又在不停的读取图片,你的内存占用,将在短时间内飙升。目前给我的感觉是,如果我封装了一个算子,然后通过halcon引擎调用,然后这个算子需要传入图片参数,这个图片传入引擎后,过很久才会被释放掉。_halcon 读二维码占内存
文章浏览阅读304次。Thinkpad X250笔记本电脑,装的是FreeBSD,进入BIOS修改虚拟化配置(其后可能是误设置了安全开机),保存退出后系统无法启动,显示:secure boot failed ,把自己惊出一身冷汗,因为这台笔记本刚好还没开始做备份.....根据错误提示,到bios里面去找相关配置,在Security里面找到了Secure Boot选项,发现果然被设置为Enabled,将其修改为Disabled ,再开机,终于正常启动了。_安装完系统提示secureboot failure