From 209ec6ab60912047cf5e428619927399c8d10e41 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 18 Jun 2023 16:05:04 +0800 Subject: [PATCH 1/7] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E5=88=86?= =?UTF-8?q?=E9=A1=B5=E6=9F=A5=E8=AF=A2=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/BaseMapper.java | 73 +++++++++---------- .../com/mybatisflex/core/row/RowMapper.java | 58 ++++++--------- .../com/mybatisflex/core/util/MapperUtil.java | 25 ++++--- 3 files changed, 72 insertions(+), 84 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 b7dec258..096edf6a 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 @@ -20,7 +20,10 @@ import com.mybatisflex.core.field.FieldQueryBuilder; import com.mybatisflex.core.mybatis.MappedStatementTypes; import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.provider.EntitySqlProvider; -import com.mybatisflex.core.query.*; +import com.mybatisflex.core.query.CPI; +import com.mybatisflex.core.query.QueryColumn; +import com.mybatisflex.core.query.QueryCondition; +import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.util.*; @@ -667,47 +670,39 @@ public interface BaseMapper { default Page paginateAs(Page page, QueryWrapper queryWrapper, Class asType, Consumer>... consumers) { - List selectColumns = CPI.getSelectColumns(queryWrapper); - List orderBys = CPI.getOrderBys(queryWrapper); - List joins = CPI.getJoins(queryWrapper); - // 只有 totalRow 小于 0 的时候才会去查询总量 - // 这样方便用户做总数缓存,而非每次都要去查询总量 - // 一般的分页场景中,只有第一页的时候有必要去查询总量,第二页以后是不需要的 - if (page.getTotalRow() < 0) { - QueryWrapper countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper); - long count = selectCountByQuery(countQueryWrapper); - page.setTotalRow(count); - } + try { + // 只有 totalRow 小于 0 的时候才会去查询总量 + // 这样方便用户做总数缓存,而非每次都要去查询总量 + // 一般的分页场景中,只有第一页的时候有必要去查询总量,第二页以后是不需要的 + if (page.getTotalRow() < 0) { + QueryWrapper countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper); + page.setTotalRow(selectCountByQuery(countQueryWrapper)); + } + + if (page.isEmpty()) { + return page; + } + + int offset = page.getPageSize() * (page.getPageNumber() - 1); + queryWrapper.limit(offset, page.getPageSize()); + + List records; + if (asType != null) { + records = selectListByQueryAs(queryWrapper, asType); + } else { + records = (List) selectListByQuery(queryWrapper); + } + MapperUtil.queryFields(this, records, consumers); + page.setRecords(records); - if (page.isEmpty()) { return page; + + } finally { + // 将之前设置的 limit 清除掉 + // 保险起见把重置代码放到 finally 代码块中 + CPI.setLimitRows(queryWrapper, null); + CPI.setLimitOffset(queryWrapper, null); } - - //重置 selectColumns - CPI.setSelectColumns(queryWrapper, selectColumns); - //重置 orderBys - CPI.setOrderBys(queryWrapper, orderBys); - //重置 join - CPI.setJoins(queryWrapper, joins); - - int offset = page.getPageSize() * (page.getPageNumber() - 1); - queryWrapper.limit(offset, page.getPageSize()); - - if (asType != null) { - List records = selectListByQueryAs(queryWrapper, asType); - MapperUtil.queryFields(this, records, consumers); - page.setRecords(records); - } else { - List records = (List) selectListByQuery(queryWrapper); - MapperUtil.queryFields(this, records, consumers); - page.setRecords(records); - } - - // 将之前设置的 limit 清除掉 - CPI.setLimitRows(queryWrapper, null); - CPI.setLimitOffset(queryWrapper, null); - - return page; } 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 1c79619f..ea418f20 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 @@ -19,7 +19,9 @@ import com.mybatisflex.core.FlexConsts; import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.provider.RowSqlProvider; -import com.mybatisflex.core.query.*; +import com.mybatisflex.core.query.CPI; +import com.mybatisflex.core.query.QueryColumn; +import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.MapperUtil; import com.mybatisflex.core.util.StringUtil; @@ -431,47 +433,35 @@ public interface RowMapper { * @return */ default Page paginate(String schema, String tableName, Page page, QueryWrapper queryWrapper) { + try { + CPI.setFromIfNecessary(queryWrapper, schema, tableName); - CPI.setFromIfNecessary(queryWrapper, schema, tableName); + // 只有 totalRow 小于 0 的时候才会去查询总量 + // 这样方便用户做总数缓存,而非每次都要去查询总量 + // 一般的分页场景中,只有第一页的时候有必要去查询总量,第二页以后是不需要的 + if (page.getTotalRow() < 0) { + QueryWrapper countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper); + page.setTotalRow(selectCountByQuery(schema, tableName, countQueryWrapper)); + } - List selectColumns = CPI.getSelectColumns(queryWrapper); + if (page.isEmpty()) { + return page; + } - List orderBys = CPI.getOrderBys(queryWrapper); + int offset = page.getPageSize() * (page.getPageNumber() - 1); + queryWrapper.limit(offset, page.getPageSize()); - List joins = CPI.getJoins(queryWrapper); + page.setRecords(selectListByQuery(schema, tableName, queryWrapper)); - // 只有 totalRow 小于 0 的时候才会去查询总量 - // 这样方便用户做总数缓存,而非每次都要去查询总量 - // 一般的分页场景中,只有第一页的时候有必要去查询总量,第二页以后是不需要的 - if (page.getTotalRow() < 0) { - QueryWrapper countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper); - long count = selectCountByQuery(schema, tableName, countQueryWrapper); - page.setTotalRow(count); - } - - if (page.isEmpty()) { return page; + + } finally { + // 将之前设置的 limit 清除掉 + // 保险起见把重置代码放到 finally 代码块中 + CPI.setLimitRows(queryWrapper, null); + CPI.setLimitOffset(queryWrapper, null); } - //重置 selectColumns - CPI.setSelectColumns(queryWrapper, selectColumns); - //重置 orderBys - CPI.setOrderBys(queryWrapper, orderBys); - //重置 join - CPI.setJoins(queryWrapper, joins); - - int offset = page.getPageSize() * (page.getPageNumber() - 1); - queryWrapper.limit(offset, page.getPageSize()); - - List records = selectListByQuery(schema, tableName, queryWrapper); - page.setRecords(records); - - // 将之前设置的 limit 清除掉 - CPI.setLimitRows(queryWrapper, null); - CPI.setLimitOffset(queryWrapper, null); - - return page; - } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java index d8e2c1ea..3f8f40b8 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java @@ -42,7 +42,7 @@ public class MapperUtil { * *

      * {@code
-     * SELECT COUNT(*) AS `total` FROM ( ...用户构建的 SQL 语句... );
+     * SELECT COUNT(*) AS `total` FROM ( ...用户构建的 SQL 语句... ) AS `t`;
      * }
      * 
* @@ -58,22 +58,25 @@ public class MapperUtil { * 优化 COUNT 查询语句。 */ public static QueryWrapper optimizeCountQueryWrapper(QueryWrapper queryWrapper) { - List selectColumns = CPI.getSelectColumns(queryWrapper); - List groupByColumns = CPI.getGroupByColumns(queryWrapper); + // 对克隆对象进行操作,不影响原来的 QueryWrapper 对象 + QueryWrapper clone = queryWrapper.clone(); + // 将最后面的 order by 移除掉 + CPI.setOrderBys(clone, null); + // 获取查询列和分组列,用于判断是否进行优化 + List selectColumns = CPI.getSelectColumns(clone); + List groupByColumns = CPI.getGroupByColumns(clone); // 如果有 distinct 语句或者 group by 语句则不优化 // 这种一旦优化了就会造成 count 语句查询出来的值不对 if (hasDistinct(selectColumns) || hasGroupBy(groupByColumns)) { - return rawCountQueryWrapper(queryWrapper); + return rawCountQueryWrapper(clone); } // 判断能不能清除 join 语句 - if (canClearJoins(queryWrapper)) { - CPI.setJoins(queryWrapper, null); + if (canClearJoins(clone)) { + CPI.setJoins(clone, null); } - // 最后将最后面的 order by 移除掉 - // 将 select 里面的列换成 COUNT(*) AS `total` 就好了 - CPI.setOrderBys(queryWrapper, null); - CPI.setSelectColumns(queryWrapper, Collections.singletonList(count().as("total"))); - return queryWrapper; + // 将 select 里面的列换成 COUNT(*) AS `total` + CPI.setSelectColumns(clone, Collections.singletonList(count().as("total"))); + return clone; } private static boolean hasDistinct(List selectColumns) { From 859c2a601aa4451ce15b59db9e98633798daa88d Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 18 Jun 2023 16:37:19 +0800 Subject: [PATCH 2/7] =?UTF-8?q?refactor:=20=E5=AE=9A=E4=B9=89=20IPage=20?= =?UTF-8?q?=E5=88=86=E9=A1=B5=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/paginate/IPage.java | 142 ++++++++++++++++++ .../com/mybatisflex/core/paginate/Page.java | 15 +- 2 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/IPage.java 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 new file mode 100644 index 00000000..ddf25861 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/IPage.java @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.core.paginate; + +import java.io.Serializable; +import java.util.List; + +/** + * 分页接口。 + * + * @param 数据类型 + * @author 王帅 + * @since 2023-06-18 + */ +public interface IPage extends Serializable { + + /** + * 获取当前页码。 + * + * @return 页码 + */ + int getPageNumber(); + + /** + * 设置当前页码。 + * + * @param pageNumber 页码 + */ + void setPageNumber(int pageNumber); + + /** + * 获取当前每页数据数量。 + * + * @return 每页数据数量 + */ + int getPageSize(); + + /** + * 设置当前每页数据数量。 + * + * @param pageSize 每页数据数量 + */ + void setPageSize(int pageSize); + + /** + * 获取数据总数。 + * + * @return 数据总数 + */ + long getTotalRow(); + + /** + * 设置数据总数。 + * + * @param totalRow 数据总数 + */ + void setTotalRow(long totalRow); + + /** + * 获取当前页的数据。 + * + * @return 当前页的数据 + */ + List getRecords(); + + /** + * 设置当前页的数据。 + * + * @param records 当前页的数据 + */ + void setRecords(List records); + + /** + * 获取总页数。 + * + * @return 总页数 + */ + default long getTotalPage() { + // 实时计算总页数 + int pageSize = getPageSize(); + if (pageSize == 0) { + return 0L; + } + long totalRow = getTotalRow(); + long totalPage = totalRow / pageSize; + if (totalRow % pageSize != 0) { + totalPage++; + } + return totalPage; + } + + /** + * 设置总页数。 + * + * @param totalPage 总页数 + */ + default void setTotalPage(long totalPage) { + // 总页数是实时计算的,所以这里设置了也没用。 + } + + /** + * 是否存在上一页。 + * + * @return {@code true} 存在上一页,{@code false} 不存在上一页 + */ + default boolean hasPrevious() { + return getPageNumber() > 1; + } + + /** + * 是否存在下一页。 + * + * @return {@code true} 存在下一页,{@code false} 不存在下一页 + */ + default boolean hasNext() { + return getPageNumber() < getTotalPage(); + } + + /** + * 当前页是否为空。 + * + * @return {@code true} 空页,{@code false} 非空页 + */ + default boolean isEmpty() { + return getTotalRow() == 0 || getPageNumber() > getTotalPage(); + } + +} \ No newline at end of file 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 8b4e75d2..e8be0587 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 @@ -15,13 +15,12 @@ */ package com.mybatisflex.core.paginate; -import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.function.Function; -public class Page implements Serializable { +public class Page implements IPage { private static final long serialVersionUID = 1L; public static final int INIT_VALUE = -1; @@ -64,10 +63,12 @@ public class Page implements Serializable { } + @Override public List getRecords() { return records; } + @Override public void setRecords(List records) { if (records == null) { records = Collections.emptyList(); @@ -75,10 +76,12 @@ public class Page implements Serializable { this.records = records; } + @Override public int getPageNumber() { return pageNumber; } + @Override public void setPageNumber(int pageNumber) { if (pageNumber < 1) { throw new IllegalArgumentException("pageNumber must greater than or equal 1,current value is: " + pageNumber); @@ -87,10 +90,12 @@ public class Page implements Serializable { } + @Override public int getPageSize() { return pageSize; } + @Override public void setPageSize(int pageSize) { if (pageSize < 0) { throw new IllegalArgumentException("pageSize must greater than or equal 0,current value is: " + pageSize); @@ -99,18 +104,22 @@ public class Page implements Serializable { this.calcTotalPage(); } + @Override public long getTotalPage() { return totalPage; } + @Override public void setTotalPage(long totalPage) { this.totalPage = totalPage; } + @Override public long getTotalRow() { return totalRow; } + @Override public void setTotalRow(long totalRow) { this.totalRow = totalRow; this.calcTotalPage(); @@ -127,10 +136,12 @@ public class Page implements Serializable { } } + @Override public boolean isEmpty() { return getTotalRow() == 0 || getPageNumber() > getTotalPage(); } + @Override public boolean hasNext() { return getTotalPage() != 0 && getPageNumber() < getTotalPage(); } 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 3/7] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E4=BC=98=E5=8C=96=20count=20=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=9A=84=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)); } From 80615f9fc06b3526726e3c43682c3f4fa662e8b8 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 18 Jun 2023 16:55:24 +0800 Subject: [PATCH 4/7] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E4=BC=98=E5=8C=96=20count=20=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/test/mapper/UserMapperTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java index e03037e0..9e3079f1 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java @@ -143,10 +143,11 @@ class UserMapperTest { .leftJoin(USER_ROLE).as("ur").on(USER_ROLE.USER_ID.eq(USER.USER_ID)) .leftJoin(ROLE).as("r").on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID)); System.err.println(queryWrapper.toSQL()); - Page page; + Page page = Page.of(1, 1); + page.setOptimizeCountSql(false); int pageNumber = 0; do { - page = Page.of(++pageNumber, 1); + page.setPageNumber(page.getPageNumber() + 1); page = userMapper.paginateAs(page, queryWrapper, UserVO.class); System.err.println(page); } while (page.hasNext()); From 8876da2268546fcf11ffaf6b87d3fee4be134dae Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 18 Jun 2023 17:04:34 +0800 Subject: [PATCH 5/7] =?UTF-8?q?refactor:=20=E6=8A=BD=E5=8F=96=20getOffset?= =?UTF-8?q?=20=E5=85=AC=E7=94=A8=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mybatisflex/core/BaseMapper.java | 3 +-- .../main/java/com/mybatisflex/core/paginate/IPage.java | 9 +++++++++ .../main/java/com/mybatisflex/core/row/RowMapper.java | 3 +-- 3 files changed, 11 insertions(+), 4 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 60d9a420..fdda9737 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 @@ -688,8 +688,7 @@ public interface BaseMapper { return page; } - int offset = page.getPageSize() * (page.getPageNumber() - 1); - queryWrapper.limit(offset, page.getPageSize()); + queryWrapper.limit(page.getOffset(), page.getPageSize()); List records; if (asType != null) { 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 111d3c03..50e9a207 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,15 @@ public interface IPage extends Serializable { */ void setRecords(List records); + /** + * 获取当前分页偏移量。 + * + * @return 偏移量 + */ + default int getOffset() { + return getPageSize() * (getPageNumber() - 1); + } + /** * 是否自动优化 COUNT 查询语句(默认优化)。 * 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 4b98182e..9467ca92 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 @@ -453,8 +453,7 @@ public interface RowMapper { return page; } - int offset = page.getPageSize() * (page.getPageNumber() - 1); - queryWrapper.limit(offset, page.getPageSize()); + queryWrapper.limit(page.getOffset(), page.getPageSize()); page.setRecords(selectListByQuery(schema, tableName, queryWrapper)); From e845902d54cd74a1713fe620e421ccc28d4afc40 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 18 Jun 2023 17:52:09 +0800 Subject: [PATCH 6/7] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=20APT=20=E6=96=87?= =?UTF-8?q?=E6=A1=A3=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=AE=9E=E4=BD=93=E7=B1=BB?= =?UTF-8?q?=E4=B8=8D=E5=9C=A8=E5=90=8C=E4=B8=80=E4=B8=AA=E5=8C=85=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/others/apt.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/zh/others/apt.md b/docs/zh/others/apt.md index ffd02273..a67fd41f 100644 --- a/docs/zh/others/apt.md +++ b/docs/zh/others/apt.md @@ -132,14 +132,48 @@ processor.mappersPackage = com.your-package processor.baseMapperClass=com.domain.mapper.MyBaseMapper ``` +## 实体类不在一个包中 +有时候可能会遇到实体类不在同一个包中的情况,例如: + +```text +com.example.entityPackage1 + └─ Entity1 +com.example.entityPackage2 + └─ Entity2 +``` + +此时的辅助类会生成在对应的包下,例如: + +```text +com.example.entityPackage1.table + └─ Entity1TableDef +com.example.entityPackage2.table + └─ Entity2TableDef +``` + +但是,如果您设置了 `processor.allInTables=true` 的话,`Tables` 文件将会生成在最后一个包中,例如: + +```text +com.example.entityPackage2.table + └─ Tables +``` + +所以,如果您的实体类在多个包中,又指定了 `processor.allInTables=true` 选项,推荐设置 `Tables` 文件的位置,例如: + +```properties +processor.allInTables=true +processor.tablesPackage=com.example.entity.table +``` ## 和 Lombok、Mapstruct 整合 -在很多项目中,用到了 Lombok 帮我们减少代码编写,同时用到 Mapstruct 进行 bean 转换。使用到 Lombok 和 Mapstruct 时,其要求我们再 pom.xml 添加 `annotationProcessorPaths` 配置, +在很多项目中,用到了 Lombok 帮我们减少代码编写,同时用到 Mapstruct 进行 bean 转换。使用到 Lombok 和 Mapstruct 时,其要求我们再 +pom.xml 添加 `annotationProcessorPaths` 配置, 此时,我们也需要把 MyBatis-Flex 的 annotation 添加到 `annotationProcessorPaths` 配置里去,如下图所示: ```xml 24,25,26,27,28 + org.apache.maven.plugins maven-compiler-plugin From 23753d73ed624f0bef3ef23e6a8e66e06ba98bfd Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 18 Jun 2023 20:41:04 +0800 Subject: [PATCH 7/7] =?UTF-8?q?fix:=20=E4=BD=8E=E7=89=88=E6=9C=AC=20spring?= =?UTF-8?q?-cloud=20=E4=BD=BF=E7=94=A8=20bootstrap.yml=20=E6=8B=89?= =?UTF-8?q?=E5=8F=96=E4=B8=8D=E5=88=B0=20nacos=20=E4=B8=AD=E5=A4=9A?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=BA=90=E9=85=8D=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spring/boot/ConditionalOnMybatisFlexDatasource.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/ConditionalOnMybatisFlexDatasource.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/ConditionalOnMybatisFlexDatasource.java index 8066f807..604fcb4a 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/ConditionalOnMybatisFlexDatasource.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/ConditionalOnMybatisFlexDatasource.java @@ -19,6 +19,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.core.env.*; import org.springframework.core.type.AnnotatedTypeMetadata; @@ -34,6 +36,7 @@ import java.util.Iterator; @Conditional(ConditionalOnMybatisFlexDatasource.OnMybatisFlexDataSourceCondition.class) public @interface ConditionalOnMybatisFlexDatasource { + @Order(Ordered.HIGHEST_PRECEDENCE + 40) class OnMybatisFlexDataSourceCondition extends SpringBootCondition { @Override @@ -44,8 +47,8 @@ public @interface ConditionalOnMybatisFlexDatasource { Iterator> it = propertySources.stream().iterator(); while (it.hasNext()) { PropertySource ps = it.next(); - if (ps instanceof MapPropertySource) { - for (String propertyName : ((MapPropertySource) ps).getSource().keySet()) { + if (ps instanceof EnumerablePropertySource) { + for (String propertyName : ((EnumerablePropertySource) ps).getPropertyNames()) { if (propertyName.startsWith("mybatis-flex.datasource.")) { return ConditionOutcome.match(); }