SpringBoot3整合Druid數據源的實現過程
一、實現過程
1.創(chuàng)建程序
2.引入依賴
<?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>springboot-starter-druid-04</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開發(fā)的場景啟動器 -->
<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.18</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>
<!-- SpringBoot應用打包插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
```3.啟動類
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}4.配置文件編寫
spring:
datasource:
# 連接池類型
type: com.alibaba.druid.pool.DruidDataSource
# Druid的其他屬性配置 springboot3整合情況下,數據庫連接信息必須在Druid屬性下!
druid:
url: jdbc:mysql://localhost:3306/day01
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,執(zhí)行validationQuery檢測連接是否有效。
test-while-idle: true
# 既作為檢測的間隔時間又作為testWhileIdel執(zhí)行的依據
time-between-eviction-runs-millis: 60000
# 銷毀線程時檢測當前連接的最后活動時間和當前時間差大于該值時,關閉當前連接(配置連接在池中的最小生存時間)
min-evictable-idle-time-millis: 30000
# 用來檢測數據庫連接是否有效的sql 必須是一個查詢語句(oracle中為 select 1 from dual)
validation-query: select 1
# 申請連接時會執(zhí)行validationQuery檢測連接是否有效,開啟會降低性能,默認為true
test-on-borrow: false
# 歸還連接時會執(zhí)行validationQuery檢測連接是否有效,開啟會降低性能,默認為true
test-on-return: false
# 是否緩存preparedStatement, 也就是PSCache,PSCache對支持游標的數據庫性能提升巨大,比如說oracle,在mysql下建議關閉。
pool-prepared-statements: false
# 要啟用PSCache,必須配置大于0,當大于0時,poolPreparedStatements自動觸發(fā)修改為true。在Druid中,不會存在Oracle下PSCache占用內存過多的問題,可以把這個數值配置大一些,比如說100
max-pool-prepared-statement-per-connection-size: -1
# 合并多個DruidDataSource的監(jiān)控數據
use-global-data-source-stat: true
logging:
level:
root: debug5.編寫Controller
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping("/getUser")
@ResponseBody
public User getUser(){
String sql = "select * from users where id = ? ; ";
User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), 1);
log.info("查詢的user數據為:{}",user.toString());
return user;
}
}6.啟動測試
7.問題解決
通過源碼分析,druid-spring-boot-3-starter目前最新版本是1.2.18,雖然適配了SpringBoot3,但缺少自動裝配的配置文件,需要手動在resources目錄下創(chuàng)建
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件內容如下!
com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure

到此這篇關于SpringBoot3整合Druid數據源的實現過程的文章就介紹到這了,更多相關springboot druid數據源內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot2.6.3讀取不到nacos上的配置文件問題
這篇文章主要介紹了springboot2.6.3讀取不到nacos上的配置文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝)
這篇文章主要介紹了IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝),本文通過截圖給大家展示的非常詳細,需要的朋友可以參考下2021-09-09
詳解Spring 參數驗證@Validated和@Valid的區(qū)別
這篇文章主要介紹了詳解參數驗證 @Validated 和 @Valid 的區(qū)別,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
Java自帶的Http?Server實現設置返回值的類型(content-type)
這篇文章主要介紹了Java自帶的Http?Server實現設置返回值的類型(content-type),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

