MybatisPlus中数据库连接池的配置

多数据源

该场景默认已配置连接池

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>

application.yaml

1
2
3
4
5
6
7
8
9
10
11
12
spring:
application:
name: mp-demo
datasource:
dynamic:
primary: mp
datasource:
mp:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mp_base?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: root

启动日志

1
2
2022-08-02 17:41:57.563  INFO 16080 --- [           main] com.zaxxer.hikari.HikariDataSource       : mp - Starting...
2022-08-02 17:41:57.662 INFO 16080 --- [ main] com.zaxxer.hikari.HikariDataSource : mp - Start completed.

单数据源

application.yaml

1
2
3
4
5
6
7
8
9
10
11
spring:
application:
name: mp-demo
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mp_base?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: root
hikari:
connection-timeout: 60000

默认是 hikari 连接池, 走 HikariDataSource 无参构造方法, 无日志