spring boot集成HiKariCP连接池
一、父模块的pom.xml添加HiKariCP连接池依赖
<!-- HiKariCP连接池 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
二、编辑application.yml,添加数据库连接配置,如下:
spring:
datasource:
url: jdbc:mysql://localhost:3306/spring_app?createDatabaseIfNotExist=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Jakarta&useSSL=false
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
auto-commit: true
cachePrepStmts: true
connection-test-query: SELECT 1
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 28740000
maximum-pool-size: 15
minimum-idle: 5
pool-name: DatebookHikariCP
prepStmtCacheSize: 300
prepStmtCacheSqlLimit: 2048
useServerPrepStmts: true
更多HiKariCP参数说明,参考下面的文章
https://segmentfault.com/a/1190000040599037
https://juejin.cn/post/6844904057576882189
三、编写单元测试:DataSourceTest.java,如下:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BootApplication.class)
public class DataSourceTest {
private static final Logger log = LoggerFactory.getLogger(DataSourceTest.class);
@Autowired
private DataSource dataSource;
@Test
public void testConnection() throws Exception {
log.info("当前数据源 = {}", dataSource.getConnection());
}
}
参考资料: