springboot 查找连接池的方式

本文详细介绍Spring Boot 2.x版本中如何使用HikariCP作为数据库连接池,包括环境搭建、依赖导入及application.yml配置。HikariCP因其性能和并发优势被官方推荐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

而且在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的依赖了。

使用指定的数据库连接池

如果你需要使用指定的数据库连接池,那么你需要在application.properties中配置:spring.datasource.type

好了关于为什么用HikariCP就说这么多吧,需要了解更多的小伙伴可以具体的上网了解了解。

环境
下面就是搭建Spring-boot+mybatis的环境:

JDK: 1.8   Spring-Boot要求至少是jdk6,建议用8以上

Maven: 3.3.9  

SpringBoot: 2.0.3.RELEASE

开发工具:Intellij IDEA 2017  或者  eclipse  都可以  这里以IDEA为             

首先我们来导入依赖:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--排除默认的tomcat-jdbc-->
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!-- mybatis一定要使用starter,不然无法自动配置和注入 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>    

基本上以上的依赖就足够了,前面介绍过,只需要导入spring-boot-starter-jdbc依赖springboot就默认使用Hikari作为数据库连接池了。

application.yml的配置
server.port=8080

#### 数据库连接池属性
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mytest?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
#自动提交
spring.datasource.default-auto-commit=true
#指定updates是否自动提交
spring.datasource.auto-commit=true
spring.datasource.maximum-pool-size=100
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
spring.datasource.validation-query=SELECT 1
spring.datasource.test-on-borrow=false
spring.datasource.test-while-idle=true
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.time-between-eviction-runs-millis=18800
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000

# mybatis对应的映射文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
# mybatis对应的实体类
mybatis.type-aliases-package=com.test.model

启动类
package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootMybatisHikaricpApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisHikaricpApplication.class, args);
    }
}

 

好了其他的关于Controller  service  dao  mapper  就在这里不多写了,这些的写法和传统的ssm写法一致

给大家科普两个知识点:

@RequestParam 用于将请求参数区数据映射到功能处理方法的参数上,value:参数名字,即入参的请求参数名字,如fileldName表示请求的参数区中的名字为fileldName的参数的值将传入,required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;

@Controller和@RestController的区别?

测试
如果项目成功启动了,那么可以开始测试了

推荐使用一个强大的http请求工具:Postman,这也是测试人员常用的接口测试工具

需要Postman工具的小伙伴们可以扫描下面的二维码点击关注找小编索取,除了Postman,其他方面的软件也可以关注索取
--------------------- 
作者:Calvin_it 
来源:优快云 
原文:https://blog.youkuaiyun.com/qq_37840993/article/details/81938637 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值