雙數據源的配置

雙數據源的簡單配置

一、配置主數據源(master)

@Configuration
@MapperScan(basePackages = "主數據源的mapper包路徑", sqlSessionTemplateRef  = "masterSqlSessionTemplate")
public class MasterDataSourceConfiguration {
		@Value("${spring.datasource.master.driver-class-name}")
	    private String driverClassName;

	    @Value("${spring.datasource.master.url}")
	    private String url;

	    @Value("${spring.datasource.master.username}")
	    private String username;

	    @Value("${spring.datasource.master.password}")
	    private String password;

	    @Bean(name = "masterDataSource")
	    @Primary
	    public DataSource dataSource() {
	        DruidDataSource dataSource = new DruidDataSource();
	        dataSource.setDriverClassName(this.driverClassName);
	        dataSource.setUrl(this.url);
	        dataSource.setUsername(this.username);
	        dataSource.setPassword(this.password);
	        return dataSource;
	    }

	    @Bean(name = "masterSqlSessionFactory")
	    @Primary
	    public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
	       try {
	    	SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
	        bean.setDataSource(dataSource);
	        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*/*.xml"));
	        return bean.getObject();
        }catch(Exception e) {
        	return null;
        }
	    }

	    @Bean(name = "masterTransactionManager")
	    @Primary
	    public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
	        return new DataSourceTransactionManager(dataSource);
	    }

	    @Bean(name = "masterSqlSessionTemplate")
	    @Primary
	    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
	        try {
	    	return new SqlSessionTemplate(sqlSessionFactory);
	        }catch(Exception e) {
	        	return null;
	        }
	    }
	}

二、配置副數據源(slaver)

@Configuration
@MapperScan(basePackages = "從數據源mapper路徑", sqlSessionTemplateRef  = "slaverSqlSessionTemplate")
public class SlaverDataSourceConfiguration {
	@Value("${spring.datasource.slaver.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.slaver.url}")
    private String url;

    @Value("${spring.datasource.slaver.username}")
    private String username;

    @Value("${spring.datasource.slaver.password}")
    private String password;


    @Bean(name = "slaverDataSource")
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(this.driverClassName);
        dataSource.setUrl(this.url);
        dataSource.setUsername(this.username);
        dataSource.setPassword(this.password);
        return dataSource;
    }

    @Bean(name = "slaverSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("slaverDataSource") DataSource dataSource) throws Exception {
        try {
    	SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*/*.xml"));
        return bean.getObject();
        }catch(Exception e) {
        	return null;
        }
    }

    @Bean(name = "slaverTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("slaverDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "slaverSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("slaverSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    	 try {
    	return new SqlSessionTemplate(sqlSessionFactory);
         }catch(Exception e) {
         	return null;
         }
    }
}

三、數據源配置

datasource:
    master:
      driver-class-name: oracle.jdbc.OracleDriver
      url: jdbc:oracle:thin:**
      username:**
      password: **
    slaver:
      driver-class-name: oracle.jdbc.OracleDriver
      url: jdbc:oracle:thin:**
      username:**
      password: **

以上,注意:master與slaver需要單獨建mapper包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值