springboot+mybatis配置多数据源

两个配置类建两个多数据源配置

## master 数据源配置
aaa.datasource.url=jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8
aaa.datasource.username=root
aaa.datasource.password=
aaa.datasource.driverClassName=com.mysql.jdbc.Driver
 
 
## slave 数据源配置
bbb.datasource.url=jdbc:mysql://localhost:3308/learn?useUnicode=true&characterEncoding=utf8
bbb.datasource.username=root
bbb.datasource.password=
`

主要配置两个配置类,两个SqlSessionFactory,读取指定包下面的接口
```java
@Configuration
//指定哪个包下面的接口使用xxxSqlSessionFactory
@MapperScan(basePackages = {"aaa.mapper"},
    sqlSessionFactoryRef = "aaaSqlSessionFactory")
public class InquiryDataSourceConfig {



    @Primary  //主数据源必须
    @Bean(name = "aaaDataSource")//数据源
    @ConfigurationProperties(prefix = "aaa")
    public DataSource inquiryDataSource() {
        DataSource dataSource = DruidDataSourceBuilder.create().build();
        return dataSource;
    }

    @Primary
    @Bean(name = "aaaSqlSessionFactory")
    public SqlSessionFactory inquirySqlSessionFactory(@Qualifier("aaaDataSource") DataSource datasource)
        throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
            new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/aaa/*.xml"));//扫描这个目录下面的xml文件
        // 配置包别名
        bean.setTypeAliasesPackage("aaa.database.dataobject");

        SqlSessionFactory factory = bean.getObject();
        return factory;
    }

    @Primary
    @Bean("aaaSqlSessionTemplate")
    public SqlSessionTemplate
        inquirySqlSessionTemplate(@Qualifier("aaaSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }

    @Primary
    @Bean(name = "aaaTransactionManager")
    public DataSourceTransactionManager inquiryTransactionManager() {
        return new DataSourceTransactionManager(inquiryDataSource());
    }

}

第二个数据源

@Configuration
//指定哪个包下面的接口使用xxxSqlSessionFactory
@MapperScan(basePackages = {"bbb.mapper"},
    sqlSessionFactoryRef = "bbbSqlSessionFactory")
public class InquiryDataSourceConfig {



   
    @Bean(name = "bbbDataSource")//数据源
    @ConfigurationProperties(prefix = "bbb")
    public DataSource inquiryDataSource() {
        DataSource dataSource = DruidDataSourceBuilder.create().build();
        return dataSource;
    }

    
    @Bean(name = "bbbSqlSessionFactory")
    public SqlSessionFactory inquirySqlSessionFactory(@Qualifier("bbbDataSource") DataSource datasource)
        throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
            new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/bbb/*.xml"));//扫描这个目录下面的xml文件
        // 配置包别名
        bean.setTypeAliasesPackage("bbb.database.dataobject");

        SqlSessionFactory factory = bean.getObject();
        return factory;
    }

    
    @Bean("bbbSqlSessionTemplate")
    public SqlSessionTemplate
        inquirySqlSessionTemplate(@Qualifier("bbbSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }

   
    @Bean(name = "bbbTransactionManager")
    public DataSourceTransactionManager inquiryTransactionManager() {
        return new DataSourceTransactionManager(inquiryDataSource());
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值