baomidou-mybatis自己写数据源切换执行updateBatchById无法切换的原因分析

探讨使用baomidou-mybatis时遇到的问题:在service层通过DynamicDataSource切换数据源,但在updateBatchById方法中失效。原因是该方法底层调用了未覆盖数据源切换的updateById方法。解决方案为直接覆盖updateById并手动切换数据源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

baomidou-mybatis将大量方法封装,在自己的service类中的方法中利用DynamicDataSource切换数据源却失败了,原因是updateBatchById方法调用了updateById

代码位置

org.apache.ibatis.executor.BatchExecutor

doUpdate方法中利用

Connection connection = getConnection(ms.getStatementLog());

进行数据源获取

调试发现

ms.getStatementLog()

调用DAO下的updateById,而我们覆盖了updateBatchById切换数据源是无效的,需要重新覆盖updateById进行数据源切换。

而Spring里interface写注解是没有用的,最后发现自己写一个方法,调用super.updateBatchById即可

org.mybatis.spring.MyBatisSystemException: null at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:97) ~[mybatis-spring-3.0.3.jar:3.0.3] at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:439) ~[mybatis-spring-3.0.3.jar:3.0.3] at jdk.proxy2/jdk.proxy2.$Proxy82.selectList(Unknown Source) ~[na:na] at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224) ~[mybatis-spring-3.0.3.jar:3.0.3] at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForIPage(MybatisMapperMethod.java:119) ~[mybatis-plus-core-3.5.7.jar:3.5.7] at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:84) ~[mybatis-plus-core-3.5.7.jar:3.5.7] at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:152) ~[mybatis-plus-core-3.5.7.jar:3.5.7] at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89) ~[mybatis-plus-core-3.5.7.jar:3.5.7] at jdk.proxy2/jdk.proxy2.$Proxy130.findZeroValueRecords(Unknown Source) ~[na:na] at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-6.0.13.jar:6.0.13] at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) ~[spring-aop-6.0.13.jar:6.0.13] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-6.0.13.jar:6.0.13] at com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationInterceptor.invoke(DynamicDataSourceAnnotationInterceptor.java:57) ~[dynamic-datasource-spring-4.3.0.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.13.jar:6.0 int offset = 0; int size = 1000; List<ElectricityDataHis> list = new ArrayList<>(); IPage<ElectricityDataHis> records; do { Page<ElectricityDataHis> pages = new Page<>(offset, size); // 第1页,每页10条 records = hisService.findZeroValueRecords(pages); records.getRecords().stream().forEach(record->{ // 获取前一个有效值 ElectricityDataHis lastValid = hisService.findLastValidBeforeTime( record.getTagId(), record.getRecordTime() ); if (lastValid != null) { // 生成修正后的新记录 record.setRecordValue(lastValid.getRecordValue()); record.setStatus(2);// 标记为已修正 // 入修正记录(保留原始记录) list.add(record); } }); hisService.fixAllData(list); list.clear(); offset++; } while (! records.getRecords().isEmpty()); }
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值