package cn.learn.mutil.datasource.config.ds;

import javax.sql.DataSource;

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 org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
//扫描 Mapper 接口并容器管理
@MapperScan(basePackages=MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef="masterSqlSessionFactory")
public class MasterDataSourceConfig {
	
	// master目录，跟踪数据源用
	static final String PACKAGE = "cn.learn.mutil.datasource.dao.master";
	static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
	
    @Value("${master.datasource.url}")
    private String url;

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

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

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

    @Bean(name="masterDataSource")
    @Primary
    public DataSource masterDataSource() {
    	// Druid
//    	DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setDriverClassName(driverClass);
//        dataSource.setUrl(url);
//        dataSource.setUsername(user);
//        dataSource.setPassword(password);
    	// hikari
    	HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setJdbcUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        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.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MasterDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}

@@ -1436,351 +1436,362 @@
 %0D%0A%0D%0A
-    @Bean(name=%22
+%09@Value(%22$%7B
 master
-DataS
+.datas
 ource
-%22)%0D%0A    @Primary%0D%0A    public DataSource 
+.filters%7D%22)%0D%0A%09private String filters;%0D%0A%09@Value(%22$%7B
 master
-DataS
+.datas
 ource
-() %7B%0D%0A    %09// Druid%0D%0A//    %09DruidDataSource dataSource = new DruidDataS
+.connectionProperties%7D%22)%0D%0A%09private String connectionProperties;%0D%0A%09@Value(%22$%7Bmaster.datas
 ource
-();%0D%0A//        dataSource.setDriverClassName(driverClass);%0D%0A//        dataSource.setUrl(url);%0D%0A//
+.useGlobalDataSourceStat%7D%22)%0D%0A%09private boolean useGlobalDataSourceStat;%0D%0A%0D%0A
     
-    dataSource.setUsername(user);%0D%0A//        dataSource.setPassword(password);
+@Bean(name=%22masterDataSource%22)%0D%0A    @Primary%0D%0A    public DataSource masterDataSource() %7B
 %0D%0A  
@@ -1800,27 +1800,25 @@
 %09// 
-hikari
+Druid
 %0D%0A    %09
-Hikari
+Druid
 Data
@@ -1841,22 +1841,21 @@
 e = new 
-Hikari
+Druid
 DataSour
@@ -1940,12 +1940,8 @@
 .set
-Jdbc
 Url(
@@ -2025,24 +2025,619 @@
 password);%0D%0A
+        dataSource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);%0D%0A        try %7B%0D%0A        %09dataSource.setFilters(filters);%0D%0A        %7D catch (SQLException e) %7B%0D%0A            System.err.println(%22druid configuration initialization filter: %22+ e);%0D%0A        %7D%0D%0A        dataSource.setConnectionProperties(connectionProperties);%0D%0A        %0D%0A    %09// hikari%0D%0A//    %09HikariDataSource dataSource = new HikariDataSource();%0D%0A//        dataSource.setDriverClassName(driverClass);%0D%0A//        dataSource.setJdbcUrl(url);%0D%0A//        dataSource.setUsername(user);%0D%0A//        dataSource.setPassword(password);%0D%0A
         retu

Update MasterDataSourceConfig.java