导入依赖
<!-- druid连接--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency>
#配置数据源
spring:datasource:driver-class-name: com.mysql.jdbc.Driverusername: "root"password: "2001"url: jdbc:mysql://localhost:3306/test?useSSL=true&useUnicode=true&characterEncoding=utf8type: 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 {@Autowiredprivate DataSource dataSource;@Testvoid 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")@Beanpublic DataSource druidDataSource(){return new DruidDataSource();}//后台监控:web.xml servlet内置的容器它没有web.xml所以只能通过这个去配置@Beanpublic 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
#配置数据源
spring:datasource:driver-class-name: com.mysql.jdbc.Driverusername: "root"password: "2001"#假如时区报错了,就增加一个时区的配置就ok了 serverTimezone=UTCurl: jdbc:mysql://localhost:3306/test?useSSL=true&useUnicode=true&characterEncoding=utf8type: com.alibaba.druid.pool.DruidDataSource#Spring Boot 默认是不注入这些属性的,需要自己绑定#druid 数据源专有配置#初始化大小,数据源连接池大小initial-size: 5#最小连接量min-idle: 5# 最大活跃量max-active: 20#最大等待量max-wait: 6000time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 30000validation-query: SELECT 1 FROM DUALtest-while-idle: truetest-on-borrow: falsetest-on-return: falsepool-prepared-statements: true#配置监控统计拦截的filters.stat:监控统计、log4j:日志记录、wall:防御sql注入#如果运行时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority#则导入 log4j 依赖即可,maven 地址 : http://mvnrepossitory.com/artifact/log4j/log4jfilters: stat,wall,log4jmax-pool-prepared-statement-per-connection-size: 20use-global-data-source-stat: trueconnect-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")@Beanpublic DataSource druidDataSource(){return new DruidDataSource();}//后台监控:web.xml servlet内置的容器它没有web.xml所以只能通过这个去配置//因为Springboot 内置了servlet容器,所以没有web.xml ,替代方法:ServletRegistrationBean@Beanpublic 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@Beanpublic 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;}}