万隆的笔记 万隆的笔记
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
  • Spring

  • SprinvMVC

  • SpringBoot

    • Spring Boot 简介
    • Hello Spring Boot
    • Spring Boot 常用配置
    • YAML 配置文件语言
    • 为什么说 JSON 不适合做配置文件?
    • Spring Boot 整合 Thymeleaf
    • Thymeleaf 常用语法
    • Thymeleaf 表达式语法
    • Thymeleaf 内置对象
    • Spring Boot 整合 Druid
      • 概述
      • Spring Boot 整合 Druid
    • Spring Boot 整合 HikariCP
    • Spring Boot 整合 tk.mybatis & PageHelper
    • MyBatis 生成代码 Maven插件
    • Spring Boot 配置拦截器
  • Spring
  • SpringBoot
2021-08-19
目录

Spring Boot 整合 Druid

# Spring Boot 整合 Druid

# 概述

Druid (opens new window) 是阿里巴巴开源平台上的一个项目,它是一个 JDBC 组件库,包含数据库连接池、SQL Parser (SQL 解析器)等组件,在功能、性能、扩展性方面,都超过其他数据库连接池,包括 DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。Druid 已经在阿里巴巴部署了超过 600 个应用,经过多年生产环境大规模部署的严苛考验。Druid 是阿里巴巴开发的号称为监控而生的数据库连接池!

Druid可以让程序员实现一些特殊的需求,比如向密钥服务请求凭证、统计 SQL 信息、SQL 性能收集、SQL 注入检查、SQL 翻译等,程序员可以通过定制来实现自己需要的功能。

# Spring Boot 整合 Druid

# 引入依赖

SpringBoot版本为2.5.3,在 pom.xml 文件中引入 druid-spring-boot-starter 依赖。

<!-- druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
</dependency>

# 配置 application.yml

在 application.yml 中配置数据库连接

spring:
  datasource:
    druid:
      url: jdbc:mysql://ip:port/dbname?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      password: 123456
      initial-size: 1
      min-idle: 1
      max-active: 20
      test-on-borrow: true
      # MySQL 8.x: com.mysql.cj.jdbc.Driver
      driver-class-name: com.mysql.jdbc.Driver

项目运行的时候看到类似一下log信息,说明配置成功:

2021-08-17 12:25:37.330  INFO 2961 --- [           main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2021-08-17 12:25:37.506  INFO 2961 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
上次更新: 5/30/2023, 12:51:40 AM
Spring Boot 整合 HikariCP

Spring Boot 整合 HikariCP→

最近更新
01
2025
01-15
02
Elasticsearch面试题
07-17
03
Elasticsearch进阶
07-16
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式