springboot2.x 整合HikariCP

为什么使用HikariCP

在Springboot2.X版本,数据库的连接池官方推荐使用HikariCP,官方的原话:

Production database connections can also be auto-configured by using a poolingDataSource. Spring Boot uses the following algorithm for choosing a specific implementation:

    We preferHikariCPfor its performance and concurrency. If HikariCP is available, we always choose it.

    Otherwise, if the Tomcat poolingDataSourceis available, we use it.

    If neither HikariCP nor the Tomcat pooling datasource are available and ifCommons DBCP2is available, we use it.

意思是说:

我们更喜欢HikariCP的性能和并发性。如果有HikariCP,我们总是选择它

否则,如果Tomcat池数据源可用,我们将使用它。

如果HikariCP和Tomcat池数据源都不可用,如果Commons DBCP2可用,我们将使用它。

那么如何使用HikariCP呢?

如果你的springboot版本是2.X,当你使用spring-boot-starter-jdbc或者spring-boot-starter-data-jpa依赖,springboot就会自动引入HikariCP的依赖了。

         <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

配置文件:application.yml

server:
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_test?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&useUnicode=true
    username: root
    password: root

springboot2.x 整合HikariCP_第1张图片

你可能感兴趣的:(springboot)