解决spring boot中多数据源invalid bound statement (not found)报错问题

1.错误重现

在这里插入图片描述
2.解决思路:
01.平常我们系统中报这个错一般都是xml路径没有扫描到,在yml或者properties里面没有配置xml的扫描路径 ,我们配置上即可在这里插入图片描述
02:或者我们dao层接口,没有加mapper注解,致使无法扫描到resource下面的xm文件
我这里加了注解的
在这里插入图片描述
03:检查dao里面的接口中的方法名和 mapper.xml 中的statement 的 id 是否保持一致。不一致的话,也会报同样的错

如果检查了以上三项确认无误的话,在使用单数据源应该是没问题的了。但是 此时,坑来了,在我们使用多数据的时候,因为关闭了springboot的默认数据源配置,使用我们自己定义的数据源,此时我们在配置文件中配置的路径是不会生效的,需要我们在定义数据源代码的时候,手动指向一下mapper.xml的位置。如下图所示
在这里插入图片描述
切记:我们使用多数据源的时候,spring boot的一些自动配置会失效,我们需自己手动指定配置项(包括事物也是)。都需要指定。

  • 15
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
多数据源Invalid bound statement not found是由于在集成mybatis-plus时,Mapper生成的代理类存在,但是调用BaseMapper如selectById()方法报错所导致的。这个错误通常是由于mybatis-plus的MapperScannerConfigurer扫描到了多个Mapper接口,导致生成的代理类出现了冲突,从而无法正确绑定Mapper接口的SQL语句。 解决这个问题的方法是在配置MapperScannerConfigurer时,指定不同的basePackage,以避免扫描到重复的Mapper接口。同时,还需要在配置文件指定每个数据源对应的Mapper接口所在的位置,以确保mybatis-plus能够正确地生成代理类并绑定SQL语句。 以下是解决这个问题的步骤: 1.在配置文件指定每个数据源对应的Mapper接口所在的位置,例如: ``` mybatis-plus: mapper-locations: - classpath*:mapper/*.xml configuration: map-underscore-to-camel-case: true global-config: db-config: id-type: auto type-aliases-package: com.example.demo.entity datasource: master: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver slave: url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath*:mapper/*.xml ``` 2.在配置MapperScannerConfigurer时,指定不同的basePackage,例如: ``` @Configuration @MapperScan(basePackages = "com.example.demo.mapper.master", sqlSessionTemplateRef = "masterSqlSessionTemplate") public class MasterDataSourceConfig { @Bean(name = "masterDataSource") @ConfigurationProperties(prefix = "datasource.master") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "masterSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/master/*.xml")); return bean.getObject(); } @Bean(name = "masterTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "masterSqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } @Configuration @MapperScan(basePackages = "com.example.demo.mapper.slave", sqlSessionTemplateRef = "slaveSqlSessionTemplate") public class SlaveDataSourceConfig { @Bean(name = "slaveDataSource") @ConfigurationProperties(prefix = "datasource.slave") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "slaveSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/slave/*.xml")); return bean.getObject(); } @Bean(name = "slaveTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("slaveDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "slaveSqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 3.在Mapper接口使用@Mapper注解,例如: ``` @Mapper public interface UserMapper extends BaseMapper<User> { } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值