我是靠谱客的博主 糟糕豌豆,这篇文章主要介绍多数据源配置mybatisplus示例,现在分享给大家,希望可以做个参考。

使用springboot,结合mybatisplus配置多个数据源

附完整示例项目地址:

多数据源配置mybatisplus示例

第一步:配置多数据源连接信息

在application.yml配置文件中添加多个数据源的连接信息,如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
spring: datasource: foo: jdbc-url: jdbc:mysql://localhost:3306/foo username: root password: 1234 bar: jdbc-url: jdbc:mysql://localhost:3306/bar username: root password: 1234

第二步:SpringBoot启动类去除数据源自动配置

SpringBoot启动类需要去除数据源自动配置类,如下

复制代码
1
2
3
4
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisPlusAutoConfiguration.class})

第三步:编写不同数据源的配置类

  1. 配置类实现接口Initializingbean
复制代码
1
2
public class BarMybatisConfig implements InitializingBean
  1. 设置数据源Bean、连接工厂Bean等
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
@Bean @ConfigurationProperties("spring.datasource.bar") public DataSource barDataSource() {return DataSourceBuilder.create().build(); } @Bean public SqlSessionFactory barSqlSessionFactory(@Qualifier("barDataSource") DataSource barDataSource) throws Exception{ return this.delegate.sqlSessionFactory(barDataSource); } @Bean public SqlSessionTemplate barSqlSessionTemplate(@Qualifier("barSqlSessionFactory") SqlSessionFactory barSqlSessionFactory) { return this.delegate.sqlSessionTemplate(barSqlSessionFactory); }
  1. 复制MybatisPlusAutoConfiguration中的属性
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private ObjectProvider<Interceptor[]> interceptorsProvider; private ObjectProvider<TypeHandler[]> typeHandlersProvider; private ObjectProvider<LanguageDriver[]> languageDriversProvider; private ResourceLoader resourceLoader; private ObjectProvider<DatabaseIdProvider> databaseIdProvider; private ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider; private ObjectProvider<List<MybatisPlusPropertiesCustomizer>> mybatisPlusPropertiesCustomizerProvider; private ApplicationContext applicationContext; private MybatisPlusAutoConfiguration delegate; public BarMybatisConfig(ObjectProvider<Interceptor[]> interceptorsProvider, ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider, ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider, ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider, ObjectProvider<List<MybatisPlusPropertiesCustomizer>> mybatisPlusPropertiesCustomizerProvider, ApplicationContext applicationContext) { this.interceptorsProvider = interceptorsProvider; this.typeHandlersProvider = typeHandlersProvider; this.languageDriversProvider = languageDriversProvider; this.resourceLoader = resourceLoader; this.databaseIdProvider = databaseIdProvider; this.configurationCustomizersProvider = configurationCustomizersProvider; this.mybatisPlusPropertiesCustomizerProvider = mybatisPlusPropertiesCustomizerProvider; this.applicationContext = applicationContext; }
  1. 实现Initializingbean接口的afterPropertiesSet方法
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override public void afterPropertiesSet() throws Exception { final MybatisPlusProperties properties = new MybatisPlusProperties(); properties.setMapperLocations(new String[] {"classpath:mapper/bar/**/*.xml"}); final MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setMapUnderscoreToCamelCase(true); properties.setConfiguration(configuration); this.delegate = new MybatisPlusAutoConfiguration(properties, this.interceptorsProvider, this.typeHandlersProvider, this.languageDriversProvider, this.resourceLoader, this.databaseIdProvider, this.configurationCustomizersProvider, this.mybatisPlusPropertiesCustomizerProvider, this.applicationContext); this.delegate.afterPropertiesSet(); }
  1. 添加@MapperScan等注解设置
复制代码
1
2
3
4
@Configuration @MapperScan(value = "com.flamingo.multidatasourcewithmybatis.dao.bar", sqlSessionFactoryRef = "barSqlSessionFactory", sqlSessionTemplateRef = "barSqlSessionTemplate")

另外的数据源配置类也同上进行设置

附完整示例项目地址:

多数据源配置mybatisplus示例

最后

以上就是糟糕豌豆最近收集整理的关于多数据源配置mybatisplus示例的全部内容,更多相关多数据源配置mybatisplus示例内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部