Ver código fonte

fix page some question

Caratacus 8 anos atrás
pai
commit
21d938a10b

+ 2 - 44
mybatis-plus/src/main/java/com/baomidou/mybatisplus/plugins/CachePaginationInterceptor.java

@@ -15,7 +15,6 @@
  */
 package com.baomidou.mybatisplus.plugins;
 
-import com.baomidou.mybatisplus.MybatisDefaultParameterHandler;
 import com.baomidou.mybatisplus.entity.CountOptimize;
 import com.baomidou.mybatisplus.plugins.pagination.DialectFactory;
 import com.baomidou.mybatisplus.plugins.pagination.Pagination;
@@ -36,13 +35,10 @@ import org.apache.ibatis.plugin.Plugin;
 import org.apache.ibatis.plugin.Signature;
 import org.apache.ibatis.reflection.MetaObject;
 import org.apache.ibatis.reflection.SystemMetaObject;
-import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
 import org.apache.ibatis.session.ResultHandler;
 import org.apache.ibatis.session.RowBounds;
 
 import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 import java.util.Properties;
 
 /**
@@ -57,7 +53,7 @@ import java.util.Properties;
 		@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class,
 				ResultHandler.class }),
 		@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
-public class CachePaginationInterceptor implements Interceptor {
+public class CachePaginationInterceptor extends PaginationInterceptor implements Interceptor {
 
 	private static final Log logger = LogFactory.getLog(CachePaginationInterceptor.class);
 
@@ -125,7 +121,7 @@ public class CachePaginationInterceptor implements Interceptor {
 						connection = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();
 						CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
 								page.isOptimizeCount());
-						this.count(countOptimize.getCountSQL(), connection, mappedStatement, boundSql, page);
+						super.count(countOptimize.getCountSQL(), mappedStatement, boundSql, page);
 						if (page.getTotal() <= 0) {
 							return invocation.proceed();
 						}
@@ -140,44 +136,6 @@ public class CachePaginationInterceptor implements Interceptor {
 
 	}
 
-	/**
-	 * 查询总记录条数
-	 *
-	 * @param sql
-	 * @param connection
-	 * @param mappedStatement
-	 * @param boundSql
-	 * @param page
-	 */
-	public void count(String sql, Connection connection, MappedStatement mappedStatement, BoundSql boundSql, Pagination page) {
-		PreparedStatement pstmt = null;
-		ResultSet rs = null;
-		try {
-			pstmt = connection.prepareStatement(sql);
-			DefaultParameterHandler parameterHandler = new MybatisDefaultParameterHandler(mappedStatement,
-					boundSql.getParameterObject(), boundSql);
-			parameterHandler.setParameters(pstmt);
-			rs = pstmt.executeQuery();
-			int total = 0;
-			if (rs.next()) {
-				total = rs.getInt(1);
-			}
-			page.setTotal(total);
-			/*
-			 * 溢出总页数,设置第一页
-			 */
-			if (overflowCurrent && (page.getCurrent() > page.getPages())) {
-				page = new Pagination(1, page.getSize());
-				page.setTotal(total);
-			}
-		} catch (Exception e) {
-			logger.error("分页查询中count查询出错", e);
-		} finally {
-			IOUtils.closeQuietly(pstmt);
-			IOUtils.closeQuietly(rs);
-		}
-	}
-
 	public Object plugin(Object target) {
 		if (target instanceof Executor) {
 			return Plugin.wrap(target, this);

+ 30 - 52
mybatis-plus/src/main/java/com/baomidou/mybatisplus/plugins/PaginationInterceptor.java

@@ -23,7 +23,6 @@ import com.baomidou.mybatisplus.toolkit.IOUtils;
 import com.baomidou.mybatisplus.toolkit.PluginUtils;
 import com.baomidou.mybatisplus.toolkit.SqlUtils;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
-import org.apache.ibatis.executor.Executor;
 import org.apache.ibatis.executor.statement.StatementHandler;
 import org.apache.ibatis.logging.Log;
 import org.apache.ibatis.logging.LogFactory;
@@ -37,7 +36,7 @@ import org.apache.ibatis.plugin.Signature;
 import org.apache.ibatis.reflection.MetaObject;
 import org.apache.ibatis.reflection.SystemMetaObject;
 import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
-import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.session.Configuration;
 import org.apache.ibatis.session.RowBounds;
 
 import java.sql.Connection;
@@ -47,19 +46,16 @@ import java.util.Properties;
 
 /**
  * <p>
- * 缓存分页拦截器
+ * 分页拦截器
  * </p>
  *
  * @author hubin
  * @Date 2016-01-23
  */
-@Intercepts({
-		@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class,
-				ResultHandler.class }),
-		@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
+@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
 public class PaginationInterceptor implements Interceptor {
 
-	private static final Log logger = LogFactory.getLog(CachePaginationInterceptor.class);
+	private static final Log logger = LogFactory.getLog(PaginationInterceptor.class);
 
 	/* 溢出总页数,设置第一页 */
 	private boolean overflowCurrent = false;
@@ -82,6 +78,7 @@ public class PaginationInterceptor implements Interceptor {
 			MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
 			RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
 
+			/* 不需要分页的场合 */
 			if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
 				return invocation.proceed();
 			}
@@ -89,6 +86,7 @@ public class PaginationInterceptor implements Interceptor {
 			String originalSql = (String) boundSql.getSql();
 
 			if (rowBounds instanceof Pagination) {
+				MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
 				Pagination page = (Pagination) rowBounds;
 				boolean orderBy = true;
 				if (page.isSearchCount()) {
@@ -96,6 +94,13 @@ public class PaginationInterceptor implements Interceptor {
 							page.isOptimizeCount());
 					orderBy = countOptimize.isOrderBy();
 				}
+				CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
+						page.isOptimizeCount());
+				this.count(countOptimize.getCountSQL(), mappedStatement, boundSql, page);
+				if (page.getTotal() <= 0) {
+					return invocation.proceed();
+				}
+
 				String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy);
 				originalSql = DialectFactory.buildPaginationSql(page, buildSql, dialectType, dialectClazz);
 			} else {
@@ -103,64 +108,40 @@ public class PaginationInterceptor implements Interceptor {
 				originalSql = DialectFactory.buildPaginationSql(rowBounds, originalSql, dialectType, dialectClazz);
 			}
 
+			/*
+			 * <p> 禁用内存分页 </p> <p> 内存分页会查询所有结果出来处理(这个很吓人的),如果结果变化频繁这个数据还会不准。
+			 * </p>
+			 */
 			metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
 			metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
 			metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
-		} else {
-			MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
-			Object parameterObject = invocation.getArgs()[1];
-			RowBounds rowBounds = (RowBounds) invocation.getArgs()[2];
-			if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
-				return invocation.proceed();
-			}
-
-			BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
-			String originalSql = (String) boundSql.getSql();
-
-			if (rowBounds instanceof Pagination) {
-				Connection connection = null;
-				try {
-					Pagination page = (Pagination) rowBounds;
-					if (page.isSearchCount()) {
-						connection = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();
-						CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
-								page.isOptimizeCount());
-						this.count(countOptimize.getCountSQL(), connection, mappedStatement, boundSql, page);
-						if (page.getTotal() <= 0) {
-							return invocation.proceed();
-						}
-					}
-				} finally {
-					IOUtils.closeQuietly(connection);
-				}
-			}
 		}
 
 		return invocation.proceed();
-
 	}
 
 	/**
 	 * 查询总记录条数
 	 *
 	 * @param sql
-	 * @param connection
 	 * @param mappedStatement
 	 * @param boundSql
 	 * @param page
 	 */
-	public void count(String sql, Connection connection, MappedStatement mappedStatement, BoundSql boundSql, Pagination page) {
-		PreparedStatement pstmt = null;
-		ResultSet rs = null;
+	protected void count(String sql, MappedStatement mappedStatement, BoundSql boundSql, Pagination page) {
+		PreparedStatement statement = null;
+		ResultSet resultSet = null;
+		Configuration configuration = mappedStatement.getConfiguration();
 		try {
-			pstmt = connection.prepareStatement(sql);
+			Connection connection = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();
+			statement = connection.prepareStatement(sql);
 			DefaultParameterHandler parameterHandler = new MybatisDefaultParameterHandler(mappedStatement,
 					boundSql.getParameterObject(), boundSql);
-			parameterHandler.setParameters(pstmt);
-			rs = pstmt.executeQuery();
+			parameterHandler.setParameters(statement);
+			resultSet = statement.executeQuery();
 			int total = 0;
-			if (rs.next()) {
-				total = rs.getInt(1);
+			if (resultSet.next()) {
+				total = resultSet.getInt(1);
 			}
 			page.setTotal(total);
 			/*
@@ -171,17 +152,14 @@ public class PaginationInterceptor implements Interceptor {
 				page.setTotal(total);
 			}
 		} catch (Exception e) {
-			logger.error("分页查询中count查询出错", e);
+			logger.error("Error: query page count total fail!", e);
 		} finally {
-			IOUtils.closeQuietly(pstmt);
-			IOUtils.closeQuietly(rs);
+			IOUtils.closeQuietly(statement);
+			IOUtils.closeQuietly(resultSet);
 		}
 	}
 
 	public Object plugin(Object target) {
-		if (target instanceof Executor) {
-			return Plugin.wrap(target, this);
-		}
 		if (target instanceof StatementHandler) {
 			return Plugin.wrap(target, this);
 		}

+ 1 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/SqlUtils.java

@@ -117,7 +117,7 @@ public class SqlUtils {
 	 * @return
 	 */
 	public static String concatOrderBy(String originalSql, Pagination page, boolean orderBy) {
-		if (orderBy && StringUtils.isNotEmpty(page.getOrderByField())) {
+		if (orderBy && StringUtils.isNotEmpty(page.getOrderByField()) && page.isOpenSort()) {
 			StringBuffer buildSql = new StringBuffer(originalSql);
 			buildSql.append(" ORDER BY ").append(page.getOrderByField());
 			buildSql.append(page.isAsc() ? " ASC " : " DESC ");

+ 1 - 1
mybatis-plus/src/test/resources/mysql-config.xml

@@ -62,7 +62,7 @@
          | dialectClazz 方言实现类
          |              自定义需要实现 com.baomidou.mybatisplus.plugins.pagination.IDialect 接口
          | -->
-        <plugin interceptor="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
+        <plugin interceptor="com.baomidou.mybatisplus.plugins.CachePaginationInterceptor">
             <property name="dialectType" value="mysql"/>
         </plugin>
         <!-- SQL 执行性能分析,开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长-->