SpringBoot多数据源配置 !

1.application.yml

server:
  port: 9091


mysql:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///ad
    username: root
    password: 123456
hbase:
  datasource:
    driver-class-name: org.apache.phoenix.jdbc.PhoenixDriver
    url: jdbc:phoenix:192.168.220.130:2181


mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  configuration:
    # 自动驼峰命名映射
    map-underscore-to-camel-case: true

 

2. 配置类


@Configuration
@MapperScan(basePackages = "com.alip15.mysql.mapper", sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MysqlConfig {


    @Bean(name = "mysqlDataSource")
    @ConfigurationProperties(prefix = "mysql.datasource")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }



    @Bean(name = "mysqlSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "mysqlTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "mysqlSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
package com.labelwall.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.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @Author sunlin
 * @Date 2019/4/18 17:17
 * @Describe
 */

@Configuration
@MapperScan(basePackages = "com.labelwall.hbase.mapper", sqlSessionFactoryRef = "hbaseSqlSessionFactory")
public class HBaseConfig {


    @Bean(name = "hbaseDataSource")
    @ConfigurationProperties(prefix = "hbase.datasource")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "hbaseSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("hbaseDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "hbaseTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("hbaseDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "hbaseSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("hbaseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}

 

你可能感兴趣的:(SpringBoot多数据源配置 !)