Durid与Springboot搭建

一、首先要导入pom.xml

         
            com.alibaba
            druid-spring-boot-starter
            1.1.9
        

二、更改配置application.yml

#数据配置
    datasource:
        name: mysql
        type: com.alibaba.druid.pool.DruidDataSource
        #druid相关配置
        druid:
          #监控统计拦截的filters
          filters: stat,wall,logging
          # logging 是打印日志的配置名
          driver-class-name: com.mysql.jdbc.Driver
          #基本属性
          url: jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
          #用户名
          username: root
          #密码
          password: root
          #配置初始化大小/最小/最大
          initial-size: 1
          min-idle: 1
          max-active: 20
          #获取连接等待超时时间
          max-wait: 60000
          #间隔多久进行一次检测,检测需要关闭的空闲连接
          time-between-eviction-runs-millis: 60000
          #一个连接在池中最小生存的时间
          min-evictable-idle-time-millis: 300000
          validation-query: SELECT 'x'
          test-while-idle: true
          test-on-borrow: false
          test-on-return: false
          #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
          pool-prepared-statements: false
          max-pool-prepared-statement-per-connection-size: 20
          # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 
          #filters: stat,wall,log4j
          #通过connectProperties属性来打开mergeSql功能;慢SQL记录
          #connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
          #合并多个DruidDataSource的监控数据
          #useGlobalDataSourceStat: true

三、编写代码



import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import com.alibaba.druid.support.http.WebStatFilter;

//druid过滤器. 
@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*",
 initParams = {
     // 忽略资源
     @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")
 }
)
public class DruidStatFilter extends WebStatFilter {
}


import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import com.alibaba.druid.support.http.StatViewServlet;


//druid数据源状态监控.
@SuppressWarnings("serial")
@WebServlet(urlPatterns = "/druid/*",
  initParams = {
      // IP白名单 (没有配置或者为空,则允许所有访问)
      @WebInitParam(name = "allow", value = "127.0.0.1"),
      // IP黑名单 (存在共同时,deny优先于allow)
      @WebInitParam(name = "deny", value = ""),
      // 用户名
      @WebInitParam(name = "loginUsername", value = "admin"),
      // 密码
      @WebInitParam(name = "loginPassword", value = "admin"),
      // 禁用HTML页面上的“Reset All”功能
      @WebInitParam(name = "resetEnable", value = "false")
  }
)
public class DruidStatViewServlet extends StatViewServlet {

}

四、访问路径127.0.0.1/druid/index.html

Durid与Springboot搭建_第1张图片

你可能感兴趣的:(JAVA开发,DB)