springboot 集成dbcp连接池
时间: 2023-08-25 11:05:54 浏览: 204
Spring Boot 集成 DBCP 连接池需要进行以下步骤:
1. 添加 DBCP 的依赖
在 pom.xml 文件中添加以下依赖:
```xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
```
2. 添加数据库配置
在 application.properties 或 application.yml 中添加数据库相关配置,如下所示:
```properties
# 数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
# 数据库用户名
spring.datasource.username=root
# 数据库密码
spring.datasource.password=123456
# 连接池参数
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxIdle=20
spring.datasource.maxTotal=50
spring.datasource.maxWaitMillis=60000
spring.datasource.validationQuery=select 1 from dual
spring.datasource.testWhileIdle=true
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.numTestsPerEvictionRun=5
spring.datasource.testOnBorrow=true
spring.datasource.testOnReturn=false
```
3. 配置 DBCP 连接池
在 Spring Boot 应用启动类上添加 `@Configuration` 注解,并配置 DBCP 连接池,如下所示:
```java
@Configuration
public class DbcpConfig {
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.initialSize}")
private int initialSize;
@Value("${spring.datasource.minIdle}")
private int minIdle;
@Value("${spring.datasource.maxIdle}")
private int maxIdle;
@Value("${spring.datasource.maxTotal}")
private int maxTotal;
@Value("${spring.datasource.maxWaitMillis}")
private long maxWaitMillis;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private long timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.numTestsPerEvictionRun}")
private int numTestsPerEvictionRun;
@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setInitialSize(initialSize);
dataSource.setMinIdle(minIdle);
dataSource.setMaxIdle(maxIdle);
dataSource.setMaxTotal(maxTotal);
dataSource.setMaxWaitMillis(maxWaitMillis);
dataSource.setValidationQuery(validationQuery);
dataSource.setTestWhileIdle(testWhileIdle);
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
dataSource.setTestOnBorrow(testOnBorrow);
dataSource.setTestOnReturn(testOnReturn);
return dataSource;
}
}
```
4. 使用 DataSource
在需要使用数据库连接的地方,可以通过 `@Autowired` 注入 DataSource,然后获取 Connection,如下所示:
```java
@Autowired
private DataSource dataSource;
public void executeSql(String sql) throws SQLException {
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement()) {
stmt.execute(sql);
}
}
```
阅读全文