SpringBoot集成Druid连接池,配置多数据源

1、配置application.yml文件

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      # 数据库链接 [主数据源]
      main:
        url: jdbc:oracle:thin:@主机:端口号:ORCL
        username:
        password: 
        driverClassName: oracle.jdbc.driver.OracleDriver
        # 初始化连接个数
        initialSize: 10
        # 最小连接池数量
        minIdle: 10
        # 最大连接池数量
        maxActive: 50
        # 获取连接时最大等待时间,单位毫秒
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        # 连接保持空闲而不被驱逐的最小时间
        minEvictableIdleTimeMillis: 300000
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20
        maxOpenPreparedStatements: 20
        validationQuery: SELECT 'x' FROM DUAL
        testWhileIdle: true
        testOnBorrow: true
        testOnReturn: true
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
        # 开启监控统计信息
        filters: stat,wall
        useGlobalDataSourceStat: true
        keep-alive: true
      # 数据库链接 [从数据源]
      slave:
        url: jdbc:oracle:thin:@主机:端口号:ORCL
        username:
        password: 
        driverClassName: oracle.jdbc.driver.OracleDriver
        # 初始化连接个数
        initialSize: 10
        # 最小连接池数量
        minIdle: 10
        # 最大连接池数量
        maxActive: 50
        # 获取连接时最大等待时间,单位毫秒
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        # 连接保持空闲而不被驱逐的最小时间
        minEvictableIdleTimeMillis: 300000
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20
        maxOpenPreparedStatements: 20
        validationQuery: SELECT 'x' FROM DUAL
        testWhileIdle: true
        testOnBorrow: true
        testOnReturn: true
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
        # 开启监控统计信息
        filters: stat,wall
        useGlobalDataSourceStat: true
        keep-alive: true

2、配置DatasourceConfig类

@Configuration
public class DatasourceConfig {
    @Bean(initMethod = "init", destroyMethod = "close")
    @ConfigurationProperties(prefix = "spring.datasource.druid.slave")
    public DataSource spcDataSource(DynamicDataSource dynamicDataSource) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        Map targetDataSources = dynamicDataSource.getTargetDataSources();
        //50代表:数据源标志
        targetDataSources.put(50,dataSource);
        dynamicDataSource.setTargetDataSources(targetDataSources);
        return dataSource;
    }
}

3、实现类上方配置注解

//数据源标识
@MyDataSource(50)
@Service
public class TestServiceImpl extends ServiceImpl  implements TestService {


}

4、从库数据操作

    
//数据源标识
@MyDataSource(50)
@Service
public class TestServiceImpl extends ServiceImpl  implements TestService {
    @Autowired
    private TestMapper mapper;

    @Override
    public List getStudentById(String id) {
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        List list = this.mapper.getStudentById(id);
        return list;
    }

    //保存数据到从数据库时,一定要配置@Transactional
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public void saveStudentEntity(StudentEntity studentEntity) {
        this.save(StudentEntity);
    }
}

你可能感兴趣的:(java,mybatis,spring)