欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

druid 整合springBoot2.x

程序员文章站 2022-07-07 11:34:10
...

0.druid 相关文档: https://github.com/alibaba/druid

1.引入jar

<!--mysql start -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

2.配置application.yml 配置参考:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

# Tomcat
server:
  tomcat:
    uri-encoding: UTF-8
    max-threads: 1000
    min-spare-threads: 30
  port: 8802
  servlet:
    context-path: /
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.jdbc.Driver
    druid:
      ##################################### 本地数据库
      url:  jdbc:mysql://localhost:3306/test?allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8
      username: root
      password: root
      initial-size: 5
      max-active: 10
      min-idle: 5
      max-wait: 60000
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 10
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        login-username: admin
        login-password: admin
      filter:
        stat:
          log-slow-sql: true
          slow-sql-millis: 1000
          merge-sql: true
        wall:
          config:
            multi-statement-allow: true
        slf4j:
          enabled: true
      keep-alive: true
# Druid 开启     spring 监控          
spring.datasource.druid.aop-patterns: com.abbottliu.sys.dao.*,com.abbottliu.sys.service.*,com.abbottliu.dao.*,com.abbottliu.service.*
spring.aop.proxy-target-class: true

3.创建数据源

@Bean
@Primary
@ConfigurationProperties("spring.datasource.druid")
public DruidDataSource druidDataSource(){
    return DruidDataSourceBuilder.create().build();
}