使用的Spring Boot的版本:2.3.4.RELEASE
先給出答案:com.zaxxer.hikari.HikariDataSource
怎么知道的呢?
新建一個Spring boot項目:springbootTest
配置pom.xml
<dependencies> <!-- SpringBoot 核心包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Mysql驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <!-- spring-boot-starter-jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies>
配置application.yml
spring: #配置MySQL連接 datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/mysql-test?characterEncoding=UTF-8 username: root password: 123456
#type: com.alibaba.druid.pool.DruidDataSource
寫一個類來輸出Spring Boot 默認的數據庫連接池類型
import javax.sql.DataSource; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class ApplicationTest implements ApplicationContextAware { @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { DataSource dataSource = applicationContext.getBean(DataSource.class) ; System.out.println("----------------------------------"); System.out.println(dataSource.getClass().getName()); System.out.println("----------------------------------"); } }
啟動Spring Boot 即可看到啟動日志中打印了:
可以看到Spring Boot 默認的數據庫連接池類型:Hikari
可以在application.yml 的配置中修改連接池類型,具體看前面的配置,在pom.xml 中加入相應的依賴即可。
接下來我們來找找看這個類在哪里