From 2cd43e4f197331530aadfc8e7a7ba3bb3389c41b Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 18 Jun 2023 16:54:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20count=20=E6=9F=A5=E8=AF=A2=E7=9A=84?= =?UTF-8?q?=E9=80=89=E9=A1=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/BaseMapper.java | 7 ++++++- .../com/mybatisflex/core/paginate/IPage.java | 18 ++++++++++++++++++ .../com/mybatisflex/core/paginate/Page.java | 12 ++++++++++++ .../com/mybatisflex/core/row/RowMapper.java | 7 ++++++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java index 096edf6a..60d9a420 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java @@ -675,7 +675,12 @@ public interface BaseMapper { // 这样方便用户做总数缓存,而非每次都要去查询总量 // 一般的分页场景中,只有第一页的时候有必要去查询总量,第二页以后是不需要的 if (page.getTotalRow() < 0) { - QueryWrapper countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper); + QueryWrapper countQueryWrapper; + if (page.isOptimizeCountSql()) { + countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper); + } else { + countQueryWrapper = MapperUtil.rawCountQueryWrapper(queryWrapper); + } page.setTotalRow(selectCountByQuery(countQueryWrapper)); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/IPage.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/IPage.java index ddf25861..111d3c03 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/IPage.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/IPage.java @@ -84,6 +84,24 @@ public interface IPage extends Serializable { */ void setRecords(List records); + /** + * 是否自动优化 COUNT 查询语句(默认优化)。 + * + * @return {@code true} 优化,{@code false} 不优化 + */ + default boolean isOptimizeCountSql() { + return true; + } + + /** + * 设置是否自动优化 COUNT 查询语句。 + * + * @param optimizeCountSql 是否优化 + */ + default void setOptimizeCountSql(boolean optimizeCountSql) { + // 默认总是优化 + } + /** * 获取总页数。 * diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/Page.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/Page.java index e8be0587..4ad603c8 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/Page.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/Page.java @@ -31,6 +31,8 @@ public class Page implements IPage { private long totalPage = INIT_VALUE; private long totalRow = INIT_VALUE; + private boolean optimizeCountSql = true; + public static Page of(int pageNumber, int pageSize) { return new Page<>(pageNumber, pageSize); } @@ -63,6 +65,16 @@ public class Page implements IPage { } + @Override + public boolean isOptimizeCountSql() { + return optimizeCountSql; + } + + @Override + public void setOptimizeCountSql(boolean optimizeCountSql) { + this.optimizeCountSql = optimizeCountSql; + } + @Override public List getRecords() { return records; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapper.java index ea418f20..4b98182e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapper.java @@ -440,7 +440,12 @@ public interface RowMapper { // 这样方便用户做总数缓存,而非每次都要去查询总量 // 一般的分页场景中,只有第一页的时候有必要去查询总量,第二页以后是不需要的 if (page.getTotalRow() < 0) { - QueryWrapper countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper); + QueryWrapper countQueryWrapper; + if (page.isOptimizeCountSql()) { + countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper); + } else { + countQueryWrapper = MapperUtil.rawCountQueryWrapper(queryWrapper); + } page.setTotalRow(selectCountByQuery(schema, tableName, countQueryWrapper)); }