登录    关于
马育民老师的博客

马育民的博客

QQ:65242847

springboot使用druid连mysql5.7(mysql5驱动)

概述

本文介绍springboot使用druid连接池,连接mysql数据库

springboot2默认的数据库连接池是:hikariCP,特点是快。

添加 jdbc 依赖

spring-boot-starter-jdbc依赖是连接数据库的基础依赖,不添加会报错

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

Druid Spring Boot Starter依赖

找到依赖

https://mvnrepository.com/ 搜索 Druid Spring Boot Starter,找到适合的版本,本文用的是: https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter/1.1.23

如下:

添加依赖

复制上图红框中的依赖到pom.xml文件

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

注意

使用 druid springboot starter 启动器,优点:简化配置。也会自动进行配置,即:启动时连接数据库,所以必须 配置数据库连接,否则报错!

mysql驱动

https://mvnrepository.com/ 搜索druid,找到适合的版本,复制依赖到pom.xml文件:

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>

配置

最少配置

application.yml:
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/数据库?useSSL=false&characterEncoding=utf8
    username: 用户名
    password: 密码
application.properties:
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库?useSSL=false&characterEncoding=utf8

#spring.datasource.url=jdbc:mysql://localhost:3308/lvyou?useUnicode=true&characterEncoding=utf8

#spring.datasource.url=jdbc:mysql://localhost:3308/lvyou?useUnicode=true&characterEncoding=utf8

spring.datasource.username=用户名
spring.datasource.password=密码

基本配置(配置连接池参数)

application.yml:
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://106.13.165.100:3306/数据库?useSSL=false&characterEncoding=utf8 
    username: 用户名
    password: 密码


    druid:
      # 连接池的配置信息
      # 初始化大小,最小,最大
      initial-size: 5
      min-idle: 5
      maxActive: 20
      # 配置获取连接等待超时的时间
      maxWait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      timeBetweenEvictionRunsMillis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall,slf4j
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
application.properties:
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库?useSSL=false&characterEncoding=utf8
spring.datasource.username=用户名
spring.datasource.password=密码


spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.druid.filters=stat,wall,slf4j
spring.datasource.druid.connectionProperties=druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000

原文出处:https://malaoshi.top/show_1IX15nklBuyx.html