brief introduction
- Druid is a database connection pool.
- Druid is currently the best database connection pool.
- Druid is a database connection pool developed by Alibaba called monitoring.
springBoot rapid integration druid
I. introducing druid dependency into pom files
<!--Alibaba database connection pool --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
II. Basic configuration
- application.properties
#Database connection information spring.datasource.druid.url= jdbc:mysql://127.0.0.1:3306/springbootdemo?characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8 spring.datasource.druid.username= root spring.datasource.druid.password= 111111 spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver # Number of physical connections established during initialization spring.datasource.druid.initial-size=5 # Maximum number of connection pools spring.datasource.druid.max-active=30 # Minimum number of connection pools spring.datasource.druid.min-idle=5 # Maximum waiting time when getting the connection, in milliseconds spring.datasource.druid.max-wait=60000 # Configure how often to check the interval. Check the idle connections that need to be closed, in milliseconds spring.datasource.druid.time-between-eviction-runs-millis=60000 # The minimum time for the connection to remain idle without being evicted spring.datasource.druid.min-evictable-idle-time-millis=300000 # The sql used to check whether the connection is valid requires a query statement spring.datasource.druid.validation-query=SELECT 1 FROM DUAL # The recommended configuration is true, which does not affect performance and ensures security. Check when applying for connection. If the idle time is greater than timebetweenevicitionrunsmillis, execute validationQuery to check whether the connection is valid. spring.datasource.druid.test-while-idle=true # When applying for a connection, a validation query is executed to check whether the connection is valid. If this configuration is made, the performance will be reduced. spring.datasource.druid.test-on-borrow=false # When returning a connection, a validationQuery is performed to check whether the connection is valid. This configuration will reduce performance. spring.datasource.druid.test-on-return=false # Whether to cache preparedStatement, that is, PSCache. PSCache can greatly improve the performance of databases supporting cursors, such as oracle. It is recommended to close it under mysql. spring.datasource.druid.pool-prepared-statements=false # To enable PSCache, it must be configured to be greater than 0. When it is greater than 0, poolPreparedStatements will automatically trigger modification to true. spring.datasource.druid.max-pool-prepared-statement-per-connection-size=50 # Configure the filters intercepted by monitoring statistics. After removal, the monitoring interface sql cannot be counted spring.datasource.druid.filters=stat,wall # Open mergeSql function through connectProperties property; slow SQL record spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 # Merge monitoring data from multiple druiddatasources spring.datasource.druid.use-global-data-source-stat=true
- New DruidConfig configuration class
@Configuration public class DruidConfig { /*Register the data source of the druid with the above parameters to the IOC container*/ @ConfigurationProperties(prefix = "spring.datasource.druid") @Bean public DataSource druid() { DruidDataSource druidDataSource = new DruidDataSource(); return druidDataSource; } /*Configure a servlet to manage the background*/ @Bean public ServletRegistrationBean statViewServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); Map<String, String> map = new HashMap<String, String>(); //User name map.put("loginUsername", "zx"); //Password map.put("loginPassword", "123456"); //IP White list (if not configured or empty, all accesses are allowed) map.put("allow", ""); //IP Blacklist (deny takes precedence over allow when there is common) map.put("deny", ""); servletRegistrationBean.setInitParameters(map); return servletRegistrationBean; } /*Configure a filter for web Monitoring*/ @Bean public FilterRegistrationBean webStatFilter() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); Map<String, String> map = new HashMap<>(); map.put("exclusions", "*.js,*.css,/druid/*"); filterRegistrationBean.setInitParameters(map); filterRegistrationBean.setUrlPatterns(Arrays.asList("/*")); return filterRegistrationBean; } }
III. verify the viewing effect
Login address: http://localhost:8080/druid/login.html
If the above effect appears, the configuration is successful!
If this happens, run the project debugging interface to access the database, and then refresh it. If it is still the case, it may be that the injection configuration prefix does not match @ ConfigurationProperties(prefix = "spring.datasource.druid"). For example, write as spring.datasource
Note:
#First kind spring.datasource.druid.url= jdbc:mysql://127.0.0.1:3306/springbootdemo?characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8 spring.datasource.druid.username= root spring.datasource.druid.password= 111111 spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver #It can also be written as the second one spring.datasource.url= jdbc:mysql://127.0.0.1:3306/springbootdemo?characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8 spring.datasource.username= root spring.datasource.password= 111111 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Both of them can be used. If the second one is used, druid() in DruidConfig must be removed (the first one can be removed but not the first one). springBoot will automatically configure the data source, otherwise an error will be reported. Because the prefix is spring.datasource.druid.