多数据源Invalid bound statement not found
时间: 2023-11-16 16:02:16 浏览: 170
MybatisPlus BaseMapper 中的方法全部 Invalid bound statement (not found Error处理)
5星 · 资源好评率100%
多数据源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> {
}
```
阅读全文