springboot+mybatis多数据源配置

1、数据源信息

spring.datasource.pg.driverClassName = org.postgresql.Driver
spring.datasource.pg.url = jdbc:postgresql://x.x.x.x:xxxx/xxx
spring.datasource.pg.username = xxx
spring.datasource.pg.password = xxx


spring.datasource.ms.driverClassName = com.mysql.jdbc.Driver
spring.datasource.ms.url = jdbc:mysql://x.x.x.x:xxxx/xxx
spring.datasource.ms.username = xxx
spring.datasource.ms.password = xxx

2、mybatis相关配置

数据源1配置:(@Primary为设置主数据源标签,在多数据源情况下必须指定主数据源)

@Configuration
@MapperScan(basePackages = "com.hysyn.mapper.pg", sqlSessionTemplateRef  = "pgSqlSessionTemplate")
public class DataSource1Config {

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

    @Bean(name = "pgSqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("pgDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/pg/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "pgTransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("pgDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "pgSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("pgSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
数据源配置:

@Configuration
@MapperScan(basePackages = "com.hysyn.mapper.ms", sqlSessionTemplateRef  = "msSqlSessionTemplate")
public class DataSource2Config {

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

    @Bean(name = "msSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("msDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/ms/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "msTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("msDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "msSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("msSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值