SpringBoot整合Druid 1.1.14

Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。

新版的Druid在SpringBoot下加入了新的依赖 druid-spring-boot-starter

druid

https://mvnrepository.com/artifact/com.alibaba/druid/1.1.14

druid-spring-boot-starter

https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter/1.1.14

在pom.xml添加依赖

<properties>
    <druid.version>1.1.14</druid.version>
</properties>

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>${druid.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>${druid.version}</version>
</dependency>

然后配置yml参数

datasource:
      username: root
      password: 123456
      url: jdbc:mysql://192.168.1.58:3306/bootsql?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
      driver-class-name: com.mysql.cj.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      #连接池的信息配置
      druid:
        #初始化大小
        initialSize: 5
        #最小值
        minIdle: 5
        #最大值
        maxActive: 20
        #最大等待时间,配置获取连接等待超时,时间单位都是毫秒ms
        maxWait: 60000
        #配置间隔多久才进行一次检测,检测需要关闭的空闲连接
        timeBetweenEvictionRunsMillis: 60000
        #配置一个连接在池中最小生存的时间
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,
        #'wall'用于防火墙,SpringBoot中没有log4j,我改成了log4j2
        filters: stat,wall,log4j2
        #最大PSCache连接
        maxPoolPreparedStatementPerConnectionSize: 20
        useGlobalDataSourceStat: true
        # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
        # 配置StatFilter
        web-stat-filter:
          #默认为false,设置为true启动
          enabled: true
          url-pattern: "/*"
          exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
        #配置StatViewServlet
        stat-view-servlet:
          url-pattern: "/druid/*"
          #允许那些ip
          allow: 127.0.0.1,192.168.1.101
          login-username: admin
          login-password: 123456
          #禁止那些ip
          deny: 192.168.1.102
          #是否可以重置
          reset-enable: true
          #启用
          enabled: true

猜你喜欢

转载自blog.csdn.net/xue9617049/article/details/88081189