Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of invalid bound statement not found when multiple data sources appear in mybatis-plus configuration

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to solve invalid bound statement not found in mybatis-plus configuration multiple data sources". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Environment

Version number

Com.baomidou mybatis-plus-boot-starter 3.2.0

Multiple data source dependencies that do not use myabtis-plus

Com.baomidou dynamic-datasource-spring-boot-starter 2.5.7

II. Configuration of multiple data sources

Startup class configuration

@ MapperScans ({@ MapperScan (basePackages = {"com.aaa.dao"}, sqlSessionFactoryRef = DatasourceConf.SESSION_FACTORY), @ MapperScan (basePackages = {"com.bbb.dao"}, sqlSessionFactoryRef = OtherDatasourceConf.OTHER_SESSION_FACTORY)})

Configuration class

@ Configurationpublic 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 ();}}

Another data source configuration

@ Configurationpublic 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 ();}}

III. Anomalies

There is no problem configuring sql in mapper.xml, but using the built-in function of mybatis-plus will report an invalid bound statement (not found) exception.

IV. Solution

View the MybatisPlusAutoConfiguration class

@ 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 uses MybatisSqlSessionFactoryBean instead of SqlSessionFactoryBean MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean (); factory.setDataSource (dataSource);. Return factory.getObject ();}}

Modify SqlSessionFactoryBean to MybatisSqlSessionFactoryBean solution customized by myabtis-plus

Note that the configuration of mybatis-plus requires MybatisPlusProperties instead of MybatisProperties

When multiple DataSource,MybatisPlusAutoConfiguration classes are configured, they are no longer valid. You need to configure them manually.

This is the end of the content of "mybatis-plus configuration multiple data sources how to solve invalid bound statement not found". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report