概述
使用springboot,结合mybatisplus配置多个数据源
附完整示例项目地址:
多数据源配置mybatisplus示例
第一步:配置多数据源连接信息
在application.yml配置文件中添加多个数据源的连接信息,如下
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启动类需要去除数据源自动配置类,如下
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
MybatisPlusAutoConfiguration.class})
第三步:编写不同数据源的配置类
- 配置类实现接口Initializingbean
public class BarMybatisConfig implements InitializingBean
- 设置数据源Bean、连接工厂Bean等
@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);
}
- 复制MybatisPlusAutoConfiguration中的属性
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;
}
- 实现Initializingbean接口的afterPropertiesSet方法
@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();
}
- 添加@MapperScan等注解设置
@Configuration
@MapperScan(value = "com.flamingo.multidatasourcewithmybatis.dao.bar", sqlSessionFactoryRef = "barSqlSessionFactory",
sqlSessionTemplateRef = "barSqlSessionTemplate")
另外的数据源配置类也同上进行设置
附完整示例项目地址:
多数据源配置mybatisplus示例
最后
以上就是糟糕豌豆为你收集整理的多数据源配置mybatisplus示例的全部内容,希望文章能够帮你解决多数据源配置mybatisplus示例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复