概述
进行动态切换,需要在类里面配置,顺便解决mybatis-plus自带代码无法使用问题,直接上代码:
一、yml配置
数据源可以都是oracle的也可以一个是oracle一个是mysql的。
spring: datasource: druid: db-type: com.alibaba.druid.pool.DruidDataSource #多数据源1: zjdb: driverClassName: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@XX.XX.XX.XX:1521/ORCL username: XXXXXXX password: XXX # u521Du59CBu5316u8FDEu63A5u5927u5C0F initial-size: 5 # u6700u5C0Fu7A7Au95F2u8FDEu63A5u6570 min-idle: 5 max-active: 20 max-wait: 30000 # u53EFu5173u95EDu7684u7A7Au95F2u8FDEu63A5u95F4u9694u65F6u95F4 time-between-eviction-runs-millis: 60000 # u914Du7F6Eu8FDEu63A5u5728u6C60u4E2Du7684u6700u5C0Fu751Fu5B58u65F6u95F4 min-evictable-idle-time-millis: 300000 validation-query: select '1' from dual test-while-idle: true test-on-borrow: false test-on-return: false # u6253u5F00PSCacheuFF0Cu5E76u4E14u6307u5B9Au6BCFu4E2Au8FDEu63A5u4E0APSCacheu7684u5927u5C0F pool-prepared-statements: true max-open-prepared-statements: 20 max-pool-prepared-statement-per-connection-size: 20 # u914Du7F6Eu76D1u63A7u7EDFu8BA1u62E6u622Au7684filters filters: stat #多数据源2: zjfz: driverClassName: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@XX.XX.XX.XX:51521/ORCL username: XXXXX password: XXXX # driverClassName: com.mysql.jdbc.Driver # url: jdbc:mysql://127.0.0.1:3306/ketech-dev?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true # username: root # password: 123456 # u521Du59CBu5316u8FDEu63A5u5927u5C0F initial-size: 5 # u6700u5C0Fu7A7Au95F2u8FDEu63A5u6570 min-idle: 5 max-active: 20 max-wait: 30000 # u53EFu5173u95EDu7684u7A7Au95F2u8FDEu63A5u95F4u9694u65F6u95F4 time-between-eviction-runs-millis: 60000 # u914Du7F6Eu8FDEu63A5u5728u6C60u4E2Du7684u6700u5C0Fu751Fu5B58u65F6u95F4 min-evictable-idle-time-millis: 300000 validation-query: select '1' from dual test-while-idle: true test-on-borrow: false test-on-return: false # u6253u5F00PSCacheuFF0Cu5E76u4E14u6307u5B9Au6BCFu4E2Au8FDEu63A5u4E0APSCacheu7684u5927u5C0F pool-prepared-statements: true max-open-prepared-statements: 20 max-pool-prepared-statement-per-connection-size: 20 # u914Du7F6Eu76D1u63A7u7EDFu8BA1u62E6u622Au7684filters filters: stat stat-view-servlet: url-pattern: /druid/* reset-enable: false login-username: admin login-password: 123456 web-stat-filter: url-pattern: /* exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
二、动态切换数据源配置文件
1.数据源db1
package yin.ketech.app.config; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import javax.sql.DataSource; /** * @FileName: ZjDbConfig * @Author Yep * @create 2020-06-17 * @Description: 多数据源-1 */ @Configuration @MapperScan(basePackages = "yin.ketech.app.mapper", sqlSessionFactoryRef = "zjdbSqlSessionFactory") public class ZjDbConfig { @Primary @Bean(name = "zjdbDataSource") @Qualifier("zjdbDataSource") @ConfigurationProperties(prefix = "spring.datasource.zjdb") public DataSource zjdbDataSource() { return DruidDataSourceBuilder.create().build(); } @Primary @Bean(name = "zjdbSqlSessionFactory") @Qualifier("zjdbSqlSessionFactory") public SqlSessionFactory zjdbSqlSessionFactory(@Qualifier("zjdbDataSource") DataSource zjdbDataSource) throws Exception { // SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); // factoryBean.setDataSource(zjdbDataSource); //解决无法使用自带方法问题 // TODO 使用 MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBean MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean(); factory.setDataSource(zjdbDataSource); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { factory.setMapperLocations(resolver.getResources("classpath:/yin/ketech/app/mapper/*/*.xml")); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } //解决无法使用分页问题 // factory.setPlugins(new Interceptor[]{new MybatisPlusConfig().paginationInterceptor()}); //设置Oracle主键自增 factory.setGlobalConfig(new MybatisPlusConfig().globalConfig()); return factory.getObject(); } @Primary @Bean(name = "zjdbSqlSessionTemplate") public SqlSessionTemplate zjdbSqlSessionTemplate(@Qualifier("zjdbSqlSessionFactory") SqlSessionFactory zjdbSqlSessionFactory) throws Exception { // 使用上面配置的Factory SqlSessionTemplate template = new SqlSessionTemplate(zjdbSqlSessionFactory); return template; } }
2.数据源db2
package yin.ketech.app.config; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import javax.sql.DataSource; /** * @FileName: ZjFzConfig * @Author Yep * @create 2020-06-17 * @Description: 多数据源-2 */ @Configuration @MapperScan(basePackages = "yin.ketech.app.zjfzmapper", sqlSessionFactoryRef = "zjfzSqlSessionFactory") public class ZjFzConfig { @Bean(name = "zjfzDataSource") @Qualifier("zjfzDataSource") @ConfigurationProperties(prefix = "spring.datasource.zjfz") public DataSource zjfzDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "zjfzSqlSessionFactory") @Qualifier("zjfzSqlSessionFactory") public SqlSessionFactory zjfzSqlSessionFactory(@Qualifier("zjfzDataSource") DataSource zjfzDataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(zjfzDataSource); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { factoryBean.setMapperLocations(resolver.getResources("classpath:/yin/ketech/app/mapper/zjfz/mapper/*.xml")); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } return factoryBean.getObject(); } @Bean(name = "zjfzSqlSessionTemplate") public SqlSessionTemplate zjfzSqlSessionTemplate(@Qualifier("zjfzSqlSessionFactory") SqlSessionFactory zjfzSqlSessionFactory) throws Exception { // 使用上面配置的Factory SqlSessionTemplate template = new SqlSessionTemplate(zjfzSqlSessionFactory); return template; } }
三、多数据源的mapper包最好是分开
在扫描的时候会动态注入,可能在上述代码中会导致数据源自动切换失败:
四、代码中调用
总结
- 1. 注意动态配置的类结合自己的包项目结构修改
- 2. 解决了mybatis-plus自带语句不能使用问题
- 3. 解决无法使用分页问题
- 4. 解决无法使用oracle自增主键问题
以上为个人经验,希望能给大家一个参考,也希望大家多多支持靠谱客。
最后
以上就是典雅樱桃为你收集整理的使用springboot+druid双数据源动态配置操作的全部内容,希望文章能够帮你解决使用springboot+druid双数据源动态配置操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复