This commit is contained in:
Michael Yang 2024-12-24 09:59:01 +08:00
commit 7ce553cc39
2 changed files with 26 additions and 6 deletions

View File

@ -133,7 +133,7 @@ public class MybatisFlexBootstrap {
* @return mapperObject * @return mapperObject
*/ */
public <T> T getMapper(Class<T> mapperClass) { public <T> T getMapper(Class<T> mapperClass) {
return Mappers.ofMapperClass(mapperClass); return Mappers.ofMapperClass(getEnvironmentId(), mapperClass);
} }

View File

@ -18,6 +18,7 @@ package com.mybatisflex.core.mybatis;
import com.mybatisflex.core.BaseMapper; import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.util.StringUtil;
import org.apache.ibatis.reflection.ExceptionUtil; import org.apache.ibatis.reflection.ExceptionUtil;
import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
@ -84,6 +85,13 @@ public class Mappers {
, new MapperHandler(mapperClass))); , new MapperHandler(mapperClass)));
return (M) mapperObject; return (M) mapperObject;
} }
public static <M> M ofMapperClass(String environmentId, Class<M> mapperClass) {
Object mapperObject = MapUtil.computeIfAbsent(MAPPER_OBJECTS, mapperClass, clazz ->
Proxy.newProxyInstance(mapperClass.getClassLoader()
, new Class[]{mapperClass}
, new MapperHandler(environmentId, mapperClass)));
return (M) mapperObject;
}
private static class MapperHandler implements InvocationHandler { private static class MapperHandler implements InvocationHandler {
@ -92,12 +100,24 @@ public class Mappers {
private final SqlSessionFactory sqlSessionFactory; private final SqlSessionFactory sqlSessionFactory;
public MapperHandler(Class<?> mapperClass) { public MapperHandler(Class<?> mapperClass) {
this(null, mapperClass);
}
public MapperHandler(String environmentId, Class<?> mapperClass) {
this.mapperClass = mapperClass; this.mapperClass = mapperClass;
this.executorType = FlexGlobalConfig.getDefaultConfig() if(StringUtil.noText(environmentId)) {
.getConfiguration() this.executorType = FlexGlobalConfig.getDefaultConfig()
.getDefaultExecutorType(); .getConfiguration()
this.sqlSessionFactory = FlexGlobalConfig.getDefaultConfig() .getDefaultExecutorType();
.getSqlSessionFactory(); this.sqlSessionFactory = FlexGlobalConfig.getDefaultConfig()
.getSqlSessionFactory();
} else {
this.executorType = FlexGlobalConfig.getConfig(environmentId)
.getConfiguration()
.getDefaultExecutorType();
this.sqlSessionFactory = FlexGlobalConfig.getConfig(environmentId)
.getSqlSessionFactory();
}
} }
private SqlSession openSession() { private SqlSession openSession() {