|
@@ -16,9 +16,11 @@
|
|
|
package com.baomidou.mybatisplus.plugins;
|
|
|
|
|
|
import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
|
|
|
+import com.baomidou.mybatisplus.plugins.entity.CountOptimize;
|
|
|
import com.baomidou.mybatisplus.plugins.pagination.DialectFactory;
|
|
|
import com.baomidou.mybatisplus.plugins.pagination.IDialect;
|
|
|
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
|
|
|
+import com.baomidou.mybatisplus.toolkit.SqlUtils;
|
|
|
import com.baomidou.mybatisplus.toolkit.StringUtils;
|
|
|
import org.apache.ibatis.executor.parameter.ParameterHandler;
|
|
|
import org.apache.ibatis.executor.statement.StatementHandler;
|
|
@@ -124,32 +126,9 @@ public class PaginationInterceptor implements Interceptor {
|
|
|
/*
|
|
|
* COUNT 查询,去掉 ORDER BY 优化执行 SQL
|
|
|
*/
|
|
|
- StringBuffer countSql = new StringBuffer("SELECT COUNT(1) AS TOTAL ");
|
|
|
- if (page.isOptimizeCount()) {
|
|
|
- String tempSql = originalSql.replaceAll("(?i)ORDER[\\s]+BY", "ORDER BY");
|
|
|
- String indexOfSql = tempSql.toUpperCase();
|
|
|
- if (!indexOfSql.contains("DISTINCT")) {
|
|
|
- int formIndex = indexOfSql.indexOf("FROM");
|
|
|
- int orderByIndex = indexOfSql.lastIndexOf("ORDER BY");
|
|
|
- if (formIndex > -1) {
|
|
|
- // 无排序情况处理
|
|
|
- if (orderByIndex > -1) {
|
|
|
- tempSql = tempSql.substring(0, orderByIndex);
|
|
|
- countSql.append(tempSql.substring(formIndex));
|
|
|
- orderBy = false;
|
|
|
- } else {
|
|
|
- countSql.append(tempSql.substring(formIndex));
|
|
|
- }
|
|
|
- } else {
|
|
|
- countSql.append("FROM (").append(originalSql).append(") A");
|
|
|
- }
|
|
|
- } else {
|
|
|
- countSql.append("FROM (").append(originalSql).append(") A");
|
|
|
- }
|
|
|
- } else {
|
|
|
- countSql.append("FROM (").append(originalSql).append(") A");
|
|
|
- }
|
|
|
- page = this.count(countSql.toString(), connection, mappedStatement, boundSql, page);
|
|
|
+ CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, page.isOptimizeCount());
|
|
|
+ orderBy = countOptimize.isOrderBy();
|
|
|
+ page = this.count(countOptimize.getCountSQL(), connection, mappedStatement, boundSql, page);
|
|
|
/** 总数 0 跳出执行 */
|
|
|
if (page.getTotal() <= 0) {
|
|
|
return invocation.proceed();
|