SpringBoot+Mybatis多数据源配置

SpringBoot+Mybatis多数据源配置

目录:

主要思想:两套配置文件,两套mapper,两套SqlSessionFactory,各自处理各自的业务,这个两套mapper都可以进行增删改查的操作。

 

多数据源配置项:配置多个数据源url;username;password;driver。

配置类DatabaseConfig1:注意设置@Primary默认配置注解

@Configuration
@MapperScan(basePackages = "com.**/mapper路径.bcop2021125.mapper.db1", sqlSessionFactoryRef = "SqlSessionFactory1")
public class DatabaseConfig1 {

    @Primary
    @Bean(name = {"DatebaseProperties"})
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties DataSourceProperties() {
        return new DataSourceProperties();
    }

    @Primary
    @Bean(name = {"DataSource"})
    public DataSource DataSource(@Qualifier("DatebaseProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Primary
    @Bean("SqlSessionFactory1")
    public SqlSessionFactory SqlSessionFactory1(@Qualifier("DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/*.xml"));
        return bean.getObject();
    }
    @Primary
    @Bean("SqlSessionTemplate1")
    public SqlSessionTemplate SqlSessionTemplate1(@Qualifier("SqlSessionFactory1")SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

配置类DatabaseConfig2:

@Configuration
@MapperScan(basePackages = "com.***/mapper路径.bcop2021125.mapper.db2", sqlSessionFactoryRef = "SqlSessionFactory2")

public class DatabaseConfig2 {
    @Bean(name = {"DatebaseProperties2"})
    @ConfigurationProperties("spring.datasource2")
    public DataSourceProperties DataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = {"DataSource2"})
    public DataSource DataSource(@Qualifier("DatebaseProperties2") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean("SqlSessionFactory2")
    public SqlSessionFactory SqlSessionFactory1(@Qualifier("DataSource2") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/*.xml"));
        return bean.getObject();
    }

    @Bean("SqlSessionTemplate2")
    public SqlSessionTemplate SqlSessionTemplate1(@Qualifier("SqlSessionFactory2")SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值