springboot整合druid的完整流程记录
更新时间:2026年02月10日 09:02:43 作者:改了一个昵称
Java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池,Druid是阿里巴巴开源平台上一个数据库连接池实现,结合了DBCP等DB池的优点,同时加入了日志监控,这篇文章主要介绍了springboot整合druid的相关资料,需要的朋友可以参考下

pom.xml
druid版本问题
druid 的依赖版本,尽量选择 1.2.20 及 以上,不然会报错
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-3-starter</artifactId> <version>1.2.20</version> // 不要用 1.2.18 哦,会报错 <version>1.2.18</version> </dependency>
通过源码分析,druid-spring-boot-3-starter 的 1.2.18 版本,
虽然,适配了 SpringBoot3,
但是,缺少自动装配的配置文件,
需要手动在 resources目录下创建
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports,
文件内容如下:
com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure

项目的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
</parent>
<groupId>com.atguigu</groupId>
<artifactId>boot-druid</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- web开发的场景启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 数据库相关配置启动器 jdbctemplate 事务相关-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- druid启动器的依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>1.2.20</version>
</dependency>
<!-- 驱动类-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
</dependencies>
</project>
application.yml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 使用druid连接池
druid:
url: jdbc:mysql:///mybatis-example
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
# 初始化时建立物理连接的个数
initial-size: 5
# 连接池的最小空闲数量
min-idle: 5
# 连接池最大连接数量
max-active: 20
# 获取连接时最大等待时间,单位毫秒
max-wait: 60000
# 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
test-while-idle: true
# 既作为检测的间隔时间又作为testWhileIdel执行的依据
time-between-eviction-runs-millis: 60000
# 销毁线程时检测当前连接的最后活动时间和当前时间差大于该值时,关闭当前连接(配置连接在池中的最小生存时间)
min-evictable-idle-time-millis: 30000
# 用来检测数据库连接是否有效的sql 必须是一个查询语句(oracle中为 select 1 from dual)
validation-query: select 1
# 申请连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
test-on-borrow: false
# 归还连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为true
test-on-return: false
# 是否缓存preparedStatement, 也就是PSCache,PSCache对支持游标的数据库性能提升巨大,比如说oracle,在mysql下建议关闭。
pool-prepared-statements: false
# 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100
max-pool-prepared-statement-per-connection-size: -1
# 合并多个DruidDataSource的监控数据
use-global-data-source-stat: true
实体类 User
package com.atguigu.pojo;
import lombok.Data;
@Data
public class User {
private int empId;
private String empName;
private double empSalary;
}
实体类 User 对应的 controller
package com.atguigu.controller;
import com.atguigu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping("findAll")
public List<User> findAll() {
String sql = "select * from t_emp";
List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
return list;
}
}
spboot 的启动程序
package com.atguigu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}


总结
到此这篇关于springboot整合druid的文章就介绍到这了,更多相关springboot整合druid内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringCloudAlibaba Nacos开启鉴权解决跳过登录页面问题
对于Nacos,如果需要开启权限控制,可以在 Nacos 控制台上进行配置,本文主要介绍了SpringCloudAlibaba Nacos开启鉴权解决跳过登录页面问题,感兴趣的可以了解一下2023-10-10
Java实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等
这篇文章主要介绍了Java如何实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序 、快速排序、归并排序、堆排序和LST基数排序,需要的朋友可以参考下2015-07-07


最新评论