diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/MybatisFlexBootstrap.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/MybatisFlexBootstrap.java index bc7be1d2..fd05c1ec 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/MybatisFlexBootstrap.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/MybatisFlexBootstrap.java @@ -133,7 +133,7 @@ public class MybatisFlexBootstrap { * @return mapperObject */ public T getMapper(Class mapperClass) { - return Mappers.ofMapperClass(mapperClass); + return Mappers.ofMapperClass(getEnvironmentId(), mapperClass); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java index 3e254b74..89b1da1d 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/Mappers.java @@ -18,6 +18,7 @@ package com.mybatisflex.core.mybatis; import com.mybatisflex.core.BaseMapper; import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.exception.FlexExceptions; +import com.mybatisflex.core.util.StringUtil; import org.apache.ibatis.reflection.ExceptionUtil; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; @@ -84,6 +85,13 @@ public class Mappers { , new MapperHandler(mapperClass))); return (M) mapperObject; } + public static M ofMapperClass(String environmentId, Class 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 { @@ -92,12 +100,24 @@ public class Mappers { private final SqlSessionFactory sqlSessionFactory; public MapperHandler(Class mapperClass) { + this(null, mapperClass); + } + + public MapperHandler(String environmentId, Class mapperClass) { this.mapperClass = mapperClass; - this.executorType = FlexGlobalConfig.getDefaultConfig() - .getConfiguration() - .getDefaultExecutorType(); - this.sqlSessionFactory = FlexGlobalConfig.getDefaultConfig() - .getSqlSessionFactory(); + if(StringUtil.noText(environmentId)) { + this.executorType = FlexGlobalConfig.getDefaultConfig() + .getConfiguration() + .getDefaultExecutorType(); + this.sqlSessionFactory = FlexGlobalConfig.getDefaultConfig() + .getSqlSessionFactory(); + } else { + this.executorType = FlexGlobalConfig.getConfig(environmentId) + .getConfiguration() + .getDefaultExecutorType(); + this.sqlSessionFactory = FlexGlobalConfig.getConfig(environmentId) + .getSqlSessionFactory(); + } } private SqlSession openSession() {