Servlet + JSP+JSTL +MySQL+Bootstrap 等技术实现分页查询功能。
所用工具:IDEA 2022.3.3 + Navicat +Tomcat 等。
本文目录
一:运行效果
二:代码详解
(1)index.jsp
(2)PageBean
(3)Servlet
(4)DAO层
(5)BaseDao
(6)JSP
(7)所需数据表
(8)案例项目结构
三:功能展示
点击下一页
点击尾页
点击上一页
点击首页
运行之后如上图所示。
所包含的功能有: 首页、尾页、上一页、下一页、当前的页码和总页码统计等。
至于关于分页的其他功能,如控制每一页显示几条数据、跳转到第几页等,有待后续更新...
首页发起请求。侧重功能实现,没做样式(可自行设计)。
<%--
Created by IntelliJ IDEA.
User: AdminSun
Date: 2023/6/12
Time: 15:44
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
首页
欢迎使用
分页查询展示
分页工具类:
package cn.shq.util;
import java.util.List;
public class PageBean {
private Integer curPage;//当前页码
private Integer pageSize;//每一页显示的条数
private List datas; //每一页要显示的数据
private Integer firstPage;//首页
private Integer prevPage;//上一页
private Integer nextPage;//下一页
private Integer lastPage;//尾页
private Integer totalCount;//数据总条数
private Integer totalPages; //总页码
public PageBean() {
}
/* 构造方法*/
public PageBean(Integer curPage, Integer pageSize, List datas, Integer totalCount) {
//初始化各个属性
this.curPage = curPage;
this.pageSize = pageSize;
this.datas = datas;
this.totalCount = totalCount;
//计算出来其他的属性=====也算作初始化
//计算总页码
/*
* 总页码 = 总条数 % pageSize ? 整除 则 总条数/pageSize
* 有余数 总条数/pageSize+1
* */
this.totalPages=this.totalCount%this.pageSize==0?this.totalCount/this.pageSize:this.totalCount/this.pageSize+1;
// 意思: ↓↓↓↓
/* if(totalCount%pageSize==0){
this.totalPages=totalCount/pageSize;
}else{
this.totalPages=totalCount/pageSize+1;
}*/
this.firstPage=1;
this.prevPage=this.curPage-1;
this.nextPage=this.curPage+1;
this.lastPage=this.totalPages;
//对于超出范围的,加以限定
//如果第一次访问的时候,
if(curPage==null||curPage<1) curPage=1;
//总页码
if(this.totalPages==0) this.totalPages=1;
//对于 上一页 当显示的页面为第一页的时候,不再递减
if(prevPage<=0) this.prevPage=1;
//对于下一页 最后一页的时候,下一页 停留在最后一页
if(nextPage>=this.totalPages) this.nextPage=this.totalPages;
}
public Integer getCurPage() {
return curPage;
}
public void setCurPage(Integer curPage) {
this.curPage = curPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public List getDatas() {
return datas;
}
public void setDatas(List datas) {
this.datas = datas;
}
public Integer getFirstPage() {
return firstPage;
}
public void setFirstPage(Integer firstPage) {
this.firstPage = firstPage;
}
public Integer getPrevPage() {
return prevPage;
}
public void setPrevPage(Integer prevPage) {
this.prevPage = prevPage;
}
public Integer getNextPage() {
return nextPage;
}
public void setNextPage(Integer nextPage) {
this.nextPage = nextPage;
}
public Integer getLastPage() {
return lastPage;
}
public void setLastPage(Integer lastPage) {
this.lastPage = lastPage;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getTotalPages() {
return totalPages;
}
public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}
}
控制器类:
package cn.shq.controller;
import cn.shq.dao.RoomDao;
import cn.shq.dao.impl.RoomDaoImp;
import cn.shq.util.PageBean;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "RoomServlet", value = "/RoomServlet")
public class RoomServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
RoomDao rm=new RoomDaoImp();
String pageParam = req.getParameter("curPage");
if (pageParam == null) pageParam = "1";
if (pageParam == "") pageParam = "1";
int curPage = Integer.parseInt(pageParam);
//每一页显示的条数, 从页面下拉框获取
String psize=req.getParameter("pageSize");
if (psize == null) psize = "3";
int pageSize = Integer.parseInt(psize);
//调用dao,执行查询
PageBean pageBean = rm.selByPage(curPage, pageSize);
req.setAttribute("pager", pageBean);
req.getRequestDispatcher("/admin/showRoom.jsp").forward(req, response);
}
}
本案例中Servlet直接访问DAO层调用方法。
package cn.shq.dao.impl;
import cn.shq.dao.RoomDao;
import cn.shq.util.BaseDao;
import cn.shq.util.PageBean;
import java.util.List;
import java.util.Map;
public class RoomDaoImp implements RoomDao {
@Override
public PageBean selByPage(int curPage, int pageSize) {
String sql="select r.rNum,r.rStatus,rs.roomstatus,rt.typeName,r.rPrice,r.rPhone,r.rPic \n" +
"from room r,roomstatus rs,roomtype rt\n" +
"where r.rType=rt.tid and r.rStatus=rs.sid\n" +
"LIMIT ?,?";
int startIndex=pageSize * (curPage-1);
List
访问数据库所用的工具类
package cn.shq.util;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BaseDao {
protected static Connection conn = null;
protected static PreparedStatement pstm = null;
protected static ResultSet rs = null;
private static String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/bbs_xdy?useSSL=true";
private static String username="root";
private static String password="123456";
private static Connection getConn(){
try {
Class.forName(driver);
Connection connection = DriverManager.getConnection(url,username,password);
return connection;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 通用关闭
* */
public static void closeAll(Connection conn,PreparedStatement pstm,ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 增删改
* */
public static int executeUpdate(String sql, Object... obj) {
try {
conn = getConn();
pstm = conn.prepareStatement(sql);
setParam(obj);
int num = pstm.executeUpdate();
return num;
} catch (Exception e) {
e.printStackTrace();
} finally {
closeAll(conn,pstm,rs);
}
return 0;
}
/*
* 解决占位符参数的问题
* */
private static void setParam(Object... obj) {
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
try {
pstm.setObject(i + 1, obj[i]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/*
* 通用查询
* */
public static List
展示数据的页面。
<%--
Created by IntelliJ IDEA.
User: AdminSun
Date: 2020/12/08
Time: 10:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
住房管理
<%--显示数据的表格--%>
序号
房号
房间图片
房间状态
房间类型
房间价格(元)
房间电话
${s.count}
${room.rNum}
${room.roomstatus}
${room.typeName}
${room.rPrice}
${room.rPhone}
<%--分页控制--%>