소스 검색

移除 Mapper 相关缓存,支持 GroovyClassLoader 动态注入 Mapper

hubin 3 년 전
부모
커밋
07ab27689e

+ 29 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisConfiguration.java

@@ -15,6 +15,10 @@
  */
 package com.baomidou.mybatisplus.core;
 
+import com.baomidou.mybatisplus.core.mapper.Mapper;
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
+import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
+import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
 import lombok.Getter;
 import lombok.Setter;
 import org.apache.ibatis.binding.MapperRegistry;
@@ -37,7 +41,9 @@ import org.apache.ibatis.transaction.Transaction;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 import java.util.function.BiFunction;
+import java.util.stream.Collectors;
 
 /**
  * replace default Configuration class
@@ -119,6 +125,29 @@ public class MybatisConfiguration extends Configuration {
         mybatisMapperRegistry.addMapper(type);
     }
 
+    /**
+     * 移除 Mapper 相关缓存,支持 GroovyClassLoader 动态注入 Mapper
+     *
+     * @param type Mapper Type
+     * @param <T>
+     */
+    public <T> void removeMapper(Class<T> type) {
+        // TODO
+        // 清空实体表信息映射信息
+        TableInfoHelper.remove(ReflectionKit.getSuperClassGenericType(type, Mapper.class, 0));
+
+        // 清空 Mapper 缓存信息
+        this.mybatisMapperRegistry.removeMapper(type);
+        this.loadedResources.remove(type.toString());
+        GlobalConfigUtils.getGlobalConfig(this).getMapperRegistryCache().remove(type.toString());
+
+        // 清空 Mapper 方法 mappedStatement 缓存信息
+        Set<String> mapperSet = mappedStatements.entrySet().stream().map(t -> t.getKey()).collect(Collectors.toSet());
+        if (null != mapperSet && !mapperSet.isEmpty()) {
+            mapperSet.forEach(key -> mappedStatements.remove(key));
+        }
+    }
+
     /**
      * 使用自己的 MybatisMapperRegistry
      */

+ 9 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisMapperRegistry.java

@@ -62,6 +62,15 @@ public class MybatisMapperRegistry extends MapperRegistry {
         return knownMappers.containsKey(type);
     }
 
+    /**
+     * 清空 Mapper 缓存信息
+     */
+    protected <T> void removeMapper(Class<T> type) {
+        // TODO
+        knownMappers.entrySet().stream().filter(t -> t.getKey().getName().equals(type.getName()))
+            .findFirst().ifPresent(t -> knownMappers.remove(t.getKey()));
+    }
+
     @Override
     public <T> void addMapper(Class<T> type) {
         if (type.isInterface()) {

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

@@ -122,6 +122,15 @@ public class TableInfoHelper {
         return Collections.unmodifiableList(new ArrayList<>(TABLE_INFO_CACHE.values()));
     }
 
+    /**
+     * 清空实体表映射缓存信息
+     *
+     * @param entityClass 实体 Class
+     */
+    public static void remove(Class<?> entityClass) {
+        TABLE_INFO_CACHE.remove(entityClass);
+    }
+
     /**
      * <p>
      * 实体类反射获取表信息【初始化】