springboot 查找连接池的方式

而且在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 
来源:CSDN 
原文:https://blog.csdn.net/qq_37840993/article/details/81938637 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/zxssoft/article/details/83181134
今日推荐