There are several main steps to use the druid connection pool:
1. Add jar and dependencies
<groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.4</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency>
2. Configuration file:
server: port: 1111 spring: application: name: springboothouse druid: druidClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/houseselling?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root password: ***** #Maximum number of connections maxActive: 30 #Minimum number of connections minIdle: 5 #Get the maximum wait time for a connection maxWait: 10000 #Solve the problem of mysql8 hours validationgQuery: SELECT 'X' #Check interval for idle connections timeBetweenEvictionRunsMillis: 60000 #Idle connection minimum idle time minEvictableIdleTimeMillis: 300000
3. Configure connection pool monitoring and slow SQL processing
Connection pool monitoring connection:
http://ip:port/ project name/druid/ or http://ip: port/ project name/druid/ index.html can be accessed
package com.house.sell.config; import com.alibaba.druid.filter.Filter; import com.alibaba.druid.filter.stat.StatFilter; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.google.common.collect.Lists; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; /** *Druid connection pool configuration, configure how to deal with slow SQL, */ @Configuration public class DruidConfig { //This annotation reads the configuration file with the prefix prefix, and binds the external configuration file to this @ConfigurationProperties(prefix = "spring.druid") //Open and close the container @Bean(initMethod = "init",destroyMethod = "close") public DruidDataSource dataSource(){ DruidDataSource dataSource=new DruidDataSource(); dataSource.setProxyFilters(Lists.newArrayList(statFilter())); return dataSource; } //Bean annotation, become spring's bean, use filter to print out the log of slow sql @Bean public Filter statFilter(){ StatFilter statFilter=new StatFilter(); //How long is defined as slow sql, here is defined as 5s statFilter.setSlowSqlMillis(5000); //Whether to print out slow log statFilter.setLogSlowSql(true); // Whether to merge the logs statFilter.setMergeSql(true); return statFilter; } //This is the monitoring of the configuration druid @Bean public ServletRegistrationBean servletRegistrationBean(){ return new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); } }