写在最前
HikariCP
HiKariCP 是数据库连接池的一个后起之秀,代码非常轻量,并且速度非常的快。已经成为了 SpringBoot 2.0 默认的连接池(官方推荐使用)!其官网有性能测试报告:https://github.com/brettwooldridge/HikariCP
SpringBoot 连接 Mysql
Demo 地址:mingyue-springboot-mysql
1.引入依赖
spring-boot-starter-jdbc 包含了 HikariCP 依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
2.修改配置文件
# 数据源
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/dbtrial?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
# 连接池最大连接数
maximum-pool-size: 12
# 空闲时保持最小连接数
minimum-idle: 5
# 空闲连接存活时间
idle-timeout: 300000
# 连接超时时间
connection-timeout: 20000
# 测试sql
connection-test-query: select 1
3.连接测试
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author Strive
* @date 2022/4/20 10:35
* @description
*/
@SpringBootTest
public class DataSourceTests {
@Autowired private DataSource dataSource;
@Test
public void testConnection() throws Exception {
System.out.println(dataSource.getConnection());
}
}
执行日志如下:
10:39:37.150 INFO 25108 --- [ main] com.csp.mingyue.mysql.DataSourceTests : Started DataSourceTests in 2.986 seconds (JVM running for 4.562)
2022-04-20 10:39:37.496 INFO 25108 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-04-20 10:39:38.733 INFO 25108 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
HikariProxyConnection@486286648 wrapping com.mysql.cj.jdbc.ConnectionImpl@66deec87
2022-04-20 10:39:38.753 INFO 25108 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2022-04-20 10:39:38.756 INFO 25108 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
HikariProxyConnection@486286648 wrapping com.mysql.cj.jdbc.ConnectionImpl@66deec87
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/78341.html