SpringBoot配置多数据源(整合MyBatis-plus)

业务需要,配置不同账号访问不同数据库。项目原来使用的单数据源+MyBatis-plus。

按下面代码整合一切正常。直接上代码:

排除SpringBoot的自动配置类DataSourceAutoConfiguration

@SpringBootApplication(exclude = {
        DataSourceAutoConfiguration.class
})

在properties中配置多数据源连接信息,名称为consultant和dw

spring.datasource.consultant.jdbc-url=jdbc:mysql://xxxx:3306/consultant?createDatabaseIfNotExist=true&autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8
spring.datasource.consultant.username=dev_w
spring.datasource.consultant.password=D83GskdgdseGDDev
spring.datasource.consultant.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.dw.jdbc-url=jdbc:mysql://xxxx:3306/up_dw?createDatabaseIfNotExist=true&autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8
spring.datasource.dw.username=bs
spring.datasource.dw.password=oDZjmrircB+EYoITyEnBPcRp
spring.datasource.dw.driver-class-name=com.mysql.cj.jdbc.Driver

创建数据库配置类

配置2个数据源

@Configuration
public class DataSourceConfig {

    @Bean(name = "consultantDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.consultant")
    public DataSource consultantDbDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "dwDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.dw")
    public DataSource dwDbDataSource() {
        return DataSourceBuilder.create().build();
    }
}

 dw库SqlSessionFactory,注意扫描不同的mapper接口包和对应的xml文件包

@Configuration
@MapperScan(basePackages = {"com.xxx.xxx.dw.mapper"}, sqlSessionFactoryRef = "sqlSessionFactoryDw")
public class DwDBConfig {

    @Autowired
    @Qualifier("dwDataSource")
    private DataSource dataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactoryDw() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/dw/*.xml"));
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        sqlSessionFactoryBean.setConfiguration(configuration);
        sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateDw() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryDw());
    }
}

 consultant库SqlSessionFactory

@Configuration
@MapperScan(basePackages = {"com.xxx.xxx.communication.mapper"},
        sqlSessionFactoryRef = "sqlSessionFactoryConsultant")
public class ConsultantDBConfig {

    @Autowired
    @Qualifier("consultantDataSource")
    private DataSource dataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactoryConsultant() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/consultant/*.xml"));
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        sqlSessionFactoryBean.setConfiguration(configuration);
        sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateConsultant() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryConsultant());
    }
}

整合完毕。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值