springBoot rapid integration Druid

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 -->

II. Basic configuration

  • application.properties
#Database connection information
spring.datasource.druid.url= jdbc:mysql://
spring.datasource.druid.username= root
spring.datasource.druid.password= 111111

# Number of physical connections established during initialization
# Maximum number of connection pools
# Minimum number of connection pools
# Maximum waiting time when getting the connection, in milliseconds
# Configure how often to check the interval. Check the idle connections that need to be closed, in milliseconds
# The minimum time for the connection to remain idle without being evicted
# 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.
# 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.
# When returning a connection, a validationQuery is performed to check whether the connection is valid. This configuration will reduce performance.
# 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.
# 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.
# Configure the filters intercepted by monitoring statistics. After removal, the monitoring interface sql cannot be counted
# Open mergeSql function through connectProperties property; slow SQL record
# Merge monitoring data from multiple druiddatasources
  • New DruidConfig configuration class
public class DruidConfig {

	/*Register the data source of the druid with the above parameters to the IOC container*/

	@ConfigurationProperties(prefix = "spring.datasource.druid")
	public DataSource druid() {
		DruidDataSource druidDataSource = new DruidDataSource();
		return druidDataSource;

	/*Configure a servlet to manage the background*/
	public ServletRegistrationBean statViewServlet() {
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
		Map<String, String> map = new HashMap<String, String>();
		//User name
		map.put("loginUsername", "zx");
		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", "");
		return servletRegistrationBean;

	/*Configure a filter for web Monitoring*/
	public FilterRegistrationBean webStatFilter() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
		Map<String, String> map = new HashMap<>();
		map.put("exclusions", "*.js,*.css,/druid/*");
		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


#First kind
spring.datasource.druid.url= jdbc:mysql://
spring.datasource.druid.username= root
spring.datasource.druid.password= 111111
#It can also be written as the second one
spring.datasource.url= jdbc:mysql://
spring.datasource.username= root
spring.datasource.password= 111111

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.

Tags: Druid Spring MySQL Database

Posted on Mon, 04 Nov 2019 17:04:18 -0500 by Jude