1.配置文件 application.yml,我这里用的postgresql,Oracle MySql等用相应的jdbc和Driver就行

#datasource
spring:
  application:
    name: bpas
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    bpas:
      filters: stat,wall,slf4j
      url: jdbc:postgresql://192.168.0.111:5432/bpas?characterEncoding=utf-8
      username: postgres
      password: postgres
      driver-class-name: org.postgresql.Driver
      initialSize: 1
      minIdle: 3
      maxActive: 20
      maxWait: 60000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 30000
      validationQuery: "SELECT 1"
      testWhileIdle: false
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
    bicp:
      filters: stat,wall,slf4j
      url: jdbc:postgresql://192.168.0.111:5432/bicp?characterEncoding=utf-8
      username: postgres
      password: postgres
      driver-class-name: org.postgresql.Driver
      initialSize: 1
      minIdle: 3
      maxActive: 20
      maxWait: 60000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 30000
      validationQuery: "SELECT 1"
      testWhileIdle: false
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20

2.项目目录

springboot多数据源其中一个连不上能启动么 springboot2多数据源配置_多数据源

3.Java配置文件,主数据源

/**
 * bpas主数据库配置
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.datasource.bpas")
@MapperScan(basePackages = BpasDruidConfig.PACKAGE, sqlSessionFactoryRef = "bpasSqlSessionFactory")
public class BpasDruidConfig {
    //dao层的包路径
    static final String PACKAGE = "com.test.bpas.mapper.bpasmapper";
    //mapper文件的相对路径
    private static final String MAPPER_LOCATION = "classpath:sqlmapper/bpasmapper/*.xml";

    private String filters;
    private String url;
    private String username;
    private String password;
    private String driverClassName;
    private int initialSize;
    private int minIdle;
    private int maxActive;
    private long maxWait;
    private long timeBetweenEvictionRunsMillis;
    private long minEvictableIdleTimeMillis;
    private String validationQuery;
    private boolean testWhileIdle;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private boolean poolPreparedStatements;
    private int maxPoolPreparedStatementPerConnectionSize;

    // 主数据源使用@Primary注解进行标识
    @Primary
    @Bean(name = "bpasDataSource")
    public DataSource bpasDataSource() throws SQLException {
        DruidDataSource druid = new DruidDataSource();
        // 监控统计拦截的filters
        druid.setFilters(filters);

        // 配置基本属性
        druid.setDriverClassName(driverClassName);
        druid.setUsername(username);
        druid.setPassword(password);
        druid.setUrl(url);

        //初始化时建立物理连接的个数
        druid.setInitialSize(initialSize);
        //最大连接池数量
        druid.setMaxActive(maxActive);
        //最小连接池数量
        druid.setMinIdle(minIdle);
        //获取连接时最大等待时间,单位毫秒。
        druid.setMaxWait(maxWait);
        //间隔多久进行一次检测,检测需要关闭的空闲连接
        druid.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        //一个连接在池中最小生存的时间
        druid.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        //用来检测连接是否有效的sql
        druid.setValidationQuery(validationQuery);
        //建议配置为true,不影响性能,并且保证安全性。
        druid.setTestWhileIdle(testWhileIdle);
        //申请连接时执行validationQuery检测连接是否有效
        druid.setTestOnBorrow(testOnBorrow);
        druid.setTestOnReturn(testOnReturn);
        //是否缓存preparedStatement,也就是PSCache,oracle设为true,mysql设为false。分库分表较多推荐设置为false
        druid.setPoolPreparedStatements(poolPreparedStatements);
        // 打开PSCache时,指定每个连接上PSCache的大小
        druid.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);

        return druid;
    }

    // 创建该数据源的事务管理
    @Primary
    @Bean(name = "bpasTransactionManager")
    public DataSourceTransactionManager bpasTransactionManager(@Qualifier("bpasDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    // 创建Mybatis的连接会话工厂实例
    @Primary
    @Bean(name = "bpasSqlSessionFactory")
    public SqlSessionFactory bpasSqlSessionFactory(@Qualifier("bpasDataSource") DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        // 设置数据源bean
        sessionFactory.setDataSource(dataSource);
        // 设置mapper文件路径
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(BpasDruidConfig.MAPPER_LOCATION));

        return sessionFactory.getObject();
    }
}

4.Java配置,从数据源

/**
 * bicp从数据库配置
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.datasource.bicp")
@MapperScan(basePackages = BicpDruidConfig.PACKAGE, sqlSessionFactoryRef = "bicpSqlSessionFactory")
public class BicpDruidConfig {
    //dao层的包路径
    static final String PACKAGE = "com.test.bpas.mapper.bicpmapper";
    //mapper文件的相对路径
    private static final String MAPPER_LOCATION = "classpath:sqlmapper/bicpmapper/*.xml";

    private String filters;
    private String url;
    private String username;
    private String password;
    private String driverClassName;
    private int initialSize;
    private int minIdle;
    private int maxActive;
    private long maxWait;
    private long timeBetweenEvictionRunsMillis;
    private long minEvictableIdleTimeMillis;
    private String validationQuery;
    private boolean testWhileIdle;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private boolean poolPreparedStatements;
    private int maxPoolPreparedStatementPerConnectionSize;

    @Bean(name = "bicpDataSource")
    public DataSource bicpDataSource() throws SQLException {
        DruidDataSource druid = new DruidDataSource();
        // 监控统计拦截的filters
        druid.setFilters(filters);

        // 配置基本属性
        druid.setDriverClassName(driverClassName);
        druid.setUsername(username);
        druid.setPassword(password);
        druid.setUrl(url);

        //初始化时建立物理连接的个数
        druid.setInitialSize(initialSize);
        //最大连接池数量
        druid.setMaxActive(maxActive);
        //最小连接池数量
        druid.setMinIdle(minIdle);
        //获取连接时最大等待时间,单位毫秒。
        druid.setMaxWait(maxWait);
        //间隔多久进行一次检测,检测需要关闭的空闲连接
        druid.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        //一个连接在池中最小生存的时间
        druid.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        //用来检测连接是否有效的sql
        druid.setValidationQuery(validationQuery);
        //建议配置为true,不影响性能,并且保证安全性。
        druid.setTestWhileIdle(testWhileIdle);
        //申请连接时执行validationQuery检测连接是否有效
        druid.setTestOnBorrow(testOnBorrow);
        druid.setTestOnReturn(testOnReturn);
        //是否缓存preparedStatement,也就是PSCache,oracle设为true,mysql设为false。分库分表较多推荐设置为false
        druid.setPoolPreparedStatements(poolPreparedStatements);
        // 打开PSCache时,指定每个连接上PSCache的大小
        druid.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);

        return druid;
    }

    // 创建该数据源的事务管理
    @Bean(name = "bicpTransactionManager")
    public DataSourceTransactionManager bicpTransactionManager(@Qualifier("bicpDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    // 创建Mybatis的连接会话工厂实例
    @Bean(name = "bicpSqlSessionFactory")
    public SqlSessionFactory bicpSqlSessionFactory(@Qualifier("bicpDataSource") DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        // 设置数据源bean
        sessionFactory.setDataSource(dataSource);
        // 设置mapper文件路径
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(BicpDruidConfig.MAPPER_LOCATION));

        return sessionFactory.getObject();
    }
}

5.注意事项:

   mapper包一定要分开,查询时会根据不同的包自动切换数据源

   若整个项目中也有其他子模块配置了相同名称的数据源,则可在yml文件配置

jmx:
  default-domain: bpas

springboot多数据源其中一个连不上能启动么 springboot2多数据源配置_sql_02