1. 程式人生 > >springboot 查詢連線池的方式

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  版權宣告:本文為博主原創文章,轉載請附上博文連結!