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 bd66b0f2..95710909 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 @@ -592,12 +592,14 @@ public interface BaseMapper { * @return 实体类数据 */ default R selectOneWithRelationsByIdAs(Serializable id, Class asType) { + R result; try { MappedStatementTypes.setCurrentType(asType); - return (R) selectOneWithRelationsById(id); + result = (R) selectOneById(id); } finally { MappedStatementTypes.clear(); } + return MapperUtil.queryRelations(this, result); } /** diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java index c86c181c..a4495b27 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java @@ -144,7 +144,7 @@ public class FlexConfiguration extends Configuration { //动态 resultsMap,方法名称为:selectListByQuery Class asType = MappedStatementTypes.getCurrentType(); //忽略掉查询 Rows 的方法 - if (asType != null && !id.endsWith("selectRowsByQuery")) { + if (asType != null) { return MapUtil.computeIfAbsent(dynamicMappedStatementCache, id + ":" + asType.getName(), clazz -> replaceResultMap(ms, TableInfoFactory.ofEntityClass(asType)) ); 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 903a2cbf..bcba101c 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 @@ -15,32 +15,23 @@ */ package com.mybatisflex.core.mybatis; -import java.util.Stack; - public class MappedStatementTypes { private MappedStatementTypes() { } - private static final ThreadLocal>> currentTypeTL = ThreadLocal.withInitial(Stack::new); + private static final ThreadLocal> currentTypeTL = new ThreadLocal<>(); public static void setCurrentType(Class type) { - currentTypeTL.get().push(type); + currentTypeTL.set(type); } public static Class getCurrentType() { - Stack> stack = currentTypeTL.get(); - return stack.isEmpty() ? null : stack.lastElement(); + return currentTypeTL.get(); } public static void clear() { - Stack> stack = currentTypeTL.get(); - if (!stack.isEmpty()) { - stack.pop(); - } - if (stack.isEmpty()) { - currentTypeTL.remove(); - } + currentTypeTL.remove(); } } diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/core/util/MappedStatementTypesTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/core/util/MappedStatementTypesTest.java deleted file mode 100644 index 25e6330f..00000000 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/core/util/MappedStatementTypesTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.mybatisflex.core.util; - -import com.mybatisflex.core.mybatis.MappedStatementTypes; -import org.junit.Assert; -import org.junit.Test; - -public class MappedStatementTypesTest { - - @Test - public void test() { - MappedStatementTypes.clear(); - - MappedStatementTypes.setCurrentType(String.class); - MappedStatementTypes.setCurrentType(MappedStatementTypesTest.class); - MappedStatementTypes.setCurrentType(StringUtilTest.class); - - Assert.assertEquals(StringUtilTest.class, MappedStatementTypes.getCurrentType()); - System.out.println(MappedStatementTypes.getCurrentType()); - MappedStatementTypes.clear(); - - Assert.assertEquals(MappedStatementTypesTest.class, MappedStatementTypes.getCurrentType()); - System.out.println(MappedStatementTypes.getCurrentType()); - MappedStatementTypes.clear(); - - Assert.assertEquals(String.class, MappedStatementTypes.getCurrentType()); - System.out.println(MappedStatementTypes.getCurrentType()); - MappedStatementTypes.clear(); - - Assert.assertNull(MappedStatementTypes.getCurrentType()); - System.out.println(MappedStatementTypes.getCurrentType()); - } -}