2229499815 / async-excel

一个基于easyexcel大数据量数据导入导出异步处理组件
Apache License 2.0
93 stars 21 forks source link

mybatisplus的globalConfig冲突。(不清楚是不是bug,) #6

Open SSSDNSY opened 6 months ago

SSSDNSY commented 6 months ago

场景是这样的:我的项目配置了一个 mybatplus填充器。

  /**
     * 填充 创建人、更新人信息
     */
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new CreatorAndUpdatorInfoHandler();
    }

但是我发现没有生效,debug之后发现,是async的GlobalConfig 把项目中的GlobalConfig替换了,这样我的MetaObjectHandler 就失效了。

/**
 * @Description TODO
 * @Author 姚仲杰#80998699
 * @Date 2022/7/5 16:37
 */
@Configuration
@MapperScan(value = "com.asyncexcel.springboot.context.mapper",sqlSessionFactoryRef = "excelSqlSessionFactory")
@EnableConfigurationProperties({ExcelDataSourceProperties.class,MybatisPlusProperties.class})
public class ExcelMybatisPlusConfiguration {

    @Bean("excelDataSource")
    public DataSource dataSource(ExcelDataSourceProperties properties){
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setJdbcUrl(properties.getUrl());
        hikariDataSource.setUsername(properties.getUsername());
        hikariDataSource.setPassword(properties.getPassword());
        hikariDataSource.setDriverClassName(properties.getDriverClassName());
        return hikariDataSource;
    }

    @Bean("excelSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("excelDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setVfs(SpringBootVFS.class);
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
        GlobalConfig globalConfig = new GlobalConfig();
        DbConfig dbConfig = new DbConfig();
        dbConfig.setIdType(IdType.AUTO);
        globalConfig.setDbConfig(dbConfig);

        mybatisSqlSessionFactoryBean.setGlobalConfig(globalConfig);
        return mybatisSqlSessionFactoryBean.getObject();
    }

}

setGlobalConfig 导致我的失效了?没有仔细debug去看源码了,结果是前端提交一片报错 ,因为我的表的createDt等字段都是自动填充的。

2229499815 commented 5 months ago

组件使用了spring的父子容器,理论上这个配置不会影响到你的项目配置,需要你提供更多的报错和调试信息

SSSDNSY commented 5 months ago

image 根据我的调试信息,正如你这个注释所说,其实ExcelMybatisPlusConfiguration.java的配置已经 生成了 SqlSessionFactory类型的名为“excelSqlSessionFactory”的Bean,但是才看到后面的父子容器加载,而mybatisplus的MybatisPlusAutoConfiguration.java的

...
    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
}
...

mybatisplus的AutoConfiguration配置类有这个注解:@ConditionalOnMissingBean ,@ConditionalOnMissingBean 注解不会让spring再去生成SqlSessionFactory 的bean。 同时,MybatisPlusAutoConfiguration在这个方法里面有这么几句是去globalConfig.setMetaObjectHandler ,这个就是前面我所说的导致了MetaObjectHandler的原因。

  // TODO 注入填充器

        if (this.applicationContext.getBeanNamesForType(MetaObjectHandler.class,
            false, false).length > 0) {
            MetaObjectHandler metaObjectHandler = this.applicationContext.getBean(MetaObjectHandler.class);
            globalConfig.setMetaObjectHandler(metaObjectHandler);
        }

结合调试,综上,其实际使用和调试,延迟加载的子容器并没有很好的隔离开asyncExcel的mybatis.globalConfig, 覆盖了业务侧的配置。 退而求其次,其实可以出一个多数据源的就可以了,业务表和下载的task表都是在一个数据库的,甚至可否不隔离交由业务选择或者配置,而且task表的字段和信息业务可能需要拓展和自己命名字段名等。 可能我因业务开发之余,匆忙之下的调试推理,不够严谨细致,还请大佬指教。

组件使用了spring的父子容器,理论上这个配置不会影响到你的项目配置,需要你提供更多的报错和调试信息