Springboot-druid connection pool configuration

1. Import external data sources

<! -- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> < artifactId > druid < / artifactId > < version > 1.2.6 < / version > < / dependency >Copy the code

Add type type: com. Alibaba. Druid. Pool. DruidDataSource

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/ssmbuild? useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
Copy the code

Run the tests to verify that Druid is used

2. Configuration Druid

There are currently two ways!

Methods a

Import directly start the application after starter dependence, visit http://localhost:8080/druid to automatically jump to the index interface at http://localhost:8080/druid/index.html

Rely on:

< the dependency > < groupId > com. Alibaba < / groupId > < artifactId > druid - spring - the boot - starter < / artifactId > < version > 1.1.10 < / version >  </dependency>Copy the code

This way, you can write the configuration in your Application configuration file

Tip: if there is no package, application. The properties/application. The yml tip will not appear on the druid

Method 2

If you’re importing the Druid package

Alibaba </groupId> <artifactId>druid</artifactId> <version>1.2.6</version> </dependency>Copy the code

You write configuration classes

Using the @configurationProperties (prefix = “Spring.datasource “) annotation, import the following dependencies

	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
Copy the code

He will prompt to go viral!

It means “Spring Boot configuration annotation executor is not configured” and what are the benefits of configuring annotation executor?

After the annotation executor is configured, when an object and its fields have been defined in the execution class, a prompt message will be displayed when assigning a value to the class in the configuration file.

Create the configuration class DruidConfig

package com.gip.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DruidConfig {
    @Bean
    public DataSource druid(a){
        return new DruidDataSource();
    }
    @Bean
    public ServletRegistrationBean druidServlet(a){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        bean.addInitParameter("allow"."127.0.0.1");
        bean.addInitParameter("loginUsername"."admin");
        bean.addInitParameter("loginPassword"."123456");
        returnbean; }}Copy the code

This method requires us to manually write a servlet and filter, * note :* we must manually inject a DruidDataSource and specify to read the file, otherwise it will not initialize the datasource.

Ok, so now druid’s integration is complete.

3. An error occurs

Run prompt URL not set?

Here’s the weird thing, the individual goes and looks at the configuration file field

Didn’t write wrong! The path

That’s the problem!

The @ConfigurationProperties(prefix = “XXXX “) annotation is incorrectly positioned and should be placed on the corresponding bean, not the class

Remember to import the log4j dependency package

filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionoProperties: druid.stat.mergeSql=true; druid.stat.slowSqlMillis=500Copy the code

Be sure to join the configuration !!!!!!!!

Otherwise, SQL cannot be monitored

So when you open this up when you first run it, it looks like this

At this time you go to send, or go to a request to connect on the line, after the refresh will be displayed

Complete configuration

package com.gip.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;

import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration

public class DruidConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druid(a) {
        return new DruidDataSource();
    }

    @Bean
    public ServletRegistrationBean druidServlet(a) {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        bean.addInitParameter("allow"."");
// bean.addInitParameter("loginUsername", "admin");
// bean.addInitParameter("loginPassword", "123456");
        return bean;
    }

    //filter
    @Bean
    public FilterRegistrationBean filterRegistrationBean(a) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/ *");
        filterRegistrationBean.addInitParameter("exclusions"."*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        returnfilterRegistrationBean; }}Copy the code