Integrating hikaricp connection pool in spring

Time:2020-11-25

1、 Add related dependencies to the project

open pom.xml File, add the following two dependency configurations
1) Mysql database driven dependency

Integrating hikaricp connection pool in spring

2) JDBC support for spring objects (hikaricp connection pool will be downloaded by default)

Integrating hikaricp connection pool in spring

2、 Configure the hikarip connection pool

open application.properties Configuration file, add the following content (required)
spring.datasource.url=jdbc:mysql://localhost:3306/dbgoods?serverTimezone=GMT%2B8&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
Pay attention to the port number, do not write wrong!!!

3、 Hikaricp connection pool test

Add unit test classes and test methods to the project, as follows:
package com.cy.pj.common.datasource;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class DataSourceTests {
    @Autowired
    private DataSource dataSource;
    @Test
    public void testConnection() throws Exception{
        System.out.println(dataSource.getConnection());
    }

######Note: the package of datasource should not be misquoted!!! (this package is:javax.sql.DataSource