Spring boot配置多数据源

注意事项

  1. 配置文件配置数据源时一定要用jdbc-url 使用url会报错

  2. 指定数据源所使用的mapper路径
    factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/.xml"));

  3. @MapperScan 中的 basePackages 是dao层的包路径 。sqlSessionFactoryRef 、 sqlSessionTemplateRef 是这个dao所用哪个数据源配置

pom

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>

配置文件

spring:
  datasource:
    db1:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://xxx:3306/xx?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&autoReconnect=true&useSSL=true&allowMultiQueries=true
      username: xx
      password: xx
    db2:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://xxx:3306/xx?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&autoReconnect=true&useSSL=true&allowMultiQueries=true
      username: xx
      password: xx

Dao层

package com.example.demo.db1;


import java.util.List;

/**
 * @author : huqy
 * @description
 * @date : 2021/5/21
 */
public interface DB1 {

    List<Object> select();

}
package com.example.demo.db2;

import java.util.List;

public interface DB2{

    List<Object> select();

}

核心配置类

@Configuration
//@MapperScan(basePackages = {"com.example.demo.db1"}, sqlSessionFactoryRef = "sqlSessionFactoryDb1")
//@MapperScan(basePackages = {"com.example.demo.db2"}, sqlSessionFactoryRef = "sqlSessionFactoryDb2")
@MapperScan(basePackages = {"com.example.demo.db1"}, sqlSessionTemplateRef = "sqlSessionTemplateDb1")
@MapperScan(basePackages = {"com.example.demo.db2"}, sqlSessionTemplateRef = "sqlSessionTemplateDb2")
public class DataSourceConfig {

    @Bean(name = "db1")
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource db1() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "db2")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource db2() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryDb1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(db1());
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateDb1() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryDb1());
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryDb2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(db2());
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db2/*.xml"));
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateDb2() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryDb2());
    }
}

测试

/**
 * @author : huqy
 * @description
 * @date : 2021/5/21
 */
@Service
public class DataSourceService {
    @Autowired
    DB1 db1;
    @Autowired
    DB2 db2;

    public void db1(){
        List<Object> select = db1.select();
        System.out.println(select);
    }
    public void db2(){
        List<Object> select = db2.select();
        System.out.println(select);
    }
}

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    DataSourceService dataSourceService;
    @Test
    void contextLoads() {
    }

    @Test
    void db1Test(){
        dataSourceService.db1();
    }

    @Test
    void db2Test(){
        dataSourceService.db2();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值