mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
feat: add support nested types for MappedStatementTypes.java
This commit is contained in:
parent
417b04838b
commit
012381510d
@ -15,23 +15,32 @@
|
|||||||
*/
|
*/
|
||||||
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<Class<?>> currentTypeTL = new ThreadLocal<>();
|
private static final ThreadLocal<Stack<Class<?>>> currentTypeTL = ThreadLocal.withInitial(Stack::new);
|
||||||
|
|
||||||
public static void setCurrentType(Class<?> type) {
|
public static void setCurrentType(Class<?> type) {
|
||||||
currentTypeTL.set(type);
|
currentTypeTL.get().push(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class<?> getCurrentType() {
|
public static Class<?> getCurrentType() {
|
||||||
return currentTypeTL.get();
|
Stack<Class<?>> stack = 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();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,32 @@
|
|||||||
|
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