配置Pagehelper分页插件
1、pom.xml引入相关jar包,如果已经引入则不用再次引入
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
2、在src/main/java目录下新建page包
3、在page包下新建PageBean、PageFilter、PageUtil类
1)封装的分页Bean
package org.example.page;
import java.io.Serializable;
import java.util.List;
import com.github.pagehelper.Page;
/**
* 封装的分页Bean
* @author th
*
* @param <T>
*/
public class PageBean<T> implements Serializable {
private static final long serialVersionUID = -14627419495444389L;
/** 总记录数 */
private long total;
/** 结果集 */
private List<T> list;
/** 页数 */
private int pageNum;
/** 每页记录数 */
private int pageSize;
/** 总页数 */
private int pages;
/** 当前页的数量 */
private int size;
/**
* 包装Page对象
*
* @param list
*/
public PageBean(List<T> list) {
if (list instanceof Page) {
Page<T> page = (Page<T>) list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();
this.total = page.getTotal();
this.pages = page.getPages();
this.list = page;
this.size = page.size();
}
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
2)传递分页参数的Filter
package org.example.page;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
/**
* 传递分页参数的Filter
* @author th
*
*/
public class PageFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
PageUtil.setPageNum(getPageNum(httpRequest));
PageUtil.setPageSize(getPageSize(httpRequest));
chain.doFilter(request, response);
PageUtil.removePageNum();
PageUtil.removePageSize();
}
/**
* 获取传参过来的页数
*
* @param request
* @return
*/
private int getPageNum(HttpServletRequest request) {
int pageNum = 1;
String pageNums = request.getParameter("pageNum");
if (StringUtils.isNotEmpty(pageNums) && StringUtils.isNumeric(pageNums)) {
pageNum = Integer.parseInt(pageNums);
}
return pageNum;
}
/**
* 获取传参过来的每页记录数
*
* @param request
* @return
*/
private int getPageSize(HttpServletRequest request) {
// 默认每页10条记录
int pageSize = 10;
String pageSizes = request.getParameter("pageSize");
if (StringUtils.isNotEmpty(pageSizes) && StringUtils.isNumeric(pageSizes)) {
pageSize = Integer.parseInt(pageSizes);
}
return pageSize;
}
public void destroy() {
}
}
3)传递分页参数的工具类
package org.example.page;
/**
* 传递分页参数的工具类
* @author th
*
*/
public class PageUtil {
/** 页数 */
private static ThreadLocal<Integer> pageNum = new ThreadLocal<Integer>();
/** 每页记录数 */
private static ThreadLocal<Integer> pageSize = new ThreadLocal<Integer>();
public static int getPageNum() {
Integer pn = pageNum.get();
if (pn == null) {
return 0;
}
return pn;
}
public static void setPageNum(int pageNumValue) {
pageNum.set(pageNumValue);
}
public static void removePageNum() {
pageNum.remove();
}
public static int getPageSize() {
Integer ps = pageSize.get();
if (ps == null) {
return 0;
}
return ps;
}
public static void setPageSize(int pageSizeValue) {
pageSize.set(pageSizeValue);
}
public static void removePageSize() {
pageSize.remove();
}
}
3、在web.xml中添加分页过滤器
<!-- 分页过滤器start -->
<filter>
<filter-name>pageFilter</filter-name>
<filter-class>com.socket.web.page.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>pageFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 分页过滤器end -->