Springboot整合Druid

导入依赖

XML 复制代码
      <!--        druid连接-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
XML 复制代码
#配置数据源
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

测试类

java 复制代码
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容器里面,通过自动装配

写一个配置类

java 复制代码
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<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
       //后台需要有人登录,账号密码配置
        HashMap<String, String> 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

会自动跳到一个登录页面

yml

XML 复制代码
#配置数据源
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
java 复制代码
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<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
       //后台需要有人登录,账号密码配置
        HashMap<String, String> 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<String, String> initParameters = new HashMap<>();
      //这些东西不进行统计
       initParameters.put("exclusions","*.js,*.css,/druid/*");



        bean.setInitParameters(initParameters);
          return bean;
    }




}
相关推荐
HBryce242 分钟前
缓存-基础概念
java·缓存
一只爱打拳的程序猿17 分钟前
【Spring】更加简单的将对象存入Spring中并使用
java·后端·spring
杨荧18 分钟前
【JAVA毕业设计】基于Vue和SpringBoot的服装商城系统学科竞赛管理系统
java·开发语言·vue.js·spring boot·spring cloud·java-ee·kafka
minDuck20 分钟前
ruoyi-vue集成tianai-captcha验证码
java·前端·vue.js
为将者,自当识天晓地。39 分钟前
c++多线程
java·开发语言
daqinzl1 小时前
java获取机器ip、mac
java·mac·ip
激流丶1 小时前
【Kafka 实战】如何解决Kafka Topic数量过多带来的性能问题?
java·大数据·kafka·topic
Themberfue1 小时前
Java多线程详解⑤(全程干货!!!)线程安全问题 || 锁 || synchronized
java·开发语言·线程·多线程·synchronized·
让学习成为一种生活方式1 小时前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
晨曦_子画1 小时前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin