SpringBoot3 学习笔记 (整合Druid)
一、Druid Github地址:https://github.com/alibaba/druid/
二、常见问题和配置:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
三、配置数据源
1、在https://mvnrepository.com/artifact/com.alibaba/druid 上找最新的版本
2、在pom.xml中添加上Druid数据源依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.18</version> </dependency>
点击更新Maven
更新完毕后,在Extermal Libraries下可以搜得到Druid,并核对版本号
点开可以看Druid的源码,可以看里面的配置项
3、切换数据源,Spring Boot 2.0 以上默认使用 com.zaxxer.hikari.HikariDataSource 数据源,通过 spring.datasource.type 指定数据源。
spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # 自定义数据源
4、编写测试类
@Resource DataSource dataSource; @Test void contextLoads() throws Exception { System.out.println(dataSource.getClass()); System.out.println(dataSource.getConnection()); }
输出结果为
分类:
SpringBoot
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)