springmvc mybatis怎么实现分页查询

2025-01-18 18:04:02
推荐回答(1个)
回答1:

  1.封装分页Page类

  package com.framework.common.page.impl;
  
  import java.io.Serializable;
  
  import com.framework.common.page.IPage;
  /**
  *
  *
  *
  */
  public abstract class BasePage implements IPage, Serializable {
  
  /**
  *
  */
  private static final long serialVersionUID = -3623448612757790359L;
  
  public static int DEFAULT_PAGE_SIZE = 20;
  private int pageSize = DEFAULT_PAGE_SIZE;
  private int currentResult;
  private int totalPage;
  private int currentPage = 1;
  private int totalCount = -1;
  
  public BasePage(int currentPage, int pageSize, int totalCount) {
  this.currentPage = currentPage;
  this.pageSize = pageSize;
  this.totalCount = totalCount;
  }
  
  public int getTotalCount() {
  return this.totalCount;
  }
  
  public void setTotalCount(int totalCount) {
  if (totalCount < 0) {
  this.totalCount = 0;
  return;
  }
  this.totalCount = totalCount;
  }
  
  public BasePage() {
  }
  
  public int getFirstResult() {
  return (this.currentPage - 1) * this.pageSize;
  }
  
  public void setPageSize(int pageSize) {
  if (pageSize < 0) {
  this.pageSize = DEFAULT_PAGE_SIZE;
  return;
  }
  this.pageSize = pageSize;
  }
  
  public int getTotalPage() {
  if (this.totalPage <= 0) {
  this.totalPage = (this.totalCount / this.pageSize);
  if ((this.totalPage == 0) || (this.totalCount % this.pageSize != 0)) {
  this.totalPage += 1;
  }
  }
  return this.totalPage;
  }
  
  public int getPageSize() {
  return this.pageSize;
  }
  
  public void setPageNo(int currentPage) {
  this.currentPage = currentPage;
  }
  
  public int getPageNo() {
  return this.currentPage;
  }
  
  public boolean isFirstPage() {
  return this.currentPage <= 1;
  }
  
  public boolean isLastPage() {
  return this.currentPage >= getTotalPage();
  }
  
  public int getNextPage() {
  if (isLastPage()) {
  return this.currentPage;
  }
  return this.currentPage + 1;
  }
  
  public int getCurrentResult() {
  this.currentResult = ((getPageNo() - 1) * getPageSize());
  if (this.currentResult < 0) {
  this.currentResult = 0;
  }
  return this.currentResult;
  }
  
  public int getPrePage() {
  if (isFirstPage()) {
  return this.currentPage;
  }
  return this.currentPage - 1;
  }
  
  
  }
  

  
  package com.framework.common.page.impl;
  
  import java.util.List;
  /**
  *
  *
  *
  */
  public class Page extends BasePage {
  
  /**
  *
  */
  private static final long serialVersionUID = -970177928709377315L;
  
  public static ThreadLocal threadLocal = new ThreadLocal();
  
  private List data;
  
  public Page() {
  }
  
  public Page(int currentPage, int pageSize, int totalCount) {
  super(currentPage, pageSize, totalCount);
  }
  
  public Page(int currentPage, int pageSize, int totalCount, List data) {
  super(currentPage, pageSize, totalCount);
  this.data = data;
  }
  
  public List getData() {
  return data;
  }
  
  public void setData(List data) {
  this.data = data;
  }
  
  
  }
  

  2.封装分页插件

  package com.framework.common.page.plugin;
  
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.List;
  import java.util.Properties;
  
  import javax.xml.bind.PropertyException;
  
  import org.apache.commons.lang3.StringUtils;
  import org.apache.ibatis.executor.ErrorContext;
  import org.apache.ibatis.executor.ExecutorException;
  import org.apache.ibatis.executor.statement.BaseStatementHandler;
  import org.apache.ibatis.executor.statement.RoutingStatementHandler;
  import org.apache.ibatis.mapping.BoundSql;
  import org.apache.ibatis.mapping.MappedStatement;
  import org.apache.ibatis.mapping.ParameterMapping;
  import org.apache.ibatis.mapping.ParameterMode;
  import org.apache.ibatis.plugin.Interceptor;
  import org.apache.ibatis.plugin.Intercepts;
  import org.apache.ibatis.plugin.Invocation;
  import org.apache.ibatis.plugin.Plugin;
  import org.apache.ibatis.reflection.MetaObject;
  import org.apache.ibatis.reflection.property.PropertyTokenizer;
  import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
  import org.apache.ibatis.session.Configuration;
  import org.apache.ibatis.type.TypeHandler;
  import org.apache.ibatis.type.TypeHandlerRegistry;
  
  import com.framework.common.page.impl.Page;
  import com.framework.common.utils.ReflectUtil;
  /**
  *
  *
  *
  */