mybatis-plus集成多数据源 Invalid bound statement (not found):
时间: 2023-10-17 09:38:09 浏览: 369
这个错误通常是由于 MyBatis-Plus 无法在 SQL 映射文件中找到指定的绑定语句所致。可能有以下几个原因导致该错误:
1. SQL 映射文件位置不正确:请确保 SQL 映射文件在正确的位置,并且能够被 MyBatis-Plus 扫描到。通常情况下,SQL 映射文件应该位于 `resources` 目录下的 `mapper` 目录中。
2. SQL 映射文件命名规范不正确:请确保 SQL 映射文件的命名规范符合 MyBatis-Plus 的要求。一般来说,SQL 映射文件的名字应该与对应的 Mapper 接口的名字相同,并且后缀为 `.xml`。
3. SQL 映射文件中没有定义对应的绑定语句:请检查 SQL 映射文件,确保其中定义了被调用的绑定语句。绑定语句应该使用 `<select>`, `<insert>`, `<update>`, `<delete>` 等标签进行定义。
如果以上方法都无效,你可以提供更多的详细信息,以便我能够更好地帮助你解决这个问题。
相关问题
mybatis 多数据源 Invalid bound statement (not found):
### 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;
}
}
```
---
###
多数据源Invalid bound statement not found
多数据源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> {
}
```
阅读全文
相关推荐
















