Mybatis配置多数据源后saveBatch()出现Mapped Statements collection does not contain value

        年前发布的最后一个版本,上线前回归其他接口,发现出现大量接口保存失败TvT由于出现的异常都是Mybatis相关的,很容易联想到这个版本是否对applicationContext.xml文件中mybatis配置部分进行了修改。下面为applicationContext.xml中部分配置:


    <!-- MyBatis会话工厂 -->
    <bean id="first_mysql_sdkMyBatisSqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean" primary="true">
        <property name="dataSource" ref="first_mysql_sdk" />
        <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml" />
        <property name="globalConfig" ref="globalConfig"/>
    </bean>

    <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        <property name="metaObjectHandler" ref="myMetaObjectHandler"/>
    </bean>

    <!-- Mapper自动扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.test.webapp.mapper" />
        <property name="sqlSessionFactoryBeanName" value="first_mysql_sdkMyBatisSqlSessionFactory" />
    </bean>

    <import resource="classpath:applicationContext-jdbc.xml" />


    <!-- 以下是多数据源 -->

    <!-- MyBatis会话工厂 -->
    <bean id="second_mysql_sdkMyBatisSqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="second_mysql_sdk" />
        <property name="mapperLocations" value="classpath*:multiMapper/biMapper/*Mapper.xml" />
        <property name="globalConfig" ref="globalConfig"/>
    </bean>

    <!-- Mapper自动扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.test.webapp.multiMapper.biMapper" />
        <property name="sqlSessionFactoryBeanName" value="second_mysql_sdkMyBatisSqlSessionFactory" />
    </bean>

        既然是新增了数据源,那么问题大概率出现在新增的数据源的配置与原本数据源的配置产生了冲突。我通过分别注释新增数据源 property 的方式,发现当注释了新增数据源的 <property name="globalConfig" ref="globalConfig"/> 后,原本数据源和新增数据源的crud均正常。那么问题应该就是出现在这个地方了。然后通过关键字去谷歌搜索,发现了在 mybatis-plus 的github的issue已经有相关问题存在(insertBatch()在某些情况下执行不生效 · Issue #87 · baomidou/mybatis-plus · GitHub),问题与我遇到的问题一致。

        因此把新增数据源中的 <property name="globalConfig" ref="globalConfig"/> 配置删除即可。当然,由于我们项目中新增的那个数据源只用于查询,globalConfig中的 myMetaObjectHandler 用于 insert/update 时自动填充创建人/修改人,所以直接删掉不影响业务。如果多个数据源要使用该 handler,那可以增加多一个不同id 的 GlobalConfig bean,各个数据源各自引用,如下:


    <!-- MyBatis会话工厂 -->
    <bean id="first_mysql_sdkMyBatisSqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean" primary="true">
        <property name="dataSource" ref="first_mysql_sdk" />
        <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml" />
        <property name="globalConfig" ref="globalConfig"/>
    </bean>

    <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        <property name="metaObjectHandler" ref="myMetaObjectHandler"/>
    </bean>

    <!-- Mapper自动扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.test.webapp.mapper" />
        <property name="sqlSessionFactoryBeanName" value="first_mysql_sdkMyBatisSqlSessionFactory" />
    </bean>

    <import resource="classpath:applicationContext-jdbc.xml" />


    <!-- 以下是多数据源 -->

    <!-- MyBatis会话工厂 -->
    <bean id="second_mysql_sdkMyBatisSqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="second_mysql_sdk" />
        <property name="mapperLocations" value="classpath*:multiMapper/biMapper/*Mapper.xml" />
        <property name="globalConfig" ref="secondGlobalConfig"/>
    </bean>

   <bean id="secondGlobalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        <property name="metaObjectHandler" ref="myMetaObjectHandler"/>
    </bean>

    <!-- Mapper自动扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.test.webapp.multiMapper.biMapper" />
        <property name="sqlSessionFactoryBeanName" value="second_mysql_sdkMyBatisSqlSessionFactory" />
    </bean>

参考链接:

       ​​​​​​insertBatch()在某些情况下执行不生效 · Issue #87 · baomidou/mybatis-plus · GitHub

         

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值