pom文件信息:
<!--引入druid数据源--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.18</version> </dependency>
application.yml文件:
####*****************### ### by咕咕咕 ### ### 数据库配置信息 ### ####***************### # -- -- -- -- -- -- # 配置数据库连接信息 # -- -- -- -- -- -- spring: datasource: username: root password: 123456 url: jdbc:mysql://192.168.31.243:3306/jdbc-data driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # 设置数据源 # -- -- -- -- -- -- # 使用Druid数据源 # -- -- -- -- -- -- # 配置Druid的其他参数,以下配置必须增加一个配置文件才能有效 # 初始化大小,最小,最大 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 # 打开PSCache,并且指定每个连接上PSCache的大小 maxPoolPreparedStatementPerConnectionSize: 20 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 # 合并多个DruidDataSource的监控数据 useGlobalDataSourceStat: true
新建config包:
创建DruidConfig.java
package com.zdgd.springbootmybatis.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.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; 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; import java.util.Map; /** * description: DruidConfig <br> * date: 2019/7/10 20:28 <br> * author: by咕咕咕 <br> * version: 1.0 <br> * 使用Druid数据源 */ @Configuration public class DruidConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druid() { return new DruidDataSource(); } //1、配置一个管理后台的Servlet @Bean public ServletRegistrationBean statViewServlet() { //这里做好不要设置为默认的路径!以免被入侵! ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/project/druid/*"); System.out.println(bean); Map<String, String> initParams = new HashMap<>(); initParams.put("loginUsername", "admin"); initParams.put("loginPassword", "bin-gu"); initParams.put("allow", "");//默认就是允许所有访问 initParams.put("deny", "192.168.5.60"); bean.setInitParameters(initParams); return bean; } //2、配置一个web监控的filter @Bean public FilterRegistrationBean webStatFilter() { FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); Map<String, String> initParams = new HashMap<>(); initParams.put("exclusions", "*.js,*.css,/druid/*"); bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }
druid去除广告:
参考:https://blog.csdn.net/weixin_42634808/article/details/86563048