集成Druid连接池
spring boot默认是使用tomcat jdbc连接池
application.properties注释掉spring.datasource配置
引入依赖
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.8</version> </dependency>
配置datasource类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.xiaoping.house.config; 2 3 import com.alibaba.druid.filter.Filter; 4 import com.alibaba.druid.filter.stat.StatFilter; 5 import com.alibaba.druid.pool.DruidDataSource; 6 import com.alibaba.druid.support.http.StatViewServlet; 7 import com.google.common.collect.Lists; 8 import org.springframework.boot.context.properties.ConfigurationProperties; 9 import org.springframework.boot.web.servlet.ServletRegistrationBean; 10 import org.springframework.context.annotation.Bean; 11 import org.springframework.context.annotation.Configuration; 12 13 @Configuration 14 public class DruidConfig { 15 //绑定application.properties的spring.druid的属性为datasource 16 @ConfigurationProperties(prefix = "spring.druid") 17 @Bean(initMethod = "init",destroyMethod = "close") 18 public DruidDataSource dataSource(){ 19 DruidDataSource druidDataSource = new DruidDataSource(); 20 druidDataSource.setProxyFilters(Lists.newArrayList(statFilter())); 21 return druidDataSource; 22 } 23 //慢日志输出 24 @Bean 25 public Filter statFilter(){ 26 StatFilter statFilter = new StatFilter(); 27 statFilter.setSlowSqlMillis(1); 28 statFilter.setLogSlowSql(true); 29 statFilter.setMergeSql(true); 30 return statFilter; 31 } 32 //监控:localhost:8080/druid 33 @Bean 34 public ServletRegistrationBean servletRegistrationBean(){ 35 return new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); 36 } 37 }
application.properties配置相关属性值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 spring.druid.driverClassName=com.mysql.jdbc.Driver 2 spring.druid.url=jdbc:mysql://localhost:33062/test?characterEncoding=UTF-8 3 spring.druid.username=root 4 spring.druid.password=766774 5 spring.druid.maxActive=30 6 spring.druid.minIdle=5 7 spring.druid.maxWait=10000 8 #解决Mysql8小时的问题 9 spring.druid.validationQuery=SELECT 'x' 10 spring.druid.minEvictableIdleTimeMillis=300000 11 spring.druid.timeBetweenEvictionRunsMillis=60000
监控可以通过访问:http://localhost:8080/druid/