From ba43579c6846c6194063569cc270aa32ac253d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Mon, 19 Jun 2023 17:03:32 +0800 Subject: [PATCH] optimize Page.java --- .../java/com/mybatisflex/core/BaseMapper.java | 4 +- .../com/mybatisflex/core/paginate/IPage.java | 169 ------------------ .../com/mybatisflex/core/paginate/Page.java | 138 +++++++++++--- .../com/mybatisflex/core/row/RowMapper.java | 4 +- .../test/mapper/UserMapperTest.java | 2 +- 5 files changed, 119 insertions(+), 198 deletions(-) delete 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/BaseMapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java index fdda9737..56a577ec 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 @@ -676,7 +676,7 @@ public interface BaseMapper { // 一般的分页场景中,只有第一页的时候有必要去查询总量,第二页以后是不需要的 if (page.getTotalRow() < 0) { QueryWrapper countQueryWrapper; - if (page.isOptimizeCountSql()) { + if (page.needOptimizeCountQuery()) { countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper); } else { countQueryWrapper = MapperUtil.rawCountQueryWrapper(queryWrapper); @@ -688,7 +688,7 @@ public interface BaseMapper { return page; } - queryWrapper.limit(page.getOffset(), page.getPageSize()); + queryWrapper.limit(page.offset(), 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 deleted file mode 100644 index 50e9a207..00000000 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/IPage.java +++ /dev/null @@ -1,169 +0,0 @@ -/* - * 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 int getOffset() { - return getPageSize() * (getPageNumber() - 1); - } - - /** - * 是否自动优化 COUNT 查询语句(默认优化)。 - * - * @return {@code true} 优化,{@code false} 不优化 - */ - default boolean isOptimizeCountSql() { - return true; - } - - /** - * 设置是否自动优化 COUNT 查询语句。 - * - * @param optimizeCountSql 是否优化 - */ - default void setOptimizeCountSql(boolean optimizeCountSql) { - // 默认总是优化 - } - - /** - * 获取总页数。 - * - * @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 4ad603c8..d115f40a 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,12 +15,18 @@ */ 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 IPage { +/** + * 分页对象 + * + * @param 对象类型 + */ +public class Page implements Serializable { private static final long serialVersionUID = 1L; public static final int INIT_VALUE = -1; @@ -31,7 +37,7 @@ public class Page implements IPage { private long totalPage = INIT_VALUE; private long totalRow = INIT_VALUE; - private boolean optimizeCountSql = true; + private boolean optimizeCountQuery = true; public static Page of(int pageNumber, int pageSize) { return new Page<>(pageNumber, pageSize); @@ -56,7 +62,6 @@ public class Page implements IPage { this.setTotalRow(totalRow); } - public Page(List records, int pageNumber, int pageSize, long totalRow) { this.setRecords(records); this.setPageNumber(pageNumber); @@ -65,22 +70,22 @@ public class Page implements IPage { } - @Override - public boolean isOptimizeCountSql() { - return optimizeCountSql; - } - @Override - public void setOptimizeCountSql(boolean optimizeCountSql) { - this.optimizeCountSql = optimizeCountSql; - } - - @Override + /** + * 获取当前页的数据。 + * + * @return 当前页的数据 + */ public List getRecords() { return records; } - @Override + + /** + * 设置当前页的数据。 + * + * @param records 当前页的数据 + */ public void setRecords(List records) { if (records == null) { records = Collections.emptyList(); @@ -88,12 +93,21 @@ public class Page implements IPage { this.records = records; } - @Override + /** + * 获取当前页码。 + * + * @return 页码 + */ public int getPageNumber() { return pageNumber; } - @Override + + /** + * 设置当前页码。 + * + * @param pageNumber 页码 + */ public void setPageNumber(int pageNumber) { if (pageNumber < 1) { throw new IllegalArgumentException("pageNumber must greater than or equal 1,current value is: " + pageNumber); @@ -102,12 +116,21 @@ public class Page implements IPage { } - @Override + /** + * 获取当前每页数据数量。 + * + * @return 每页数据数量 + */ public int getPageSize() { return pageSize; } - @Override + + /** + * 设置当前每页数据数量。 + * + * @param pageSize 每页数据数量 + */ public void setPageSize(int pageSize) { if (pageSize < 0) { throw new IllegalArgumentException("pageSize must greater than or equal 0,current value is: " + pageSize); @@ -116,22 +139,39 @@ public class Page implements IPage { this.calcTotalPage(); } - @Override + /** + * 获取数据总数。 + * + * @return 数据总数 + */ public long getTotalPage() { return totalPage; } - @Override + /** + * 设置总页数。 + * + * @param totalPage 总页数 + */ public void setTotalPage(long totalPage) { this.totalPage = totalPage; } - @Override + /** + * 获取数据总数。 + * + * @return 数据总数 + */ public long getTotalRow() { return totalRow; } - @Override + + /** + * 设置数据总数。 + * + * @param totalRow 数据总数 + */ public void setTotalRow(long totalRow) { this.totalRow = totalRow; this.calcTotalPage(); @@ -148,17 +188,67 @@ public class Page implements IPage { } } - @Override + + /** + * 当前页是否为空。 + * + * @return {@code true} 空页,{@code false} 非空页 + */ public boolean isEmpty() { return getTotalRow() == 0 || getPageNumber() > getTotalPage(); } - @Override + + /** + * 是否存在下一页。 + * + * @return {@code true} 存在下一页,{@code false} 不存在下一页 + */ public boolean hasNext() { return getTotalPage() != 0 && getPageNumber() < getTotalPage(); } + /** + * 是否存在上一页。 + * + * @return {@code true} 存在上一页,{@code false} 不存在上一页 + */ + public boolean hasPrevious() { + return getPageNumber() > 1; + } + + + /** + * 获取当前分页偏移量。 + * + * @return 偏移量 + */ + public int offset() { + return getPageSize() * (getPageNumber() - 1); + } + + + /** + * 设置是否自动优化 COUNT 查询语句。 + * + * @param optimizeCountQuery 是否优化 + */ + public void setOptimizeCountQuery(boolean optimizeCountQuery) { + this.optimizeCountQuery = optimizeCountQuery; + } + + + /** + * 是否自动优化 COUNT 查询语句(默认优化)。 + * + * @return {@code true} 优化,{@code false} 不优化 + */ + public boolean needOptimizeCountQuery() { + return optimizeCountQuery; + } + + public Page map(Function mapper) { Page newPage = new Page<>(); newPage.pageNumber = pageNumber; 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 9467ca92..6e85d43f 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 @@ -441,7 +441,7 @@ public interface RowMapper { // 一般的分页场景中,只有第一页的时候有必要去查询总量,第二页以后是不需要的 if (page.getTotalRow() < 0) { QueryWrapper countQueryWrapper; - if (page.isOptimizeCountSql()) { + if (page.needOptimizeCountQuery()) { countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper); } else { countQueryWrapper = MapperUtil.rawCountQueryWrapper(queryWrapper); @@ -453,7 +453,7 @@ public interface RowMapper { return page; } - queryWrapper.limit(page.getOffset(), page.getPageSize()); + queryWrapper.limit(page.offset(), page.getPageSize()); page.setRecords(selectListByQuery(schema, tableName, queryWrapper)); 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 9e3079f1..1fa82b23 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 @@ -144,7 +144,7 @@ class UserMapperTest { .leftJoin(ROLE).as("r").on(USER_ROLE.ROLE_ID.eq(ROLE.ROLE_ID)); System.err.println(queryWrapper.toSQL()); Page page = Page.of(1, 1); - page.setOptimizeCountSql(false); + page.setOptimizeCountQuery(false); int pageNumber = 0; do { page.setPageNumber(page.getPageNumber() + 1);