springboot+DruidDataSource 双数据源配置

配置文件:

spring:
  profiles:
      active: default
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://120.55.111.11:3306/weichuang?characterEncoding=UTF-8&autoReconnect=true&useSSL=false
    username: root
    password: root
  datasource-v:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
    url: jdbc:sqlserver://172.16.111.1:1433;DatabaseName=zkteco_database
    username: root
    password: root

主数据源配置
指定数据源对应的Repository定义位置,用@Primary区分主数据源。

/**
 * 主数据库连接
 *
 * @author wzx
 * @create 2017/12/25 13:48
 */
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {

    // 精确到 master 目录,以便跟其他数据源隔离
    static final String PACKAGE = "com.weichi.repository";
    static final String MAPPER_LOCATION = "classpath:mybaits/mybatis-config.xml";

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

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

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

    @Value("${spring.datasource.driver-class-name}")
    private String driverClass;

    @Bean(name = "masterDataSource")
    @Primary
    public DataSource masterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        try {
            dataSource.setFilters("stat, wall");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }

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

    @Bean(name = "masterSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setTypeAliasesPackage("com.weichi.domain");
        /*sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MasterDataSourceConfig.MAPPER_LOCATION));*/


        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            sessionFactory.setConfigLocation(resolver.getResource(MasterDataSourceConfig.MAPPER_LOCATION));
            return sessionFactory.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}

次数据源配置

/**
 * 从数据库连接
 *
 * @author wzx
 * @create 2017/12/25 14:01
 */
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = ClusterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "clusterSqlSessionFactory")
public class ClusterDataSourceConfig {

    // 精确到 cluster 目录,以便跟其他数据源隔离
    static final String PACKAGE = "com.weichi.veichidb.repository";
    static final String MAPPER_LOCATION = "classpath:mybaits/mybatis-config.xml";

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

    @Value("${spring.datasource-v.username}")
    private String user;

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

    @Value("${spring.datasource-v.driver-class-name}")
    private String driverClass;

    @Bean(name = "clusterDataSource")
    public DataSource clusterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        try {
            dataSource.setFilters("stat, wall");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }

    @Bean(name = "clusterTransactionManager")
    public DataSourceTransactionManager clusterTransactionManager() {
        return new DataSourceTransactionManager(clusterDataSource());
    }

    @Bean(name = "clusterSqlSessionFactory")
    public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource clusterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(clusterDataSource);
        sessionFactory.setTypeAliasesPackage("com.weichi.veichidb.domain");
        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            sessionFactory.setConfigLocation(resolver.getResource(MasterDataSourceConfig.MAPPER_LOCATION));
            return sessionFactory.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

注意:
由于数据源是mysql和sqlserver两种,在使用分页插件PageHelper时取消指定数据库,以及驼峰设置

mybatis:
  configuration:
    map-underscore-to-camel-case: true
    #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

pagehelper:
  reasonable: false
  autoRuntimeDialect: true
  supportMethodsArguments: true
  params: count=countSql

mybatis-config.xml





    
        
        
    


你可能感兴趣的:(java,数据库)