Oracle进行多数据源配置

有时候需要在不同数据源中获取数据,所以需要进行多数据源的配置,话不多说,直接上代码:

项目结构:

这里使用的是Oracle数据库,用的是IDEA,Application.properties配置文件数据库信息:

首先关闭Springboot的默认数据源读取

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

然后对数据源进行注入,使Spring能够读取数据源信息

package cn.cdhy.pdf2image.config;



import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @Description: 数据源一配置
 */
@Configuration
@MapperScan(basePackages = "cn.cdhy.pdf2image.mapper.cst", sqlSessionTemplateRef  = "cstSqlSessionTemplate")
public class DataSourceCstConfig {

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

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

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

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

}
package cn.cdhy.pdf2image.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @Description: 数据源二配置
 */
@Configuration
@MapperScan(basePackages = "cn.cdhy.pdf2image.mapper.dzda", sqlSessionTemplateRef = "dzdaSqlSessionTemplate")
public class DataSourceDzdaConfig {

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

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

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

    @Bean(name = "dzdaSqlSessionTemplate")
    public SqlSessionTemplate dzdaSqlSessionTemplate(@Qualifier("dzdaSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值