Spring Bootマルチデータソースの構成
3440 ワード
1.アプリケーション.propertiesに複数のデータソースを追加する構成
2.読み出しファイルのコンフィギュレーションファイルの構成
#
master.datasource.url=jdbc:mysql://***.**.***.**:****/ ?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
master.datasource.username=st
master.datasource.password=select#123
#
cluster.datasource.url=jdbc:mysql://***.***.***.***:****/ ?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
cluster.datasource.username=root
cluster.datasource.password=opRRd4P$iFr@hemE
#
app.datasource.third.url=jdbc:mysql://***.***.***.***:****/ ?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
app.datasource.third.username=cxt_onlyread
app.datasource.third.password=yz1zelX6q02&WOBs
2.読み出しファイルのコンフィギュレーションファイルの構成
package com.caixiaotu.orderreport;
/**
* @author :yousahng520i
* @date :Created in 2019/7/31 20:55
* @description:
* @modified By:
* @version: 1.0
*/
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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 javax.sql.DataSource;
import java.io.IOException;
/**
* @author:wong
*/
@Configuration
// mapper
@MapperScan(basePackages = "com.caixiaotu.orderreport.dao.jly",sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
@Value("${master.datasource.url}")
private String masterDataSourceUrl;
@Value("${master.datasource.username}")
private String masterDatasourceUsername;
@Value("${master.datasource.password}")
private String masterDatasourcePassword;
@Value("com.mysql.jdbc.Driver ")
private String masterDatasourceDriverClassName;
// dataSource bean
@Bean(name = "masterDataSource")
@Primary //primary bean , bean,
public DataSource masterDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUsername(masterDatasourceUsername);
dataSource.setPassword(masterDatasourcePassword);
dataSource.setDriverClassName(masterDatasourceDriverClassName);
dataSource.setUrl(masterDataSourceUrl);
return dataSource;
}
// sqlSessionFactory bean
@Bean(name = "masterSqlSessionFactory")
@Primary// ,
// @Qualifier bean, bean name
//@Autowired bean bean
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
// mapper.xml
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
return sqlSessionFactoryBean.getObject();
}
}