mybatis(xml方式)多数据源异常: Invalid bound statement (not found)

本文解决MyBatis配置多数据源时遇到的BindingException异常问题,详细介绍了如何通过在mybatis配置类中设置mapper路径来正确读取mapper的XML文件。

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

mybatis配置多数据源请求后台时抛出异常:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

很显然异常是在说没找到对应的方法,或者说没有对应方法的sql。

之前配置xml方式的mybatis都是在application.properties中添加一行

mybatis.mapper-locations=classpath:mappers/**/*.xml

就行了。

因为多数据源是通过@contigration注解的Java类编程配置的,所以这种方式并不能读取到mapper的xml文件在哪。

解决方法是分别在mybatis配置类中生成SqlSessionFactory的方法中配置mapper路径:

sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mappers/**/*.xml"));

### MyBatis 多数据源下 'Invalid bound statement (not found)' 错误解决方案 在多数据源场景中,MyBatis 报错 `Invalid bound statement (not found)` 通常是由于配置问题或 Mapper 文件未正确加载引起的。以下是可能导致该问题的原因及解决方法: #### 1. **Mapper 接口与 XML 文件未正确关联** - 确保 Mapper 接口的全限定名与对应的 XML 文件名称一致。例如,如果 Mapper 接口为 `com.example.mapper.UserMapper`,那么对应的 XML 文件应命名为 `UserMapper.xml` 并位于正确的路径下[^1]。 - 检查 XML 文件是否被正确加载到 Spring 容器中。可以通过设置 `mybatis.mapper-locations` 属性来指定 Mapper 文件的位置。例如: ```properties mybatis.mapper-locations=classpath*:mapper/**/*Mapper.xml ``` #### 2. **多数据源配置冲突** - 在多数据源场景下,每个数据源需要独立配置其对应的 MyBatis 工厂和 SqlSessionFactory。确保每个数据源的 Mapper 文件被正确加载到对应的 SqlSessionFactory 中[^3]。 - 使用动态数据源切换时,需确保切换逻辑正确。例如,在使用 `@DS` 注解时,必须保证注解值与配置的数据源名称一致[^4]。 #### 3. **Mapper 扫描路径不正确** - 如果使用了 `@MapperScan` 注解,需确保扫描路径包含所有 Mapper 接口所在的包。例如: ```java @MapperScan(basePackages = {"com.example.mapper.ds1", "com.example.mapper.ds2"}) public class MyBatisConfig { // 配置代码 } ``` - 对于多数据源场景,建议为每个数据源单独配置一个 `@MapperScan`,并指定不同的 SqlSessionTemplate[^2]。 #### 4. **XML 文件未被正确加载** - 如果使用的是 MyBatis-Plus,需检查是否正确设置了 `mapperLocations` 属性。例如: ```java @Bean @ConfigurationProperties(prefix = "spring.datasource.dynamic.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath*:mapper/primary/*.xml")); return bean.getObject(); } ``` #### 5. **动态 SQL 或自定义方法问题** - 如果 Mapper 方法未在 XML 文件中定义,或者方法签名与 XML 中的 `<select>`、`<insert>` 等标签不匹配,也会导致此错误。确保方法签名与 XML 文件中的 SQL 语句完全一致。 #### 6. **Spring Boot 版本兼容性** - 不同版本的 Spring Boot 和 MyBatis-Plus 可能存在兼容性问题。例如,在 Spring Boot 3.x 和 MyBatis-Plus 3.5.9 的组合中,需确保所有依赖版本匹配。可以通过以下方式检查版本兼容性: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.9</version> </dependency> ``` ```java // 示例:动态数据源配置 @Configuration public class DynamicDataSourceConfig { @Bean public DataSource dynamicDataSource(DataSource primaryDataSource, DataSource secondaryDataSource) { DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(); Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put("primary", primaryDataSource); targetDataSources.put("secondary", secondaryDataSource); dataSource.setTargetDataSources(targetDataSources); dataSource.setDefaultTargetDataSource(primaryDataSource); return dataSource; } } ``` --- ###
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值