1.Maven加载jar包
<!--阿里巴巴durid https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
2.在application.properties配置文件中写相关配置信息
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/thirdteam?useSSL=false&characterEncoding=UTF-8&?useUnicode=true
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.filter=stat,wall,log4j
spring.datasource.tomcat.initial-size=1
spring.datasource.maxWait = 60000
spring.datasource.minIdle = 1
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.minEvictableIdleTimeMillis = 300000
spring.datasource.validationQuery = select 'x'
spring.datasource.testWhileIdle = true
spring.datasource.testOnBorrow = false
spring.datasource.testOnReturn = false
spring.datasource.poolPreparedStatements = true
spring.datasource.maxOpenPreparedStatements = 20
3.添加Druid配置
package com.song.util.Durid;
import com.alibaba.druid.support.http.StatViewServlet;
import org.apache.catalina.filters.WebdavFixFilter;
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.servlet.ServletRegistration;
@Configuration
public class DuridConfiguration {
@Bean//声明bean实例
public ServletRegistrationBean stateViewServlet(){
//创建servlet注册实体
ServletRegistrationBean servletRegistrationBean =new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
//设置ip白名单
servletRegistrationBean.addInitParameter("allow","127.0.0.1");
//设置ip黑名单,如果allow与deny共同存在时,deny优于allow
servletRegistrationBean.addInitParameter("denny","192.168.0.100");
//设置druid登录入口,1267.0.0.1/druid/login.html
servletRegistrationBean.addInitParameter("loginUsername","druid");
servletRegistrationBean.addInitParameter("loginPassword","123456");
servletRegistrationBean.addInitParameter("resetEnable","false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean statFilter(){
//创建过滤器
FilterRegistrationBean filterRegistrationBean =new FilterRegistrationBean(new WebdavFixFilter());
//设置过滤路径
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,/druid/*");
return filterRegistrationBean;
}
}
4、接着登录http://localhost:8081/druid/index.html就可以监控数据库了。