jsp mysql 实现客户端简单分页查询
dao.impl
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import sim.dao.StudentDao;
import sim.entity.Student;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;
public class StudentDaoImpl implements StudentDao {
private DataSource ds = new ComboPooledDataSource();
private QueryRunner qr = new QueryRunner(ds); // 必有的两句
@Override
public List selectAll(Integer currentPageNo, Integer pageSize) {
List studentList = null;
try {
String sql ="select * from user limit ?,?";
Object[] objects={(currentPageNo-1)*pageSize,pageSize}; //页面数量,页面大小
studentList = qr.query(sql,new BeanListHandler(Student.class),objects); //很多地方少写了 r currentPageNo
} catch (SQLException e) {
e.printStackTrace();
}
return studentList;
}
@Override
public Integer selectCount() {
Long count = null;
try {
String sql ="select count(1) from user"; //把 Long 写成 long
count = (Long) qr.query(sql,new ScalarHandler()); //总页数
} catch (SQLException e) {
e.printStackTrace();
}
return Integer.parseInt(count+"");
}
}
StudentServiceImpl
import sim.dao.StudentDao;
import sim.dao.StudentDaoImpl.StudentDaoImpl;
import sim.entity.Student;
import sim.service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = new StudentDaoImpl(); //必有的实例
@Override
public List selectAll(Integer currentPageNo, Integer pageSize) { //
return studentDao.selectAll(currentPageNo,pageSize);
}
@Override
public Integer selectCount() {
return studentDao.selectCount();
}
}
servlet
import sim.Utils.PageUtils;
import sim.entity.Student;
import sim.service.StudentService;
import sim.service.StudentServiceImpl.StudentServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "studentServlet" ,urlPatterns = "/studentServlet")
public class StudentServlet extends HttpServlet {
private StudentService studentService; //一定有的一句
@Override
public void init() throws ServletException {
studentService = new StudentServiceImpl(); //一定有的一句
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String currentPageNoStr = req.getParameter("currentPageNo"); //本页的当前页量
Integer currentPageNo = null;
if(currentPageNoStr ==null || currentPageNoStr==""){
currentPageNo = 1;
}else{
currentPageNo =Integer.parseInt(currentPageNoStr);
}
String pageSizeStr = req.getParameter("pageSize"); //当前页量数
Integer pageSize=null;
if(pageSizeStr ==null || pageSizeStr==""){
pageSize = 2; //无页量,则为2
}else{
pageSize =Integer.parseInt(pageSizeStr); //有页量,则传递
}
Integer totalPageCount = studentService.selectCount();
Integer totalPageSize = totalPageCount % pageSize ==0 ? totalPageCount / pageSize //为什么等于0————因为把 % 打成了 /
: totalPageCount / pageSize +1;
List studentList = studentService.selectAll(currentPageNo,pageSize);
PageUtils pageUtils = new PageUtils<>(); //数据库的名字和实体类不同,导致错误。
pageUtils.setCurrentPageNo(currentPageNo);
pageUtils.setPageSize(pageSize);
pageUtils.setTotalPageCount(totalPageCount);
pageUtils.setTotalPageSize(totalPageSize);
pageUtils.setList(studentList);
req.setAttribute("pageUtils",pageUtils);
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
分页
<%-- 重定向,加载两次页面(跳过第一次无数据)--%>
<% Object object = request.getAttribute("pageUtils");
if (object==null){
request.getRequestDispatcher("studentServlet").forward(request,response);
}
%>
编号
名字
年龄
${student.uid}
${student.uname}
${student.uage}
Student (实现;三个变量;两构造;set get;toString)
PageUtils (五个变量;两构造;set get;toString)
结果展示: