Skip to content

Commit

Permalink
Fix the issue of loading mybatis plugin in MP
Browse files Browse the repository at this point in the history
  • Loading branch information
jujunchen committed Sep 8, 2024
1 parent a90f960 commit e02291e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
3 changes: 2 additions & 1 deletion spring-hot-plugin-mybatis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
<parent>
<groupId>vip.aliali.spring</groupId>
<artifactId>spring-hot-plugin-parent</artifactId>
<version>1.1</version>
<version>1.1.1</version>
</parent>

<artifactId>spring-hot-plugin-mybatis</artifactId>
<description>插件Mybatis、MybatisPlus组件</description>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@Configuration
public class MybatisPluginConfiguration {

@Bean
@Bean("registerMybatis")
@ConditionalOnClass(name = "org.apache.ibatis.session.SqlSession")
public RegisterMybatis createRegisterMybatis(ApplicationContext main) {
return new RegisterMybatis(main);
Expand All @@ -18,9 +18,9 @@ public RegisterMybatis createRegisterMybatis(ApplicationContext main) {
* 用来处理MybatisPlus
* @return MybatisPlus的处理工具
*/
@Bean
@ConditionalOnClass(name = "com.baomidou.mybatisplus.core.MybatisConfiguration")
public MybatisPlusUtil createMybatisPluginAutoConfiguration() {
@Bean("mybatisPlusUtil")
@ConditionalOnClass(name = "com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration")
public MybatisPlusUtil createMybatisPluginUtil() {
return new MybatisPlusUtil();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisMapperRegistry;
import com.baomidou.mybatisplus.core.mapper.Mapper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.context.ApplicationContext;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* 用来处理MybatisPlus的工具类
Expand All @@ -33,23 +41,57 @@ public void clearMPQuote(Configuration configuration, ApplicationContext plugin)
MybatisConfiguration mpConfiguration = (MybatisConfiguration) configuration;
//当前插件加载的mapper
Map<String, MapperFactoryBean> mapperFactoryBeanMap = plugin.getBeansOfType(MapperFactoryBean.class);
Set<String> mapperRegistryCache = GlobalConfigUtils.getGlobalConfig(mpConfiguration).getMapperRegistryCache();
for (MapperFactoryBean mapperFactoryBean : mapperFactoryBeanMap.values()) {
Class<?> mapper = mapperFactoryBean.getMapperInterface();

//清除MP没有清除掉的引用缓存
Class<?> modelClass = ReflectionKit.getSuperClassGenericType(mapper, com.baomidou.mybatisplus.core.mapper.Mapper.class, 0);
Class<?> modelClass = ReflectionKit.getSuperClassGenericType(mapper, Mapper.class, 0);
TableInfo tableInfo = TableInfoHelper.getTableInfo(modelClass);
//null 表示已经清理过
if (tableInfo != null) {
//清除Model 的Class引用缓存
((Map) ReflectUtil.getFieldValue(mpConfiguration.getReflectorFactory(), "reflectorMap")).remove(modelClass);
((Map)ReflectUtil.getFieldValue(ReflectionKit.class, "CLASS_FIELD_CACHE")).remove(modelClass);
((Map)ReflectUtil.getFieldValue(TableInfoHelper.class, "TABLE_NAME_INFO_CACHE")).remove(tableInfo.getTableName());
//删除类型转换处理器中的引用
((Map)ReflectUtil.getFieldValue(configuration.getTypeHandlerRegistry(), "typeHandlerMap")).remove(modelClass);
// 清空实体表信息映射信息
TableInfoHelper.remove(modelClass);
}

// 清空 Mapper 缓存信息
final String mapperType = mapper.toString();
MybatisMapperRegistry mapperRegistry = ((MybatisMapperRegistry) ReflectUtil.getFieldValue(mpConfiguration, "mybatisMapperRegistry"));
ReflectUtil.invoke(mapperRegistry, "removeMapper", mapper);
//删除MappedStatement缓存
Map<String, MappedStatement> mappedStatements = ((Map) ReflectUtil.getFieldValue(mpConfiguration, "mappedStatements"));
final String typeKey = mapper.getName() + StringPool.DOT;
Set<String> mapperSet = mappedStatements.keySet().stream().filter(ms -> ms.startsWith(typeKey)).collect(Collectors.toSet());
if (!mapperSet.isEmpty()) {
mapperSet.forEach(mappedStatements::remove);
}
//删除ResultMap缓存
Map<String, ResultMap> resultMaps = ((Map)ReflectUtil.getFieldValue(mpConfiguration, "resultMaps"));
Set<String> resultMapSet = resultMaps.keySet().stream().filter(ms -> ms.startsWith(typeKey)).collect(Collectors.toSet());
if (!resultMapSet.isEmpty()) {
resultMapSet.forEach(resultMaps::remove);
}
//删除类型转换处理器中的引用
((Map)ReflectUtil.getFieldValue(configuration.getTypeHandlerRegistry(), "typeHandlerMap")).remove(modelClass);
mpConfiguration.removeMapper(mapper);
//删除MP的CRUD的Mapper缓存
mapperRegistryCache.remove(mapperType);

//重置资源加载标识
Set<String> loadedResources = ((Set)ReflectUtil.getFieldValue(configuration, "loadedResources"));
String xmlResource = getXmlResource(mapper.getName());
loadedResources.remove(xmlResource);
loadedResources.remove(mapper.toString());
}

SqlHelper.FACTORY = null;
}
}

private String getXmlResource(String name) {
return name.replace('.', '/') + ".xml";
}
}

0 comments on commit e02291e

Please sign in to comment.