在项目中分页是十分常见的功能,一般使用插件实现分页功能,但是在使用插件之前我们首先手动写出分页代码,对比插件实现的分页,利于我们理解分页底层实现和更好的实现插件分页实用技术,本次使用的插件是PageHelper(采用都是物理分页)
在开始之前我们创建两个表,分别是t_user和person表,并且插入大量的数据。
t_user建表语句:
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
person建表语句:
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`gender` varchar(255) DEFAULT NULL,
`dept_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2:手动分页查询针对user表数据
项目首页:
<%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"utf-8"%>
<%
String path &#61; request.getContextPath();
String basePath &#61; request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>index.jsp
首先开始我们的手动分页&#xff0c;核心是一个分页page类&#xff0c;里面有用于分页的各种属性
package com.thit.util;import java.util.List;/*** 抽象出来的分页类*/
public class PageUtil {private int currentPageNum; //当前要看哪一页&#xff0c;当前页private int pageSize&#61;10;//每页显示的条数&#xff0c;页面显示数据条数private int totalSize;//总记录条数&#xff0c;总行数private int startIndex;//查询开始记录的索引 limit ? ? 开始索引private int totalPageNum;//总页数private int prePageNum;//上一页private int nextPageNum;//下一页private List records;//当前页的记录集//用于显示页面上的导航的页号 用户可自定义//开始页码private int startPageNum;//结束页码private int endPageNum;private String url;//使用构造方法&#xff0c;传递必要的两个参数.第一个是页码&#xff0c;第二个总记录条数public PageUtil(int currentPageNum,int totalrecords){this.currentPageNum&#61;currentPageNum;this.totalSize&#61;totalrecords;//计算开始记录索引this.startIndex&#61;(currentPageNum-1)*pageSize;//计算总页数this.totalPageNum&#61;totalSize%pageSize&#61;&#61;0?totalSize/pageSize:totalSize/pageSize&#43;1;this.prePageNum&#61;getPrePageNum1();this.nextPageNum&#61;getNextPageNum1();//计算开始和结束页号 这个根据自身可设计if(totalPageNum>9){ //如果总页数大于9 开始页面startPageNum&#61;currentPageNum-4;//结束页面endPageNum&#61;currentPageNum&#43;4;if(startPageNum<1){startPageNum&#61;1;endPageNum&#61;startPageNum&#43;8;}if(endPageNum>totalPageNum){endPageNum&#61;totalPageNum;startPageNum&#61;endPageNum-8;}}else{startPageNum&#61;1;endPageNum&#61;totalPageNum;}}public int getStartPageNum() {return startPageNum;}public void setStartPageNum(int startPageNum) {this.startPageNum &#61; startPageNum;}public int getEndPageNum() {return endPageNum;}public void setEndPageNum(int endPageNum) {this.endPageNum &#61; endPageNum;}//得到上一页方法public int getPrePageNum1() {System.out.println("得到上一页方法");//上一页等于当前页减1prePageNum&#61;currentPageNum-1;//如过上一个小于0if(prePageNum<&#61;0){ //上一页等于1System.out.println("上一页小于0");prePageNum&#61;1;}return prePageNum;}//得到下一页方法public int getNextPageNum1() {//下一页等于当前页加1System.out.println("得到下一页的方法");nextPageNum&#61;currentPageNum&#43;1;//如果下一页大于总页数if(nextPageNum>totalPageNum){ //下一页等于总页数System.out.println("下一页大于总页数");nextPageNum&#61;totalPageNum;}return nextPageNum;}public int getPrePageNum() {return prePageNum;}public int getNextPageNum() {return nextPageNum;}public int getCurrentPageNum() {return currentPageNum;}public void setCurrentPageNum(int currentPageNum) {this.currentPageNum &#61; currentPageNum;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize &#61; pageSize;}public int getTotalSize() {return totalSize;}public void setTotalSize(int totalSize) {this.totalSize &#61; totalSize;}public int getStartIndex() {return startIndex;}public void setStartIndex(int startIndex) {this.startIndex &#61; startIndex;}public int getTotalPageNum() {return totalPageNum;}public void setTotalPageNum(int totalPageNum) {this.totalPageNum &#61; totalPageNum;}public List getRecords() {return records;}public void setRecords(List records) {this.records &#61; records;}public void setPrePageNum(int prePageNum) {this.prePageNum &#61; prePageNum;}public void setNextPageNum(int nextPageNum) {this.nextPageNum &#61; nextPageNum;}public String getUrl() {return url;}public void setUrl(String url) {this.url &#61; url;}&#64;Overridepublic String toString() {return "PageUtil [currentPageNum&#61;" &#43; currentPageNum &#43; ", pageSize&#61;" &#43; pageSize &#43; ", totalSize&#61;" &#43; totalSize&#43; ", startIndex&#61;" &#43; startIndex &#43; ", totalPageNum&#61;" &#43; totalPageNum &#43; ", 上一页&#61;" &#43; prePageNum&#43; ", 下一页&#61;" &#43; nextPageNum &#43; ", records&#61;" &#43; records &#43; ", startPageNum&#61;" &#43; startPageNum&#43; ", endPageNum&#61;" &#43; endPageNum &#43; ", url&#61;" &#43; url &#43; "]";}}
然后是Servlet:
package com.thit.web;
import java.io.IOException;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 com.thit.service.Userservice;
import com.thit.serviceimpl.UserserviceImpl;
import com.thit.util.PageUtil;&#64;WebServlet("/servlet/UserServlet")
public class UserServlet extends HttpServlet{Userservice userservice&#61;new UserserviceImpl();/*** */private static final long serialVersionUID &#61; 1L;&#64;Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubSystem.out.println("首先进入doget");String para&#61;req.getParameter("method");System.out.println("方法参数&#xff1a;"&#43;para);if(para.equals("all")) {//查询所有用户信息selectAllUsers(req,resp);}}private void selectAllUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubString num&#61;req.getParameter("num");//第一次传递 num为空System.out.println("num的值是&#xff1a;"&#43;num);if(null&#61;&#61;num) {num&#61;"1";}PageUtil page&#61;userservice.getAllusers(num);System.out.println(page.toString());req.setAttribute("page",page);//转发到新的页面req.getRequestDispatcher("/users.jsp").forward(req, resp);}&#64;Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubSystem.out.println("首先进入doPost");this.doGet(req, resp);}}
接着是service接口和实现类&#xff1a;
package com.thit.service;import java.util.List;import com.thit.entity.Person;
import com.thit.util.PageUtil;public interface Userservice {
//查询userpublic PageUtil getAllusers(String num);
//查询personpublic List
}-------------------实现类-----------------
package com.thit.serviceimpl;import java.util.List;import org.apache.commons.dbutils.DbUtils;import com.thit.dao.Userdao;
import com.thit.daoimpl.Userdaoimpl;
import com.thit.entity.Person;
import com.thit.entity.User;
import com.thit.service.Userservice;
import com.thit.util.PageUtil;public class UserserviceImpl implements Userservice {Userdao dao&#61;new Userdaoimpl();public PageUtil getAllusers(String num) {// TODO Auto-generated method stubint currentPageNum&#61;1;//如果当前页不为空&#xff0c;当前页等于numif(num!&#61;null&&!num.trim().equals("")) {currentPageNum&#61;Integer.parseInt(num);}//查询总行数方法int totalPageNum&#61;dao.getTotalSize();System.out.println("查询总行数&#xff1a;"&#43;totalPageNum);//当前页 和 总行数PageUtil pageUtil&#61;new PageUtil(currentPageNum, totalPageNum);//根据开始下标和行数查询出来每页的数据List
dao层和实现类&#xff1a;
package com.thit.dao;import java.util.List;import com.thit.entity.Person;
import com.thit.entity.User;public interface Userdao {//手写分页查询user数据List
}-----------------------dao实现类------------------------
package com.thit.daoimpl;public class Userdaoimpl extends BaseDao implements Userdao {Dbtools dbtools&#61;new Dbtools();//手写查询分页public List
最后的页面展示代码jsp如下&#xff1a;
<%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"utf-8"%>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/core" prefix&#61;"c"%>
<%String path &#61; request.getContextPath();String basePath &#61; request.getScheme() &#43; "://" &#43; request.getServerName() &#43; ":" &#43; request.getServerPort()&#43; path &#43; "/";System.out.println("path:"&#43;path);System.out.println("basePath:"&#43;basePath);%>分页展示数据
id username address ${user.id} ${user.username} ${user.address}
用户表共${requestScope.page.totalPageNum}页
servlet/UserServlet?method&#61;all&num&#61;2">第二页
首页上一页
手动分页结果显示如下&#xff1a;
3&#xff1a;pegeHelper插件分页针对user表数据pegeHelper插件分页只是几个部分
第一&#xff1a;需要的mybatis的配置文件中配置插件
第二&#xff1a;在servlect中使用PageHelper的startPage方法
第三&#xff1a;PageHelper拦截器会拦截查询方法&#xff0c;并且在查询的sql中根据不同的数据库拼接分页语句实现分页
第四&#xff1a;将PageInfo这个类存放分页的各种属性信息&#xff0c;核心代码就这三行&#xff0c;num的值由页面传递过来
Page page&#61;PageHelper.startPage(Integer.valueOf(num), 10)&#xff1b;
List
PageInfo> pageHepler&#61;page.toPageInfo();
需要添加mybatis配置文件和mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
然后servlet如下&#xff1a;
package com.thit.web;import java.io.IOException;
import java.util.List;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 com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thit.entity.Person;
import com.thit.service.Userservice;
import com.thit.serviceimpl.UserserviceImpl;
import com.thit.util.PageUtil;&#64;WebServlet("/servlet/UserServlet2")
public class UserServlet2 extends HttpServlet{Userservice userservice&#61;new UserserviceImpl();/*** */private static final long serialVersionUID &#61; 1L;&#64;Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubSystem.out.println("首先进入doget");String para&#61;req.getParameter("method");System.out.println("方法参数&#xff1a;"&#43;para);if(para.equals("all")) {//查询所有用户信息selectAllUsers(req,resp);}}private void selectAllUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubString num&#61;req.getParameter("num");//第一次传递num为页数 num为空System.out.println("num的值是&#xff1a;"&#43;num);if(null&#61;&#61;num) {num&#61;"1";}//第二种&#xff0c;Mapper接口方式的调用&#xff0c;页数和页面显示条数Page page&#61;PageHelper.startPage(Integer.valueOf(num), 10);List
service和dao层在上边的代码中已经贴出来了。
页面展示代码如下&#xff1a;
<%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"utf-8"%>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/core" prefix&#61;"c"%>
<%
String path &#61; request.getContextPath();
String basePath &#61; request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>id username address ${person.id} ${person.username} ${person.email}
最后的展示效果如下&#xff1a;