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);
    }

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章