springboot以yml方式配置mysql数据库druid连接池

在配置阿里提供的druid连接池时,有俩种方式,一是通过自定义方式,这需要我们在配置类中注册servlet、filter等组件,比较麻烦,相对方便的是采取官方的starter方式,只需要在pom文件中引入如下依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>

然后再yml文件中进行相关配置就可以了,下面是一些常见配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xxx
    username: root
    password: ******
    driver-class-name: com.mysql.jdbc.Driver

    druid:
      aop-patterns: com.example.demo.*  #监控SpringBean
      filters: stat,wall     # 底层开启功能,stat(sql监控),wall(防火墙)

      stat-view-servlet:   # 配置监控页开启 可在浏览器访问后台监控功能
        enabled: true
        login-username: admin #配置访问后台时的登录用户名和密码
        login-password: admin
        resetEnable: false # 配置重置按钮失效

      web-stat-filter:  # 监控web请求
        enabled: true
        urlPattern: /* #添加索引请求
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' #排除这些请求


      filter:
        stat:    # 对上面filters里面的stat的详细配置
          slow-sql-millis: 1000 #设置慢查询时间
          logSlowSql: true #设置慢查询sql日志打印
          enabled: true
        wall:
          enabled: true
          config:
            drop-table-allow: false

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个快速开发框架,而Druid是一个高性能的数据库连接。在Spring Boot中,可以通过配置来使用Druid连接。 首先,在pom.xml文件中添加Druid的依赖: ``` <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> ``` 接下来,在application.properties或application.yml配置文件中添加Druid连接的相关配置信息: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 spring.datasource.maxWait=60000 spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.filters=stat,wall,slf4j ``` 其中,关键的配置项包括: - `spring.datasource.url`:数据库连接URL。 - `spring.datasource.username`:数据库用户名。 - `spring.datasource.password`:数据库密码。 - `spring.datasource.driver-class-name`:数据库驱动类名。 - `spring.datasource.type`:连接类型,这里配置为`com.alibaba.druid.pool.DruidDataSource`。 - `spring.datasource.initialSize`:连接的初始化大小。 - `spring.datasource.minIdle`:空闲连接的最小数量。 - `spring.datasource.maxActive`:最大活跃连接数。 - `spring.datasource.maxWait`:获取连接的最大等待时间。 - `spring.datasource.timeBetweenEvictionRunsMillis`:定期检查连接中空闲连接的间隔时间。 - `spring.datasource.minEvictableIdleTimeMillis`:连接中最小空闲时间,超过该时间则将被销毁。 - `spring.datasource.validationQuery`:连接校验SQL。 - `spring.datasource.testWhileIdle`:连接空闲时是否进行校验。 - `spring.datasource.testOnBorrow`:从连接获取连接时是否进行校验。 - `spring.datasource.testOnReturn`:归还连接连接时是否进行校验。 - `spring.datasource.poolPreparedStatements`:是否缓存PreparedStatement。 - `spring.datasource.maxPoolPreparedStatementPerConnectionSize`:缓存PreparedStatement的最大数量。 以上就是Spring Boot中使用Druid连接的详细配置解析,通过适当调整这些配置项,可以实现对连接的更好性能和管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值