From b91f84632e40c9bbdf50aa988e77d1d427d9087f Mon Sep 17 00:00:00 2001 From: wcc <1842281311@qq.com> Date: Mon, 23 Dec 2024 16:58:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=A4=9A=20MybatisFlexBootst?= =?UTF-8?q?rap=20=E5=AE=9E=E4=BE=8B=E6=97=B6=E8=B0=83=E7=94=A8=20MybatisFl?= =?UTF-8?q?exBootstrap.getMapper(Class=20mapperClass)=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=8F=AA=E8=83=BD=E8=8E=B7=E5=8F=96=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E7=9A=84=E5=AE=9E=E4=BE=8B=E7=9A=84=20mapper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed the problem that when calling MybatisFlexBootstrap.getMapper(Class mapperClass) method with multiple MybatisFlexBootstrap instances, only the mapper of the last instance can be obtained. --- .../core/MybatisFlexBootstrap.java | 2 +- .../com/mybatisflex/core/mybatis/Mappers.java | 30 +++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) 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() {