Springboot整合Druid

导入依赖 

      
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        
#配置数据源
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: "root"
    password: "2001"
    url: jdbc:mysql://localhost:3306/test?useSSL=true&useUnicode=true&characterEncoding=utf8
    type: com.alibaba.druid.pool.DruidDataSource

测试类 

package com.qf;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@SpringBootTest
class Spring05DataApplicationTests {
    @Autowired
    private DataSource dataSource;
    @Test
    void contextLoads() throws SQLException {
        System.out.println(dataSource.getClass());

        //获取数据源链接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();

    }

}

 druid代理的JDBC对象,永远是JDBC,这个数据源会被加载到IOC容器里面,通过自动装配

写一个配置类

package com.qf.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.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){

        return new DruidDataSource();
    }

    //后台监控:web.xml  servlet内置的容器它没有web.xml所以只能通过这个去配置
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
       //后台需要有人登录,账号密码配置
        HashMap initParameters = new HashMap<>();
          //增加配置
//        initParameters.put("","")
            initParameters.put("loginUsername","admin");
            initParameters.put("loginPassword","123456");

            //允许谁可以访问
        initParameters.put("allow","");
        //禁止谁能访问 initParameters.put("kuangshen","192.168.11.123");

        bean.setInitParameters(initParameters);//设置初始化参数
        return bean;
    }


}

访问http://localhost:8080/druid/login.html

会自动跳到一个登录页面Springboot整合Druid_第1张图片

 yml

#配置数据源
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: "root"
    password: "2001"
    #假如时区报错了,就增加一个时区的配置就ok了 serverTimezone=UTC
    url: jdbc:mysql://localhost:3306/test?useSSL=true&useUnicode=true&characterEncoding=utf8
    type: com.alibaba.druid.pool.DruidDataSource

    #Spring Boot 默认是不注入这些属性的,需要自己绑定
    #druid 数据源专有配置
      #初始化大小,数据源连接池大小
    initial-size: 5
    #最小连接量
    min-idle: 5
    # 最大活跃量
    max-active: 20
    #最大等待量
    max-wait: 6000
    time-between-eviction-runs-millis: 60000
    min-evictable-idle-time-millis: 30000
    validation-query: SELECT 1 FROM DUAL
    test-while-idle: true
    test-on-borrow: false
    test-on-return: false
    pool-prepared-statements: true
    #配置监控统计拦截的filters.stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果运行时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,maven 地址 : http://mvnrepossitory.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    max-pool-prepared-statement-per-connection-size: 20
    use-global-data-source-stat: true
    connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
package com.qf.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;
import java.util.HashMap;

@Configuration
//web.xml
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){

        return new DruidDataSource();
    }

    //后台监控:web.xml  servlet内置的容器它没有web.xml所以只能通过这个去配置
    //因为Springboot 内置了servlet容器,所以没有web.xml ,替代方法:ServletRegistrationBean
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        //访问路径,请求路径
        ServletRegistrationBean bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
       //后台需要有人登录,账号密码配置
        HashMap initParameters = new HashMap<>();
          //增加配置
//        initParameters.put("","")
            initParameters.put("loginUsername","admin");
            initParameters.put("loginPassword","123456");

            //允许谁可以访问
        initParameters.put("allow","");
        //禁止谁能访问 initParameters.put("kuangshen","192.168.11.123");

        bean.setInitParameters(initParameters);//设置初始化参数
        return bean;
    }


    //过滤器Filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        //可以过滤那些请求
        HashMap initParameters = new HashMap<>();
      //这些东西不进行统计
       initParameters.put("exclusions","*.js,*.css,/druid/*");



        bean.setInitParameters(initParameters);
          return bean;
    }




}

你可能感兴趣的:(SpringBoot,spring,boot,java,spring)