1. 程式人生 > >springboot配置多資料來源(不同DB)

springboot配置多資料來源(不同DB)

springBoot整合Mysql+MongoDB 因為在專案中需要用到兩個不同的資料來源。但是又不存在於一個DB中。讓我很是苦惱,不得已只能整合多資料來源。 博主文筆不好,只能講乾貨了。。 目標: 使用springBoot整合mysql於MongoDB,即一個專案操作多資料來源(不同DB)。 步驟一: pom依賴的匯入,這一步大家就很熟悉了~
		<!-- mysql begin -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.5</version>
		</dependency>
		<!-- mysql end -->
步驟二:配置檔案的編寫
server:
  port: 8081
## multifl dataSource
## mysql
spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_xx?useUnicode=true&characterEncoding=utf-8
    username: root
    password: root
ops:
  mongodb:
    host: localhost
    port: 27017
    database: db_xx

 
  
步驟三: 配置類的編寫 1.MongoMasterConfig(為什麼是Master呢,嘿嘿)
@Configuration
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) //必不可少
@EnableMongoRepositories(basePackages = {"com.xx.mapper.mongodb"},mongoTemplateRef = "demo1MongoTemplate") //配置介面資訊
@ComponentScan
@ConfigurationProperties(prefix = "ops.mongodb")// 從配置檔案中獲取資料
public class MongoMasterConfig extends AbstractMongoConfigure {

    @Override
    @Bean(name = "demo1MongoTemplate")
    @Primary
    public MongoTemplate getMongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }
}
     2.MysqlDataSourceConfig
@Configuration
@MapperScan(basePackages = MysqlDataSourceConfig.PACKAGE,sqlSessionFactoryRef = "sqlSessionFactory")
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class MysqlDataSourceConfig {
    //精確定位Mapper介面到master目錄,以便與其他資料隔離
    static final String PACKAGE = "com.xx.mapper.mysql";

    static final String MAPPER_LOCATION = "classpath*:mapper/*.xml";

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

    @Value("${spring.datasource.driverClassName}")
    private String driverClass;

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

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

    /**
     * 建立資料來源
     * @return
     */
    @Bean(name = "dataSource")
    @Primary
    public DataSource masterDataSource(){
        //例項化阿里資料來源
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/db_xx");
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        return druidDataSource;
    }

    /**
     * 事務
     * @return
     */
    @Bean(name = "transactionManager")
    public DataSourceTransactionManager masterTransactionManager(){
        return new DataSourceTransactionManager(masterDataSource());
    }

    /*建立SqlSessionFactory
     */
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("dataSource") DataSource masterDataSource) throws Exception {
        final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(masterDataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()//
                .getResources(MysqlDataSourceConfig.MAPPER_LOCATION));
        return sqlSessionFactoryBean.getObject();
    }

到此為止~,你的專案已經配置好了多資料來源(不同DB),使用時,就像以前一樣。。。