您好,登錄后才能下訂單哦!
本篇內容介紹了“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。