Mybatis 分页:Pagehelper + 拦截器实现 (2)

分页辅助参数内部类 PageParam.java

public static class PageParam { // 当前页 int pageNum; // 分页开始位置 int offset; // 分页数量 int limit; // 总数 public int totalSize; // 总页数 public int totalPage; }

查询总记录数

private long queryTotal(MappedStatement mappedStatement, BoundSql boundSql) throws SQLException { Connection connection = null; PreparedStatement countStmt = null; ResultSet rs = null; try { connection = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection(); String countSql = this.dialect.getCountSql(boundSql.getSql()); countStmt = connection.prepareStatement(countSql); BoundSql countBoundSql = new BoundSql(mappedStatement.getConfiguration(), countSql, boundSql.getParameterMappings(), boundSql.getParameterObject()); setParameters(countStmt, mappedStatement, countBoundSql, boundSql.getParameterObject()); rs = countStmt.executeQuery(); long totalCount = 0; if (rs.next()) { totalCount = rs.getLong(1); } return totalCount; } catch (SQLException e) { log.error("查询总记录数出错", e); throw e; } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { log.error("exception happens when doing: ResultSet.close()", e); } } if (countStmt != null) { try { countStmt.close(); } catch (SQLException e) { log.error("exception happens when doing: PreparedStatement.close()", e); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { log.error("exception happens when doing: Connection.close()", e); } } } }

对分页SQL参数?设值

private void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql, Object parameterObject) throws SQLException { ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql); parameterHandler.setParameters(ps); }

利用方言接口替换原始的SQL语句

private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) { MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType()); builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) { StringBuffer keyProperties = new StringBuffer(); for (String keyProperty : ms.getKeyProperties()) { keyProperties.append(keyProperty).append(","); } keyProperties.delete(keyProperties.length() - 1, keyProperties.length()); builder.keyProperty(keyProperties.toString()); } //setStatementTimeout() builder.timeout(ms.getTimeout()); //setStatementResultMap() builder.parameterMap(ms.getParameterMap()); //setStatementResultMap() builder.resultMaps(ms.getResultMaps()); builder.resultSetType(ms.getResultSetType()); //setStatementCache() builder.cache(ms.getCache()); builder.flushCacheRequired(ms.isFlushCacheRequired()); builder.useCache(ms.isUseCache()); return builder.build(); }

计算总页数

public int countPage(int totalSize, int offset) { int totalPageTemp = totalSize / offset; int plus = (totalSize % offset) == 0 ? 0 : 1; totalPageTemp = totalPageTemp + plus; if (totalPageTemp <= 0) { totalPageTemp = 1; } return totalPageTemp; }

供调用的静态分页方法

我这里设计的,页数是从1开始的,如果习惯用0开始,可以自己修改。

public static void startPage(int pageNum, int pageSize) { int offset = (pageNum-1) * pageSize; int limit = pageSize; PageInterceptor.PageParam pageParam = new PageInterceptor.PageParam(); pageParam.offset = offset; pageParam.limit = limit; pageParam.pageNum = pageNum; PARAM_THREAD_LOCAL.set(pageParam); } 2.5 分页结果集

为了便于结果封装,我这里自己封装了一个比较全的分页结果集,包含太多的东西了,自己慢慢看下面的属性吧(自认为比较全了,欢迎打脸)

public class PageResult<T> implements Serializable { /** * 是否为第一页 */ private Boolean isFirstPage = false; /** * 是否为最后一页 */ private Boolean isLastPage = false; /** * 当前页 */ private Integer pageNum; /** * 每页的数量 */ private Integer pageSize; /** * 总记录数 */ private Integer totalSize; /** * 总页数 */ private Integer totalPage; /** * 结果集 */ private List<T> data; public PageResult() { } public PageResult(List<T> data) { this.data = data; PageInterceptor.PageParam pageParam = PageInterceptor.PARAM_THREAD_LOCAL.get(); if (pageParam != null) { pageNum = pageParam.pageNum; pageSize = pageParam.limit; totalSize = pageParam.totalSize; totalPage = pageParam.totalPage; isFirstPage = (pageNum == 1); isLastPage = (pageNum == totalPage); PageInterceptor.PARAM_THREAD_LOCAL.remove(); } } public Integer getPageNum() { return pageNum; } public void setPageNum(Integer pageNum) { this.pageNum = pageNum; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getTotalSize() { return totalSize; } public void setTotalSize(Integer totalSize) { this.totalSize = totalSize; } public Integer getTotalPage() { return totalPage; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public List<T> getData() { return data; } public void setData(List<T> data) { this.data = data; } public Boolean getFirstPage() { return isFirstPage; } public void setFirstPage(Boolean firstPage) { isFirstPage = firstPage; } public Boolean getLastPage() { return isLastPage; } public void setLastPage(Boolean lastPage) { isLastPage = lastPage; } @Override public String toString() { return "PageResult{" + "isFirstPage=" + isFirstPage + ", isLastPage=" + isLastPage + ", pageNum=" + pageNum + ", pageSize=" + pageSize + ", totalSize=" + totalSize + ", totalPage=" + totalPage + ", data=" + data + '}'; } } 2.6 简单测试下 @Test public void selectForPage() { // 该查询进行分页,指定第几页和每页数量 PageInterceptor.startPage(1,4); List<UserInfoDO> all = userMapper.findAll(); PageResult<UserInfoDO> result = new PageResult<>(all); // 分页结果打印 log.info("总记录数:{}", result.getTotalSize()); log.info("list:{}", result.getData()); log.info("result:{}", result); }

使用方法基本1.3完全一致吧,只是封装成了我自己的分页结果集。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxfyj.html