From 0cffd08ce7a8fe51afd42db7c453c90b2054cff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 16 May 2023 09:19:05 +0800 Subject: [PATCH] =?UTF-8?q?fixed=201.2.4=E6=96=B0=E5=A2=9E=E7=9A=84=20pagi?= =?UTF-8?q?nateAs=20=E5=BC=82=E5=B8=B8=20close=20#I73BP6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/core/BaseMapper.java | 14 ++++- .../core/mybatis/MappedStatementTypes.java | 6 +- .../mybatisflex/test/EntityTestStarter.java | 56 ++++++++++--------- 3 files changed, 43 insertions(+), 33 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 bc6b3480..3b0f777c 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 @@ -16,6 +16,7 @@ package com.mybatisflex.core; import com.mybatisflex.core.exception.FlexExceptions; +import com.mybatisflex.core.mybatis.MappedStatementTypes; import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.provider.EntitySqlProvider; import com.mybatisflex.core.query.CPI; @@ -615,7 +616,7 @@ public interface BaseMapper { //清除group by 去查询数据 CPI.setGroupByColumns(queryWrapper, null); - CPI.setSelectColumns(queryWrapper, Arrays.asList(count())); + CPI.setSelectColumns(queryWrapper, Collections.singletonList(count())); long count = selectCountByQuery(queryWrapper); page.setTotalRow(count); @@ -633,8 +634,15 @@ public interface BaseMapper { int offset = page.getPageSize() * (page.getPageNumber() - 1); queryWrapper.limit(offset, page.getPageSize()); - List records = selectListByQueryAs(queryWrapper, asType); - page.setRecords(records); + try { + // 调用内部方法,不走代理,需要主动设置 MappedStatementType + // fixed https://gitee.com/mybatis-flex/mybatis-flex/issues/I73BP6 + MappedStatementTypes.setCurrentType(asType); + List records = selectListByQueryAs(queryWrapper, asType); + page.setRecords(records); + }finally { + MappedStatementTypes.clear(); + } return page; } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MappedStatementTypes.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MappedStatementTypes.java index f3383262..09c68037 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MappedStatementTypes.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/MappedStatementTypes.java @@ -19,15 +19,15 @@ public class MappedStatementTypes { private static ThreadLocal> currentTypeTL = new ThreadLocal<>(); - static void setCurrentType(Class type){ + public static void setCurrentType(Class type){ currentTypeTL.set(type); } - static Class getCurrentType(){ + public static Class getCurrentType(){ return currentTypeTL.get(); } - static void clear(){ + public static void clear(){ currentTypeTL.remove(); } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java index 8b1dfa7c..38b02c03 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java @@ -25,7 +25,6 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import javax.sql.DataSource; -import java.util.List; import static com.mybatisflex.test.table.Tables.ACCOUNT; import static com.mybatisflex.test.table.Tables.ARTICLE; @@ -58,39 +57,42 @@ public class EntityTestStarter { // List accounts1 = myAccountMapper.selectAll(); - QueryWrapper wrapper = QueryWrapper.create().select().from(ACCOUNT) - .and(ACCOUNT.ID.ge(100).and(ACCOUNT.ID.ge(200))) - .and(ACCOUNT.ID.ge(100).and(ACCOUNT.ID.ge(200))) - .groupBy(ACCOUNT.ID); - - List accounts = accountMapper.selectListByQuery(wrapper); - System.out.println(accounts); - - QueryWrapper wrapper1 = QueryWrapper.create().select().from(ACCOUNT) -// .leftJoin(ARTICLE).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID).and(ACCOUNT.ID.ge(100))) - .and(ACCOUNT.ID.ge(100).when(false).and(ACCOUNT.ID.ge(100).when(false))); - - Page accounts1 = accountMapper.paginate(Page.of(1,1),wrapper1); - System.out.println(accounts1); - - - QueryWrapper wrapper2 = QueryWrapper.create().select(ACCOUNT.ID).from(ACCOUNT); - List objects = accountMapper.selectObjectListByQuery(wrapper2); - System.out.println(objects); - - - Object object = accountMapper.selectObjectByQuery(wrapper2); - System.out.println(object); - +// QueryWrapper wrapper = QueryWrapper.create().select().from(ACCOUNT) +// .and(ACCOUNT.ID.ge(100).and(ACCOUNT.ID.ge(200))) +// .and(ACCOUNT.ID.ge(100).and(ACCOUNT.ID.ge(200))) +// .groupBy(ACCOUNT.ID); +// +// List accounts = accountMapper.selectListByQuery(wrapper); +// System.out.println(accounts); +// +// QueryWrapper wrapper1 = QueryWrapper.create().select().from(ACCOUNT) +//// .leftJoin(ARTICLE).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID).and(ACCOUNT.ID.ge(100))) +// .and(ACCOUNT.ID.ge(100).when(false).and(ACCOUNT.ID.ge(100).when(false))); +// +// Page accounts1 = accountMapper.paginate(Page.of(1,1),wrapper1); +// System.out.println(accounts1); +// +// +// QueryWrapper wrapper2 = QueryWrapper.create().select(ACCOUNT.ID).from(ACCOUNT); +// List objects = accountMapper.selectObjectListByQuery(wrapper2); +// System.out.println(objects); +// +// +// Object object = accountMapper.selectObjectByQuery(wrapper2); +// System.out.println(object); +// QueryWrapper asWrapper = QueryWrapper.create() .select(ARTICLE.ALL_COLUMNS) .select(ACCOUNT.USER_NAME,ACCOUNT.AGE,ACCOUNT.BIRTHDAY) .from(ARTICLE) .leftJoin(ACCOUNT).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID)) .where(ACCOUNT.ID.ge(0)); +// +// List articleDTOS = accountMapper.selectListByQueryAs(asWrapper, ArticleDTO.class); +// System.out.println(articleDTOS); + Page paginate = accountMapper.paginateAs(Page.of(1, 1), asWrapper, ArticleDTO.class); + System.out.println(paginate); - List articleDTOS = accountMapper.selectListByQueryAs(asWrapper, ArticleDTO.class); - System.out.println(articleDTOS); // QueryWrapper queryWrapper = new QueryWrapper();