本篇内容介绍了“mybatis-plus配置多数据源出现invalid bound statement not found怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
版本号
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency>
没有使用myabtis-plus
的多数据源依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.5.7</version> </dependency>
启动类配置
@MapperScans({ @MapperScan(basePackages = {"com.aaa.dao"}, sqlSessionFactoryRef = DatasourceConf.SESSION_FACTORY), @MapperScan(basePackages = {"com.bbb.dao"}, sqlSessionFactoryRef = OtherDatasourceConf.OTHER_SESSION_FACTORY) })
配置类
@Configuration public class DatasourceConf { public static final String SESSION_FACTORY = "SessionFactory"; @Primary @Bean(name = SESSION_FACTORY) public SqlSessionFactory dianOrderSessionFactory(MybatisProperties properties) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(secondDatasource()); //... } @Primary @Bean public DataSource secondDatasource() { return new HikariDataSource(secondHikariConfig()); } @Bean @ConfigurationProperties(prefix = "spring.datasource.aaa.hikari") public HikariConfig secondHikariConfig() { return new HikariConfig(); } }
另一个数据源配置
@Configuration public class OtherDatasourceConf { public static final String OTHER_SESSION_FACTORY = "otherSessionFactory"; @Bean(name = OTHER_SESSION_FACTORY) public SqlSessionFactory otherSessionFactory(MybatisProperties properties) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(otherDataSource()); //.... } @Bean public DataSource otherDataSource() { return new HikariDataSource(otherHikariConfig()); } @Bean @ConfigurationProperties(prefix = "spring.datasource.hikari") public HikariConfig otherHikariConfig() { return new HikariConfig(); } }
在mapper.xml
中配置了sql
的都没问题,但是使用mybatis-plus
的内置函数就会报invalid bound statement (not found)
异常。
查看MybatisPlusAutoConfiguration
类
@Configuration @ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) @ConditionalOnSingleCandidate(DataSource.class) @EnableConfigurationProperties(MybatisPlusProperties.class) @AutoConfigureAfter(DataSourceAutoConfiguration.class) public class MybatisPlusAutoConfiguration implements InitializingBean { @Bean @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { // TODO 使用 MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBean MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean(); factory.setDataSource(dataSource); ... return factory.getObject(); } }
将SqlSessionFactoryBean
修改成myabtis-plus
自定义的MybatisSqlSessionFactoryBean
解决
注意mybatis-plus
的配置需要使用MybatisPlusProperties
而不是MybatisProperties
当配置类多个DataSource
,MybatisPlusAutoConfiguration
类就不再生效,需要手动配置
“mybatis-plus配置多数据源出现invalid bound statement not found怎么解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。