springboot使用druid连接池
时间: 2023-06-05 20:47:10 浏览: 240
SpringBoot 使用 Druid 连接池需要以下步骤:
1. 引入 druid 依赖:
```
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
```
2. 配置 Druid 连接池参数,可在 application.properties 文件中进行配置。
3. 在启动类中添加注解 `@ServletComponentScan` 开启 Druid 监控。
4. 在数据源配置类中添加注解 `@Configuration` 和 `@EnableTransactionManagement`。
5. 创建 DruidDataSource 实例并进行配置。
更详细的使用方法可以参考 Druid 官方文档和相关教程。
相关问题
springboot配置druid连接池
Spring Boot 配置 Druid 连接池的步骤如下:
1. 在 pom.xml 中添加 Druid 的依赖:
```
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
```
2. 在 application.properties 或 application.yml 中配置 Druid 的相关参数,例如:
```
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
# 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.validation-query=SELECT 1
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
```
3. 在启动类上添加 @ServletComponentScan 注解:
```
@ServletComponentScan
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
4. 在启动类中添加 Druid 的 Servlet 配置:
```
@Bean
public ServletRegistrationBean druidServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String, String> initParams = new HashMap<>();
initParams.put("loginUsername", "admin");
initParams.put("loginPassword", "admin");
servletRegistrationBean.setInitParameters(initParams);
return servletRegistrationBean;
}
```
以上是在 Spring Boot 中配置 Druid 连接池的简单步骤
springboot整合druid连接池
Spring Boot整合Druid连接池的步骤如下:
1. 在pom.xml文件中添加Druid和JDBC依赖:
```
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
```
2. 在application.properties文件中配置Druid连接池:
```
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Druid配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.test-on-return=false
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.filters=stat,wall,log4j
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
```
3. 在启动类上添加注解@EnableTransactionManagement和@Bean注解创建DruidDataSource:
```
@SpringBootApplication
@EnableTransactionManagement
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.druid")
public DataSource druidDataSource() {
return new DruidDataSource();
}
}
```
4. 在需要使用数据库的地方注入JdbcTemplate:
```
@Autowired
private JdbcTemplate jdbcTemplate;
```
以上就是Spring Boot整合Druid连接池的步骤。
阅读全文