mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
This commit is contained in:
parent
b98acf0b47
commit
1028bbd58c
@ -592,12 +592,14 @@ public interface BaseMapper<T> {
|
|||||||
* @return 实体类数据
|
* @return 实体类数据
|
||||||
*/
|
*/
|
||||||
default <R> R selectOneWithRelationsByIdAs(Serializable id, Class<R> asType) {
|
default <R> R selectOneWithRelationsByIdAs(Serializable id, Class<R> asType) {
|
||||||
|
R result;
|
||||||
try {
|
try {
|
||||||
MappedStatementTypes.setCurrentType(asType);
|
MappedStatementTypes.setCurrentType(asType);
|
||||||
return (R) selectOneWithRelationsById(id);
|
result = (R) selectOneById(id);
|
||||||
} finally {
|
} finally {
|
||||||
MappedStatementTypes.clear();
|
MappedStatementTypes.clear();
|
||||||
}
|
}
|
||||||
|
return MapperUtil.queryRelations(this, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -144,7 +144,7 @@ public class FlexConfiguration extends Configuration {
|
|||||||
//动态 resultsMap,方法名称为:selectListByQuery
|
//动态 resultsMap,方法名称为:selectListByQuery
|
||||||
Class<?> asType = MappedStatementTypes.getCurrentType();
|
Class<?> asType = MappedStatementTypes.getCurrentType();
|
||||||
//忽略掉查询 Rows 的方法
|
//忽略掉查询 Rows 的方法
|
||||||
if (asType != null && !id.endsWith("selectRowsByQuery")) {
|
if (asType != null) {
|
||||||
return MapUtil.computeIfAbsent(dynamicMappedStatementCache, id + ":" + asType.getName(),
|
return MapUtil.computeIfAbsent(dynamicMappedStatementCache, id + ":" + asType.getName(),
|
||||||
clazz -> replaceResultMap(ms, TableInfoFactory.ofEntityClass(asType))
|
clazz -> replaceResultMap(ms, TableInfoFactory.ofEntityClass(asType))
|
||||||
);
|
);
|
||||||
|
|||||||
@ -15,32 +15,23 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.core.mybatis;
|
package com.mybatisflex.core.mybatis;
|
||||||
|
|
||||||
import java.util.Stack;
|
|
||||||
|
|
||||||
public class MappedStatementTypes {
|
public class MappedStatementTypes {
|
||||||
|
|
||||||
private MappedStatementTypes() {
|
private MappedStatementTypes() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final ThreadLocal<Stack<Class<?>>> currentTypeTL = ThreadLocal.withInitial(Stack::new);
|
private static final ThreadLocal<Class<?>> currentTypeTL = new ThreadLocal<>();
|
||||||
|
|
||||||
public static void setCurrentType(Class<?> type) {
|
public static void setCurrentType(Class<?> type) {
|
||||||
currentTypeTL.get().push(type);
|
currentTypeTL.set(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class<?> getCurrentType() {
|
public static Class<?> getCurrentType() {
|
||||||
Stack<Class<?>> stack = currentTypeTL.get();
|
return currentTypeTL.get();
|
||||||
return stack.isEmpty() ? null : stack.lastElement();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void clear() {
|
public static void clear() {
|
||||||
Stack<Class<?>> stack = currentTypeTL.get();
|
|
||||||
if (!stack.isEmpty()) {
|
|
||||||
stack.pop();
|
|
||||||
}
|
|
||||||
if (stack.isEmpty()) {
|
|
||||||
currentTypeTL.remove();
|
currentTypeTL.remove();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user