标签:mysq 连接 数据库 idc 检测 时间 环境 bean pattern
1 <!-- 阿里系的Druid依赖包 --> 2 <dependency> 3 <groupId>com.alibaba</groupId> 4 <artifactId>druid-spring-boot-starter</artifactId> 5 <version>1.1.21</version> 6 </dependency> 7 8 9 <!-- Druid 依赖 log4j包 --> 10 <dependency> 11 <groupId>org.slf4j</groupId> 12 <artifactId>slf4j-log4j12</artifactId> 13 </dependency>
2.配置application.yml
1 spring: 2 datasource: 3 username: root 4 password: root 5 url: jdbc:mysql://localhost:3306/security_authority?characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true 6 driver-class-name: com.mysql.jdbc.Driver 7 type: com.alibaba.druid.pool.DruidDataSource 8 # 下面为连接池的补充设置,应用到上面所有数据源中 9 # 初始化大小,最小,最大 10 druid.initial-size: 5 11 druid.min-idle: 5 12 druid.max-active: 20 13 # 配置获取连接等待超时的时间 14 druid.max-wait: 60000 15 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 16 druid.time-between-eviction-runs-millis: 60000 17 # 配置一个连接在池中最小生存的时间,单位是毫秒 18 druid.min-evictable-idle-time-millis: 300000 19 druid.validation-query: SELECT 1 FROM DUAL 20 druid.test-while-idle: true 21 druid.test-on-borrow: false 22 druid.test-on-return: false 23 # 打开PSCache,并且指定每个连接上PSCache的大小 24 druid.pool-prepared-statements: true 25 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,‘wall‘用于防火墙 26 druid.max-pool-prepared-statement-per-connection-size: 20 27 druid.filters: stat,wall 28 druid.use-global-data-source-stat: true 29 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 30 druid.connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
3.建立配置类
1 /** 2 * @author jiz-a 3 * @version 1.0 4 * @date 2020/5/28 16:10 5 */ 6 @Configuration 7 public class DruidConfig { 8 /** 9 * 把配置文件中自己配置的数据源值注入到druid 10 * 11 * @return 数据库资源 12 */ 13 @Bean 14 @ConfigurationProperties(prefix = "spring.datasource") 15 public DataSource druid() { 16 return new DruidDataSource(); 17 } 18 19 /** 20 * 配置Druid的监控,如果不配置这个类,将连接不上druid后台。http://localhost:8080/druid/ 21 * 配置一个管理后台的Servlet 22 * 23 * @return Servlet 24 */ 25 @Bean 26 public ServletRegistrationBean<StatViewServlet> statViewServlet() { 27 ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); 28 Map<String, String> params = new HashMap<>(4); 29 //设置后台登录名、密码 30 params.put("loginUsername", "admin"); 31 params.put("loginPassword", "666"); 32 //设置默认就是允许所有访问、设置黑名单 33 params.put("allow", ""); 34 // params.put("deny", ""); 35 bean.setInitParameters(params); 36 return bean; 37 } 38 39 /** 40 * 配置一个web监控的filter,如果不配置这个类,在页面的Web模块功能不会开启 41 * 42 * @return 过滤器 43 */ 44 @Bean 45 public FilterRegistrationBean<WebStatFilter> webStatFilter() { 46 FilterRegistrationBean<WebStatFilter> frb = new FilterRegistrationBean<>(); 47 frb.setFilter(new WebStatFilter()); 48 Map<String, String> params = new HashMap<>(); 49 //设置不拦截请求 50 params.put("exclusions", "*.js,*.css,/druid/*"); 51 frb.setInitParameters(params); 52 //拦截请求 53 frb.setUrlPatterns(Arrays.asList("/*")); 54 return frb; 55 } 56 }
4.启动,测试;
http://localhost:8090/druid/login.html
至此SpringBoot集成了Druid。
SpringBoot随笔-SpringBoot集成Druid
标签:mysq 连接 数据库 idc 检测 时间 环境 bean pattern
原文地址:https://www.cnblogs.com/jiz-eddy/p/12985389.html