news 2026/4/23 11:32:38

springboot 整合 druid

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
springboot 整合 druid

文章目录

    • 目录
    • pom.xml
      • druid版本问题
      • 项目的依赖
    • application.yml
    • 实体类 User
    • 实体类 User 对应的 controller
    • spboot 的启动程序

目录

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"?><projectxmlns="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-exampleusername:rootpassword:rootdriver-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检测连接是否有效,开启会降低性能,默认为truetest-on-borrow:false# 归还连接时会执行validationQuery检测连接是否有效,开启会降低性能,默认为truetest-on-return:false# 是否缓存preparedStatement, 也就是PSCache,PSCache对支持游标的数据库性能提升巨大,比如说oracle,在mysql下建议关闭。pool-prepared-statements:false# 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100max-pool-prepared-statement-per-connection-size:-1# 合并多个DruidDataSource的监控数据use-global-data-source-stat:true

实体类 User

packagecom.atguigu.pojo;importlombok.Data;@DatapublicclassUser{privateintempId;privateStringempName;privatedoubleempSalary;}

实体类 User 对应的 controller

packagecom.atguigu.controller;importcom.atguigu.pojo.User;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jdbc.core.BeanPropertyRowMapper;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjava.util.List;@RestController@RequestMapping("user")publicclassUserController{@AutowiredprivateJdbcTemplatejdbcTemplate;@GetMapping("findAll")publicList<User>findAll(){Stringsql="select * from t_emp";List<User>list=jdbcTemplate.query(sql,newBeanPropertyRowMapper<>(User.class));returnlist;}}

spboot 的启动程序

packagecom.atguigu;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassMain{publicstaticvoidmain(String[]args){SpringApplication.run(Main.class,args);}}

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/21 12:32:21

收藏!6大MCP工具让你秒懂AI大模型,小白也能轻松上手!

在 AI 开发进入新阶段的 2025 年&#xff0c;MCP&#xff08;模型上下文协议&#xff0c;Model Context Protocol&#xff09; 正在成为行业新标准。 它像一把“万能遥控器”&#xff0c;让大语言模型&#xff08;LLM&#xff09;直接连接到 GitHub、文件系统、浏览器和团队知…

作者头像 李华
网站建设 2026/4/18 9:36:34

Python爬虫实战:手把手教你掏空 Books to Scrape 书店数据!

㊗️本期内容已收录至专栏《Python爬虫实战》&#xff0c;持续完善知识体系与项目实战&#xff0c;建议先订阅收藏&#xff0c;后续查阅更方便&#xff5e; ㊙️本期爬虫难度指数&#xff1a;⭐⭐⭐ &#x1f250;福利&#xff1a; 一次订阅后&#xff0c;专栏内的所有文章可永…

作者头像 李华
网站建设 2026/4/16 14:19:35

收藏!小白程序员也能轻松掌握大模型营销术,面试必看!

本文的重点不会在技术相关上&#xff0c;关于微调的分类、特点等等&#xff0c;互联网上精妙的文章比比皆是&#xff0c;我更想分享可能比技术更重要的、也是绝大多数新手程序员可能认为是无用之术的——营销。 面试&#xff0c;在本质上是把自己推销给面试官&#xff0c;在技术…

作者头像 李华
网站建设 2026/4/22 14:43:52

综合项目规划:全栈学生管理系统需求分析与架构设计

综合项目规划&#xff1a;全栈学生管理系统需求分析与架构设计 ——一个老架构师的“别再用 Excel 管学生”的血泪忠告&#xff1a;在国产化教育信息化浪潮里&#xff0c;手工台账 数据孤岛 安全漏洞 国产数据库价值归零&#xff01; 开场白&#xff1a;你的“学生管理”还在…

作者头像 李华