springboot druid mysql线程池配置
时间: 2023-08-09 09:07:24 浏览: 116
在Spring Boot中使用Druid连接池进行MySQL的线程池配置,你可以按照以下步骤进行操作:
1. 首先,在你的Spring Boot项目的pom.xml文件中添加Druid和MySQL的依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
2. 在application.properties(或application.yml)文件中配置Druid连接池的参数:
```properties
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_name
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Druid相关配置
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.filters=stat,wall,log4j
```
3. 创建一个配置类,用于配置Druid数据源:
```java
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DruidConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}
}
```
4. 在主启动类上加上`@EnableConfigurationProperties(DruidConfig.class)`注解,以启用Druid配置。
现在,你的Spring Boot项目已经配置好了使用Druid连接池进行MySQL的线程池配置。你可以根据实际需求调整Druid的参数,比如连接池的大小、最大等待时间等。
阅读全文