springboot+mybatisplus配置连接池
时间: 2023-08-31 09:09:19 浏览: 124
SpringBoot整合mybatis配置
要配置Spring Boot和MyBatis-Plus的连接池,你需要在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.x.x</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
```
其中,mybatis-plus-boot-starter是MyBatis-Plus的Spring Boot Starter,druid-spring-boot-starter是阿里巴巴的Druid连接池的Spring Boot Starter。
然后,在application.properties文件中添加以下配置:
```properties
# 数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
# 连接池配置
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
```
其中,spring.datasource.url、spring.datasource.username和spring.datasource.password是数据库连接信息,spring.datasource.druid.*是Druid连接池的相关配置。
最后,在Spring Boot的配置类中添加以下代码:
```java
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
```
这段代码的作用是配置MyBatis-Plus的分页插件和禁用MyBatis自带的Executor类型。这将使MyBatis-Plus使用新的Executor类型,从而提高性能。
现在,你已经成功地配置了Spring Boot和MyBatis-Plus的连接池。
阅读全文