大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容介绍了“mybatis-plus配置多数据源出现invalid bound statement not found怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联建站主要从事网站设计、做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务岚县,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
版本号
com.baomidou mybatis-plus-boot-starter 3.2.0
没有使用myabtis-plus
的多数据源依赖
com.baomidou dynamic-datasource-spring-boot-starter 2.5.7
启动类配置
@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怎么解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!