mybatis配置多数据源

多源数据库配置:三种数据库连接池,好文章

https://www.cnblogs.com/wzk-0000/p/9544432.html

 

https://blog.csdn.net/weixin_37664872/article/details/80088014

https://blog.csdn.net/tuesdayma/article/details/81081666

网上的文章基本上都是只有多数据源或只有动态数据源,而最近的项目需要同时使用两种方式,记录一下配置方法供大家参考。

应用场景

项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库。

多数据源

首先要将spring boot自带的DataSourceAutoConfiguration禁掉,因为它会读取application.properties文件的spring.datasource.*属性并自动配置单数据源。在@SpringBootApplication注解中添加exclude属性即可:

 
  1. @SpringBootApplication(exclude = {

  2. DataSourceAutoConfiguration.class

  3. })

  4. public class TitanWebApplication {

  5. public static void main(String[] args) {

  6. SpringApplication.run(TitanWebApplication.class, args);

  7. }

  8. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

然后在application.properties中配置多数据源连接信息:

 
  1. # titan库

  2. spring.datasource.titan-master.url=jdbc:mysql://X.X.X.X:port/titan?characterEncoding=UTF-8

  3. spring.datasource.titan-master.username=

  4. spring.datasource.titan-master.password=

  5. spring.datasource.titan-master.driver-class-name=com.mysql.jdbc.Driver

  6. # 连接池配置

  7. # 省略

  8.  
  9. # 其它库

  10. spring.datasource.db2.url=jdbc:mysql://X.X.X.X:port/titan2?characterEncoding=UTF-8

  11. spring.datasource.db2.username=

  12. spring.datasource.db2.password=

  13. spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

由于我们禁掉了自动数据源配置,因些下一步就需要手动将这些数据源创建出来:

 
  1. @Configuration

  2. public class DataSourceConfig {

  3.  
  4. @Bean(name = "titanMasterDS")

  5. @ConfigurationProperties(prefix = "spring.datasource.titan-master") // application.properteis中对应属性的前缀

  6. public DataSource dataSource1() {

  7. return DataSourceBuilder.create().build();

  8. }

  9.  
  10.  
  11.  
  12.  
  13. @Bean(name = "ds2")

  14. @ConfigurationProperties(prefix = "spring.datasource.db2") // application.properteis中对应属性的前缀

  15. public DataSource dataSource2() {

  16. return DataSourceBuilder.create().build();

  17. }

  18.  
  19. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

接下来需要配置两个mybatis的SqlSessionFactory分别使用不同的数据源:

 
  1. @Configuration

  2. @MapperScan(basePackages = {"titan.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory1")

  3. public class MybatisDbAConfig {

  4.  
  5. @Autowired

  6. @Qualifier("titanMasterDS")

  7. private DataSource ds1;

  8.  
  9.  
  10. @Bean

  11. public SqlSessionFactory sqlSessionFactory1() throws Exception {

  12. SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

  13. factoryBean.setDataSource(ds1); // 使用titan数据源, 连接titan库

  14.  
  15. return factoryBean.getObject();

  16.  
  17. }

  18.  
  19. @Bean

  20. public SqlSessionTemplate sqlSessionTemplate1() throws Exception {

  21. SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1()); // 使用上面配置的Factory

  22. return template;

  23. }

  24. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

经过上面的配置后,titan.mapper下的Mapper接口,都会使用titan数据源。同理可配第二个SqlSessionFactory:

 
  1. @Configuration

  2. @MapperScan(basePackages = {"other.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory2")

  3. public class MybatisDbBConfig {

  4. @Autowired

  5. @Qualifier("ds2")

  6. private DataSource ds2;

  7.  
  8. @Bean

  9. public SqlSessionFactory sqlSessionFactory2() throws Exception {

  10. SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

  11. factoryBean.setDataSource(ds2);

  12.  
  13.  
  14. return factoryBean.getObject();

  15.  
  16. }

  17.  
  18. @Bean

  19. public SqlSessionTemplate sqlSessionTemplate2() throws Exception {

  20. SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());

  21. return template;

  22. }

  23. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

完成这些配置后,假设有2个Mapper titan.mapper.UserMapperother.mapper.RoleMapper,使用前者时会自动连接titan库,后者连接ds2库。

使用此配置后需要将*Mapper.xml文件放入与Mappr.java同级目录,如使用的是idea可以添加依赖,亲测可用

 

  1. <build>  
  2.     <resources>  
  3.         <resource>  
  4.             <directory>src/main/java</directory>  
  5.             <includes>  
  6.                 <include>**/*.properties</include>  
  7.                 <include>**/*.xml</include>  
  8.             </includes>  
  9.             <filtering>false</filtering>  
  10.         </resource>  
  11.     </resources>  
  12. </build>  

转载地址: https://blog.csdn.net/neosmith/article/details/61202084

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值