浏览代码

处理removeMapper资源移除.

https://github.com/baomidou/mybatis-plus/issues/6442
nieqiurong 8 月之前
父节点
当前提交
9be6278118

+ 13 - 7
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisConfiguration.java

@@ -133,8 +133,9 @@ public class MybatisConfiguration extends Configuration {
      * 新增注入新的 Mapper 信息,新增前会清理之前的缓存信息
      *
      * @param type Mapper Type
-     * @param <T>
+     * @deprecated 3.5.8 不建议在实际生产环境中使用.
      */
+    @Deprecated
     public <T> void addNewMapper(Class<T> type) {
         this.removeMapper(type);
         this.addMapper(type);
@@ -144,8 +145,9 @@ public class MybatisConfiguration extends Configuration {
      * 移除 Mapper 相关缓存,支持 GroovyClassLoader 动态注入 Mapper
      *
      * @param type Mapper Type
-     * @param <T>
+     * @deprecated 3.5.8 不建议在实际生产环境中使用.
      */
+    @Deprecated
     public <T> void removeMapper(Class<T> type) {
         Set<String> mapperRegistryCache = GlobalConfigUtils.getGlobalConfig(this).getMapperRegistryCache();
         final String mapperType = type.toString();
@@ -156,14 +158,18 @@ public class MybatisConfiguration extends Configuration {
             // 清空 Mapper 缓存信息
             this.mybatisMapperRegistry.removeMapper(type);
             this.loadedResources.remove(type.toString());
+            this.loadedResources.remove(type.getName().replace(StringPool.DOT, StringPool.SLASH) + ".xml");
             mapperRegistryCache.remove(mapperType);
 
             // 清空 Mapper 方法 mappedStatement 缓存信息
-            final String typeKey = type.getName() + StringPool.DOT;
-            Set<String> mapperSet = mappedStatements.keySet().stream().filter(ms -> ms.startsWith(typeKey)).collect(Collectors.toSet());
-            if (!mapperSet.isEmpty()) {
-                mapperSet.forEach(mappedStatements::remove);
-            }
+            String typeKey = type.getName() + StringPool.DOT;
+            String simpleName = type.getSimpleName();
+            mappedStatements.keySet().stream().filter(ms -> ms.startsWith(typeKey) || ms.equals(simpleName)).collect(Collectors.toSet()).forEach(mappedStatements::remove);
+            resultMaps.keySet().stream().filter(r -> r.startsWith(typeKey)).collect(Collectors.toSet()).forEach(resultMaps::remove);
+            parameterMaps.keySet().stream().filter(p -> p.startsWith(typeKey)).collect(Collectors.toSet()).forEach(parameterMaps::remove);
+            keyGenerators.keySet().stream().filter(k -> k.startsWith(typeKey)).collect(Collectors.toSet()).forEach(keyGenerators::remove);
+            sqlFragments.keySet().stream().filter(s -> s.startsWith(typeKey)).collect(Collectors.toSet()).forEach(sqlFragments::remove);
+            caches.keySet().stream().filter(p -> p.equals(type.getName()) || p.equals(simpleName)).collect(Collectors.toSet()).forEach(caches::remove);
         }
     }
 

+ 22 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/MybatisConfigurationTest.java

@@ -3,6 +3,8 @@ package com.baomidou.mybatisplus.test;
 import com.baomidou.mybatisplus.core.MybatisConfiguration;
 import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
 import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
+import com.baomidou.mybatisplus.test.mapper.AMapper;
+import com.baomidou.mybatisplus.test.mapper.BMapper;
 import org.apache.ibatis.builder.StaticSqlSource;
 import org.apache.ibatis.executor.keygen.NoKeyGenerator;
 import org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory;
@@ -115,4 +117,24 @@ class MybatisConfigurationTest {
             );
         }
     }
+
+    @Test
+    void testReload() {
+        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
+        mybatisConfiguration.addMapper(BMapper.class);
+        mybatisConfiguration.addMapper(AMapper.class);
+        Assertions.assertEquals(39, mybatisConfiguration.getMappedStatementNames().size());
+        Assertions.assertEquals(4, mybatisConfiguration.getSqlFragments().size());
+        Assertions.assertEquals(2, mybatisConfiguration.getResultMaps().size());
+        Assertions.assertEquals(2, mybatisConfiguration.getCaches().size());
+        Assertions.assertEquals(2, mybatisConfiguration.getMapperRegistry().getMappers().size());
+        mybatisConfiguration.addNewMapper(BMapper.class);
+        mybatisConfiguration.addNewMapper(AMapper.class);
+        Assertions.assertEquals(39, mybatisConfiguration.getMappedStatementNames().size());
+        Assertions.assertEquals(4, mybatisConfiguration.getSqlFragments().size());
+        Assertions.assertEquals(2, mybatisConfiguration.getResultMaps().size());
+        Assertions.assertEquals(2, mybatisConfiguration.getCaches().size());
+        Assertions.assertEquals(2, mybatisConfiguration.getMapperRegistry().getMappers().size());
+    }
+
 }

+ 12 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/entity/AEntity.java

@@ -0,0 +1,12 @@
+package com.baomidou.mybatisplus.test.entity;
+
+import lombok.Data;
+
+@Data
+public class AEntity {
+
+    private Long id;
+
+    private String name;
+
+}

+ 12 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/entity/BEntity.java

@@ -0,0 +1,12 @@
+package com.baomidou.mybatisplus.test.entity;
+
+import lombok.Data;
+
+@Data
+public class BEntity {
+
+    private Long id;
+
+    private String name;
+
+}

+ 11 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/mapper/AMapper.java

@@ -0,0 +1,11 @@
+package com.baomidou.mybatisplus.test.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.test.entity.AEntity;
+
+public interface AMapper extends BaseMapper<AEntity> {
+
+
+    AEntity test();
+
+}

+ 11 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/mapper/BMapper.java

@@ -0,0 +1,11 @@
+package com.baomidou.mybatisplus.test.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.test.entity.AEntity;
+import com.baomidou.mybatisplus.test.entity.BEntity;
+
+public interface BMapper extends BaseMapper<BEntity> {
+
+    BEntity test();
+
+}

+ 20 - 0
mybatis-plus-core/src/test/resources/com/baomidou/mybatisplus/test/mapper/AMapper.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="com.baomidou.mybatisplus.test.mapper.AMapper">
+
+    <cache-ref namespace="com.baomidou.mybatisplus.test.mapper.BMapper"/>
+
+    <resultMap id="xxx1" type="com.baomidou.mybatisplus.test.entity.AEntity">
+        <id column="id" property="id"/>
+        <result column="name" property="name"/>
+    </resultMap>
+
+    <sql id="testAsql">
+        id
+    </sql>
+
+    <select id="test" resultType="com.baomidou.mybatisplus.test.entity.AEntity">
+        select * from test_a where id = 1
+    </select>
+
+</mapper>

+ 15 - 0
mybatis-plus-core/src/test/resources/com/baomidou/mybatisplus/test/mapper/BMapper.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="com.baomidou.mybatisplus.test.mapper.BMapper">
+
+    <cache/>
+
+    <sql id="testBsql">
+        name
+    </sql>
+
+    <select id="test" resultType="com.baomidou.mybatisplus.test.entity.BEntity">
+        select * from test_b where id = 1
+    </select>
+
+</mapper>