浏览代码

支持自定义SqlSessionTemplate

https://github.com/baomidou/mybatis-plus/issues/6766
nieqiurong 1 月之前
父节点
当前提交
a0e2ed0b6a

+ 2 - 0
mybatis-plus-core/build.gradle

@@ -7,4 +7,6 @@ dependencies {
     implementation "${lib.'imadcn'}"
 
     testImplementation "${lib.'logback-classic'}"
+    testImplementation "${lib."mybatis-spring"}"
+    testImplementation "${lib.'spring-jdbc'}"
 }

+ 4 - 8
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/MybatisUtils.java

@@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.core.handlers.IJsonTypeHandler;
 import com.baomidou.mybatisplus.core.override.MybatisMapperProxy;
 import lombok.experimental.UtilityClass;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.reflection.MetaObject;
+import org.apache.ibatis.reflection.SystemMetaObject;
 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.session.defaults.DefaultSqlSession;
@@ -69,14 +71,8 @@ public class MybatisUtils {
             // TODO 原生mybatis下只能这样了.
             return GlobalConfigUtils.getGlobalConfig(mybatisMapperProxy.getSqlSession().getConfiguration()).getSqlSessionFactory();
         }
-        Field declaredField;
-        try {
-            declaredField = sqlSession.getClass().getDeclaredField("sqlSessionFactory");
-            declaredField.setAccessible(true);
-            return (SqlSessionFactory) declaredField.get(sqlSession);
-        } catch (NoSuchFieldException | IllegalAccessException e) {
-            throw new RuntimeException(e);
-        }
+        MetaObject metaObject = SystemMetaObject.forObject(sqlSession);
+        return (SqlSessionFactory) metaObject.getValue("sqlSessionFactory");
     }
 
     /**

+ 79 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/toolkit/MybatisUtilsTest.java

@@ -0,0 +1,79 @@
+package com.baomidou.mybatisplus.core.toolkit;
+
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
+import com.baomidou.mybatisplus.core.config.GlobalConfig;
+import com.baomidou.mybatisplus.core.override.MybatisMapperProxy;
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.mapping.Environment;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.session.SqlSessionManager;
+import org.apache.ibatis.session.defaults.DefaultSqlSession;
+import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+import org.mybatis.spring.SqlSessionTemplate;
+
+import java.util.HashMap;
+
+/**
+ * @author nieqiurong
+ */
+public class MybatisUtilsTest {
+
+    interface MyMapper {
+
+    }
+
+    @Test
+    void testGetSqlSessionFactoryByDefaultSqlSession() {
+        var configuration = getMybatisConfiguration();
+        var globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
+        globalConfig.setSqlSessionFactory(Mockito.mock(SqlSessionFactory.class));
+        GlobalConfigUtils.setGlobalConfig(configuration, globalConfig);
+        var sqlSession = new DefaultSqlSession(configuration, Mockito.mock(Executor.class));
+        var mybatisMapperProxy = new MybatisMapperProxy<>(sqlSession, MyMapper.class, new HashMap<>());
+        SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory(mybatisMapperProxy);
+        Assertions.assertNotNull(sqlSessionFactory);
+    }
+
+    @Test
+    void testGetSqlSessionFactoryBySqlSessionManager() {
+        var sqlSession = SqlSessionManager.newInstance(Mockito.mock(SqlSessionFactory.class));
+        var mybatisMapperProxy = new MybatisMapperProxy<>(sqlSession, MyMapper.class, new HashMap<>());
+        SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory(mybatisMapperProxy);
+        Assertions.assertNotNull(sqlSessionFactory);
+    }
+
+    @Test
+    void testGetSqlSessionFactoryBySqlSessionTemplate() {
+        var sqlSession = new SqlSessionTemplate(getDefaultSqlSessionFactory());
+        var mybatisMapperProxy = new MybatisMapperProxy<>(sqlSession, MyMapper.class, new HashMap<>());
+        Assertions.assertNotNull(MybatisUtils.getSqlSessionFactory(mybatisMapperProxy));
+    }
+
+    static class MySqlSessionTemplate extends SqlSessionTemplate {
+
+        public MySqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
+            super(sqlSessionFactory);
+        }
+    }
+
+    @Test
+    void testGetSqlSessionFactoryByExtendSqlSessionTemplate() {
+        var sqlSession = new MySqlSessionTemplate(getDefaultSqlSessionFactory());
+        var mybatisMapperProxy = new MybatisMapperProxy<>(sqlSession, MyMapper.class, new HashMap<>());
+        Assertions.assertNotNull(MybatisUtils.getSqlSessionFactory(mybatisMapperProxy));
+    }
+
+    private SqlSessionFactory getDefaultSqlSessionFactory() {
+        return new DefaultSqlSessionFactory(getMybatisConfiguration());
+    }
+
+    private MybatisConfiguration getMybatisConfiguration() {
+        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration(Mockito.mock(Environment.class));
+        mybatisConfiguration.addMapper(MyMapper.class);
+        return mybatisConfiguration;
+    }
+
+}