introduction
According to project requirements, another Mysql database needs to be introduced, but the project has introduced a Mysql, and there are several solutions at this time.
- Through the Dynamic-DataSource framework, seamless integration, but dynamically switches data sources, which does not conform to project requirements, so the second type is adopted
- Multiple data sources are seamlessly introduced through custom data source configuration classes and are isolated based on file directory
step
Introduce dependencies
The project needs to introduce MyBatis Plus, no additional dependencies are required
<dependency> <groupId></groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.12</version> </dependency>
Modify the configuration file
spring: datasource: d1: password: xxxx username: xxxx jdbc-url: jdbc:mysql://xxxx:3306/ecshop_wp?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true driver-class-name: d2: password: root username: root jdbc-url: jdbc:mysql://127.0.0.1:3306/xg_bi_lcc?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true driver-class-name:
Add configuration class
@Configuration @MapperScan(basePackages = ".mapper1", sqlSessionFactoryRef = "d1SqlSessionFactory") public class EcshopWpDataSourceConfig { @Bean(name = "d1DataSource") @ConfigurationProperties(prefix = ".d1") public DataSource ecshopWpDataSource() { return ().build(); } @Bean(name = "d1SqlSessionFactory") public SqlSessionFactory ecshopWpSqlSessionFactory(@Qualifier("d1DataSource") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); //configuration configuration bean //MybatisConfiguration configuration = new MybatisConfiguration(); //(true); //(false); // Configure printing sql statements //(); // Add custom SQL injection //(configuration); //Plugin object MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); //Dynamic table name //DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor(); //You can pass multiple table name parameters to specify which tables use MonthTableNameHandler to process table names //(new MonthTableNameHandler("t_table_name")); //Processing table names in the form of interceptor // Multiple interceptors can be passed, that is, multiple table name processors can be passed TableNameHandler //(dynamicTableNameInnerInterceptor); //Pagination plugin (new PaginationInnerInterceptor()); (dataSource); // Set the location of mybatis' xml (new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/d1/*.xml")); (mybatisPlusInterceptor); return (); } @Bean(name = "d1TransactionManager") public DataSourceTransactionManager ecshopWpTransactionManager(@Qualifier("d1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } }
@Configuration @MapperScan(basePackages = ".Mapper2", sqlSessionFactoryRef = "d2SqlSessionFactory") public class XgBiLccDataSourceConfig { @Bean(name = "d2DataSource") @ConfigurationProperties(prefix = ".d2") public DataSource ecshopWpDataSource() { return ().build(); } @Bean(name = "d2SqlSessionFactory") public SqlSessionFactory ecshopWpSqlSessionFactory(@Qualifier("d2DataSource") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); //configuration configuration bean //MybatisConfiguration configuration = new MybatisConfiguration(); //(true); //(false); // Configure printing sql statements //(); // Add custom SQL injection //(configuration); //Plugin object MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); //Dynamic table name //DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor(); //You can pass multiple table name parameters to specify which tables use MonthTableNameHandler to process table names //(new MonthTableNameHandler("t_table_name")); //Processing table names in the form of interceptor // Multiple interceptors can be passed, that is, multiple table name processors can be passed TableNameHandler //(dynamicTableNameInnerInterceptor); //Pagination plugin (new PaginationInnerInterceptor()); (dataSource); // Set the location of mybatis' xml (new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/d2/*.xml")); (mybatisPlusInterceptor); return (); } @Bean(name = "d2TransactionManager") public DataSourceTransactionManager ecshopWpTransactionManager(@Qualifier("d2DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } }
Use Cases
@Transactional
@Transactional(value = "d2TransactionManager", rollbackFor = ) This allows global business management
This is the end of this article about SpringBoot + MybatisPlus integrating multi-data source. For more related SpringBoot MybatisPlus multi-data source content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!