瀏覽代碼

优化sqlSessionFactory获取.

https://github.com/baomidou/mybatis-plus/issues/5400
nieqiurong 2 年之前
父節點
當前提交
7ffdf415e7

+ 3 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/config/GlobalConfig.java

@@ -67,7 +67,10 @@ public class GlobalConfig implements Serializable {
     private Class<?> superMapperClass = Mapper.class;
     private Class<?> superMapperClass = Mapper.class;
     /**
     /**
      * 仅用于缓存 SqlSessionFactory(外部勿进行set,set了也没用)
      * 仅用于缓存 SqlSessionFactory(外部勿进行set,set了也没用)
+     *
+     * @deprecated 3.5.3
      */
      */
+    @Deprecated
     private SqlSessionFactory sqlSessionFactory;
     private SqlSessionFactory sqlSessionFactory;
     /**
     /**
      * 缓存已注入CRUD的Mapper信息
      * 缓存已注入CRUD的Mapper信息

+ 3 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java

@@ -99,9 +99,12 @@ public class TableInfo implements Constants {
     private String currentNamespace;
     private String currentNamespace;
     /**
     /**
      * MybatisConfiguration 标记 (Configuration内存地址值)
      * MybatisConfiguration 标记 (Configuration内存地址值)
+     *
+     * @deprecated 3.5.3 初始化阶段可以使用一下,后期尽量避免在容器初始化完成之后再继续调用此方法
      */
      */
     @Getter
     @Getter
     @Setter(AccessLevel.NONE)
     @Setter(AccessLevel.NONE)
+    @Deprecated
     private Configuration configuration;
     private Configuration configuration;
     /**
     /**
      * 是否开启下划线转驼峰
      * 是否开启下划线转驼峰

+ 6 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java

@@ -68,6 +68,7 @@ import static java.util.stream.Collectors.toList;
  * @since 2016-09-09
  * @since 2016-09-09
  */
  */
 public class TableInfoHelper {
 public class TableInfoHelper {
+
     private static final Log logger = LogFactory.getLog(TableInfoHelper.class);
     private static final Log logger = LogFactory.getLog(TableInfoHelper.class);
 
 
     /**
     /**
@@ -633,4 +634,9 @@ public class TableInfoHelper {
         return new SelectKeyGenerator(mappedStatement, true);
         return new SelectKeyGenerator(mappedStatement, true);
     }
     }
 
 
+    public static void clearCache(){
+        TABLE_INFO_CACHE.clear();
+        TABLE_NAME_INFO_CACHE.clear();
+    }
+
 }
 }

+ 2 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/GlobalConfigUtils.java

@@ -49,7 +49,9 @@ public class GlobalConfigUtils {
      * 获取当前的SqlSessionFactory
      * 获取当前的SqlSessionFactory
      *
      *
      * @param clazz 实体类
      * @param clazz 实体类
+     * @deprecated 3.5.3 尽量少用,后期取消此方法获取实例
      */
      */
+    @Deprecated
     public static SqlSessionFactory currentSessionFactory(Class<?> clazz) {
     public static SqlSessionFactory currentSessionFactory(Class<?> clazz) {
         Assert.notNull(clazz, "Class must not be null");
         Assert.notNull(clazz, "Class must not be null");
         TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
         TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);

+ 7 - 3
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.java

@@ -27,6 +27,7 @@ import org.apache.ibatis.binding.MapperMethod;
 import org.apache.ibatis.logging.Log;
 import org.apache.ibatis.logging.Log;
 import org.apache.ibatis.logging.LogFactory;
 import org.apache.ibatis.logging.LogFactory;
 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
 import org.mybatis.spring.SqlSessionUtils;
 import org.mybatis.spring.SqlSessionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
@@ -54,6 +55,9 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     @Autowired
     @Autowired
     protected M baseMapper;
     protected M baseMapper;
 
 
+    @Autowired
+    protected SqlSessionFactory sqlSessionFactory;
+
     @Override
     @Override
     public M getBaseMapper() {
     public M getBaseMapper() {
         return baseMapper;
         return baseMapper;
@@ -107,7 +111,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
      */
      */
     @Deprecated
     @Deprecated
     protected void closeSqlSession(SqlSession sqlSession) {
     protected void closeSqlSession(SqlSession sqlSession) {
-        SqlSessionUtils.closeSqlSession(sqlSession, GlobalConfigUtils.currentSessionFactory(entityClass));
+        SqlSessionUtils.closeSqlSession(sqlSession, this.sqlSessionFactory);
     }
     }
 
 
     /**
     /**
@@ -226,7 +230,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
      */
      */
     @Deprecated
     @Deprecated
     protected boolean executeBatch(Consumer<SqlSession> consumer) {
     protected boolean executeBatch(Consumer<SqlSession> consumer) {
-        return SqlHelper.executeBatch(this.entityClass, this.log, consumer);
+        return SqlHelper.executeBatch(this.sqlSessionFactory, this.entityClass, this.log, consumer);
     }
     }
 
 
     /**
     /**
@@ -240,7 +244,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
      * @since 3.3.1
      * @since 3.3.1
      */
      */
     protected <E> boolean executeBatch(Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
     protected <E> boolean executeBatch(Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
-        return SqlHelper.executeBatch(this.entityClass, this.log, list, batchSize, consumer);
+        return SqlHelper.executeBatch(this.sqlSessionFactory, this.entityClass, this.log, list, batchSize, consumer);
     }
     }
 
 
     /**
     /**

+ 7 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/spring/MybatisSqlSessionFactoryBean.java

@@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.core.MybatisPlusVersion;
 import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
 import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
 import com.baomidou.mybatisplus.core.MybatisXMLConfigBuilder;
 import com.baomidou.mybatisplus.core.MybatisXMLConfigBuilder;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
 import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
 import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
 import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
@@ -438,9 +439,14 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
         notNull(dataSource, "Property 'dataSource' is required");
         notNull(dataSource, "Property 'dataSource' is required");
         state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
         state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
             "Property 'configuration' and 'configLocation' can not specified with together");
             "Property 'configuration' and 'configLocation' can not specified with together");
+        clearCache();
+        this.sqlSessionFactory = buildSqlSessionFactory();
+    }
+
+    protected void clearCache() throws IOException {
         //TODO 清理掉资源  建议不要保留这个玩意了
         //TODO 清理掉资源  建议不要保留这个玩意了
         SqlRunner.DEFAULT.close();
         SqlRunner.DEFAULT.close();
-        this.sqlSessionFactory = buildSqlSessionFactory();
+        TableInfoHelper.clearCache();
     }
     }
 
 
     /**
     /**

+ 17 - 3
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java

@@ -71,7 +71,9 @@ public final class SqlHelper {
      * @param clazz 实体类
      * @param clazz 实体类
      * @return SqlSessionFactory
      * @return SqlSessionFactory
      * @since 3.3.0
      * @since 3.3.0
+     * @deprecated 3.5.3 尽量少用,后期取消此方法获取实例
      */
      */
+    @Deprecated
     public static SqlSessionFactory sqlSessionFactory(Class<?> clazz) {
     public static SqlSessionFactory sqlSessionFactory(Class<?> clazz) {
         return GlobalConfigUtils.currentSessionFactory(clazz);
         return GlobalConfigUtils.currentSessionFactory(clazz);
     }
     }
@@ -81,7 +83,9 @@ public final class SqlHelper {
      *
      *
      * @param clazz 实体类
      * @param clazz 实体类
      * @return SqlSession
      * @return SqlSession
+     * @deprecated 3.5.3 尽量少用,后期取消打开session方法
      */
      */
+    @Deprecated
     public static SqlSession sqlSession(Class<?> clazz) {
     public static SqlSession sqlSession(Class<?> clazz) {
         return SqlSessionUtils.getSqlSession(GlobalConfigUtils.currentSessionFactory(clazz));
         return SqlSessionUtils.getSqlSession(GlobalConfigUtils.currentSessionFactory(clazz));
     }
     }
@@ -163,9 +167,13 @@ public final class SqlHelper {
      * @return 操作结果
      * @return 操作结果
      * @since 3.4.0
      * @since 3.4.0
      */
      */
-    @SneakyThrows
+    @Deprecated
     public static boolean executeBatch(Class<?> entityClass, Log log, Consumer<SqlSession> consumer) {
     public static boolean executeBatch(Class<?> entityClass, Log log, Consumer<SqlSession> consumer) {
-        SqlSessionFactory sqlSessionFactory = sqlSessionFactory(entityClass);
+        return executeBatch(sqlSessionFactory(entityClass), entityClass, log, consumer);
+    }
+
+    @SneakyThrows
+    public static boolean executeBatch(SqlSessionFactory sqlSessionFactory, Class<?> entityClass, Log log, Consumer<SqlSession> consumer) {
         SqlSessionHolder sqlSessionHolder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sqlSessionFactory);
         SqlSessionHolder sqlSessionHolder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sqlSessionFactory);
         boolean transaction = TransactionSynchronizationManager.isSynchronizationActive();
         boolean transaction = TransactionSynchronizationManager.isSynchronizationActive();
         if (sqlSessionHolder != null) {
         if (sqlSessionHolder != null) {
@@ -211,10 +219,16 @@ public final class SqlHelper {
      * @param <E>         T
      * @param <E>         T
      * @return 操作结果
      * @return 操作结果
      * @since 3.4.0
      * @since 3.4.0
+     * @deprecated {@link #executeBatch(SqlSessionFactory, Class, Log, Collection, int, BiConsumer)}
      */
      */
+    @Deprecated
     public static <E> boolean executeBatch(Class<?> entityClass, Log log, Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
     public static <E> boolean executeBatch(Class<?> entityClass, Log log, Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
+        return executeBatch(sqlSessionFactory(entityClass), entityClass, log, list, batchSize, consumer);
+    }
+
+    public static <E> boolean executeBatch(SqlSessionFactory sqlSessionFactory, Class<?> entityClass, Log log, Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
         Assert.isFalse(batchSize < 1, "batchSize must not be less than one");
         Assert.isFalse(batchSize < 1, "batchSize must not be less than one");
-        return !CollectionUtils.isEmpty(list) && executeBatch(entityClass, log, sqlSession -> {
+        return !CollectionUtils.isEmpty(list) && executeBatch(sqlSessionFactory, entityClass, log, sqlSession -> {
             int size = list.size();
             int size = list.size();
             int idxLimit = Math.min(batchSize, size);
             int idxLimit = Math.min(batchSize, size);
             int i = 1;
             int i = 1;