引入依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
编写配置类:DuridConfig
package com.frost.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.Arrays;
import java.util.HashMap;
/**
* @Auther: frost GG
* @Date: 2019/9/19 09:27
* @Description:
*/
@Configuration
public class DruidConfig {
//配置德鲁依数据库连接池的信息
//把yml文件中的属性注入进来
@ConfigurationProperties(prefix="spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
//1.配置servlet
@Bean
public ServletRegistrationBean statViewServlet(){
//进入网战的地址
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
HashMap<Object, Object> hashMap = new HashMap<>();
//配置连接的信息
//用户名
hashMap.put("loginUsername", "admin");
//密码
hashMap.put("loginPassword", "1234");
//允许访问所有
hashMap.put("allow","");
bean.setInitParameters(hashMap);
return bean;
}
//2.配置Filter
@Bean
public FilterRegistrationBean webStatFilter() {
//配置拦截的信息
FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
HashMap<Object, Object> hashMap = new HashMap<>();
//不拦截那些信息
hashMap.put("exclusions", "*.js,*css,/druid/*");
bean.setInitParameters(hashMap);
//要过滤那些属性
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
配置数据库信息:
# 配置数据库连接信息
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/exam?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
最后的是时候是在项目的端口号后面加上 durid/login.html 然后账号是:admain 密码是:1234
