SpringBoot+Mybatis项目中多数据源配置

447 阅读2分钟
  1. 首先在配置文件中配置多数据源的数据库连接密码、url等信息,每个数据源采用不同的配置名来命名
1、第一个数据源
spring.datasource.druid.name=test1
spring.datasource.druid.url= jdbc:mysql://****:3306/test1?characterEncoding=utf8&serverTimezone=Asia/Shanghai&connectTimeout=20000&socketTimeout=20000&autoReconnect=true&useUnicode=true&useSSL=false&allowMultiQueries=true
spring.datasource.druid.username= root
spring.datasource.druid.password= ***
#spring.datasource.druid.driver-class-name= com.mysql.cj.jdbc.Driver

2、第二个数据源
spring.datasource.druid.onlineweb.name=test2
spring.datasource.druid.onlineweb.url= jdbc:mysql://****:3306/test2?characterEncoding=utf8&serverTimezone=Asia/Shanghai&connectTimeout=20000&socketTimeout=20000&autoReconnect=true&useUnicode=true&useSSL=false 
spring.datasource.druid.onlineweb.username= root
spring.datasource.druid.onlineweb.password= ***
spring.datasource.druid.onlineweb.driver-class-name= com.mysql.cj.jdbc.Driver

3、第三个数据源
spring.datasource.druid.datahouse.name=datahouse
spring.datasource.druid.datahouse.url= jdbc:mysql://****:3306/test3?characterEncoding=utf8&serverTimezone=Asia/Shanghai&connectTimeout=20000&socketTimeout=20000&autoReconnect=true&useUnicode=true&useSSL=false
spring.datasource.druid.datahouse.username=root
spring.datasource.druid.datahouse.password=***
spring.datasource.druid.datahouse.driver-class-name= com.mysql.cj.jdbc.Driver

2.编写配置类,将多数据源的配置应用到SpringBoot中

@Configuration
@MapperScan(basePackages = {"com.zgl.crm.scheduling.dao.onlineWeb"}, sqlSessionFactoryRef = "onlinewebSqlSessionFactory")
@MapperScan(basePackages = {"com.zgl.crm.scheduling.dao.datahouse"}, sqlSessionFactoryRef = "datahouseSqlSessionFactory")
@MapperScan(basePackages = {"com.zgl.crm.dao"}, sqlSessionFactoryRef = "crmSqlSessionFactory")
public class DataSourceConfig {

    /********* test2  ***********/
    @Bean(name = "onlinewebDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.onlineweb")
    public DataSource onlinewebDataSource() {
        //指定使用DruidDataSource
        return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
    }

    @Bean(name = "onlinewebSqlSessionFactory")
    public SqlSessionFactory onlinewebSqlSessionFactory(@Qualifier("onlinewebDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "onlinewebTransactionManager")
    public DataSourceTransactionManager onlinewebTransactionManager(@Qualifier("onlinewebDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "onlinewebSqlSessionTemplate")
    public SqlSessionTemplate onlinewebSqlSessionTemplate(@Qualifier("onlinewebSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    /********* test2  ***********/



    /********* test1  ***********/
    @Bean(name = "crmDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    @Primary
    public DataSource crmDataSource() {
        //指定使用DruidDataSource
        return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
    }

    @Bean(name = "crmSqlSessionFactory")
    @Primary
    public SqlSessionFactory crmSqlSessionFactory(@Qualifier("crmDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("com.zgl.crm.dao");

        MybatisProperties properties = new MybatisProperties();
        properties.setMapperLocations(new String[]{"classpath:mybatis/**/*.xml"});
        bean.setMapperLocations(properties.resolveMapperLocations());
        bean.setDefaultEnumTypeHandler(BaseEnumTypeHandler.class);
        bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return bean.getObject();
    }

    @Bean(name = "crmTransactionManager")
    @Primary
    public DataSourceTransactionManager crmTransactionManager(@Qualifier("crmDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "crmSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate crmSqlSessionTemplate(@Qualifier("crmSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
        return sqlSessionTemplate;
    }
    /********* test1  ***********/



    /********* test3  ***********/
    @Bean(name = "datahouseDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.datahouse")
    public DataSource datahouseDataSource() {
        //指定使用DruidDataSource
        return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
    }

    @Bean(name = "datahouseSqlSessionFactory")
    public SqlSessionFactory datahouseSqlSessionFactory(@Qualifier("datahouseDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "datahouseTransactionManager")
    public DataSourceTransactionManager datahouseTransactionManager(@Qualifier("datahouseDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "datahouseSqlSessionTemplate")
    public SqlSessionTemplate datahouseSqlSessionTemplate(@Qualifier("datahouseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
/********* test3  ***********/

在上述代码中,test1数据源源使用@Primary注解来声明这是一个主数据源(默认数据源),多数据源配置时必不可少。 使用@ConfigurationProperties(prefix = "spring.datasource.druid")来读取配置文件中spring.datasource.druid开头的配置项。 配置类记得要加上@Configuration注解。 在@MapperScan这个注解中,@sqlSessionFactoryRef的值引用下面代码中注入SqlSessionFactory的bean的值,再配合@basePackages来说明该目录下的mapper文件使用哪一个数据源。

  • 至此,多数据源配置完成,在项目运行调用不同的mapper文件的sql时候就会调用不同的数据源