SpringBoot整合数据库连接池其实是很简单的,不管是c3p0还是Druid,步骤都一样,都是在pom.xml中加入依赖,在application.yml加入连接信息,再加上一个配置类即可。
一、整合C3P0
1、pom.xml加入如下依赖
<dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency>
这里要注意c3p0的版本,有时候版本太低的话跟mybatis整合在一起可能会报错。
2、application.yml加入如下配置
c3p0:jdbcUrl: jdbc:mysql://127.0.0.1:3306/weixinseruser: rootpassword: foreverdriverClass: com.mysql.jdbc.DriverminPoolSize: 2maxPoolSize: 10maxIdleTime: 1800000acquireIncrement: 3maxStatements: 1000initialPoolSize: 3idleConnectionTestPeriod: 60acquireRetryAttempts: 30acquireRetryDelay: 1000breakAfterAcquireFailure: falsetestConnectionOnCheckout: false
3、新建一个配置类
package cn.forever.config;import javax.sql.DataSource;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;@Configurationpublic class DataSourceConfiguration {// c3p0 连接池@Bean(name = "dataSource")@Qualifier(value = "dataSource")@Primary@ConfigurationProperties(prefix = "c3p0")public DataSource dataSource() {return DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build();}}
然后程序就自动回使用c3p0连接池啦
e二、整合Druid
其实是一样的模式,但是druid的话可能需要在maven上加入阿里云的镜像
1、pom.xml加入如下依赖
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.12</version></dependency>
2、application.yml加入如下配置
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourceurl: jdbc:mysql://127.0.0.1:3306/sb#生产环境#username: itweb#password: LWHMHM@1205#开发环境username: rootpassword: foreverdriverClassName: com.mysql.jdbc.Driver#连接池的配置信息initialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: truemaxPoolPreparedStatementPerConnectionSize: 20#wall, 去掉这个,不然会报sql injection violationfilters: stat,log4jconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
3、新建一个配置类
package cn.myforever.blog.config;import com.alibaba.druid.pool.DruidDataSource;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import javax.sql.DataSource;import java.sql.SQLException;@Configurationpublic class DruidDataSourceConfiguration {@Value("${spring.datasource.url}")private String dbUrl;@Value("${spring.datasource.username}")private String username;@Value("${spring.datasource.password}")private String password;@Value("${spring.datasource.driverClassName}")private String driverClassName;@Value("${spring.datasource.initialSize}")private int initialSize;@Value("${spring.datasource.minIdle}")private int minIdle;@Value("${spring.datasource.maxActive}")private int maxActive;@Value("${spring.datasource.maxWait}")private int maxWait;@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")private int timeBetweenEvictionRunsMillis;@Value("${spring.datasource.minEvictableIdleTimeMillis}")private int minEvictableIdleTimeMillis;@Value("${spring.datasource.validationQuery}")private String validationQuery;@Value("${spring.datasource.testWhileIdle}")private boolean testWhileIdle;@Value("${spring.datasource.testOnBorrow}")private boolean testOnBorrow;@Value("${spring.datasource.testOnReturn}")private boolean testOnReturn;@Value("${spring.datasource.poolPreparedStatements}")private boolean poolPreparedStatements;@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")private int maxPoolPreparedStatementPerConnectionSize;@Value("${spring.datasource.filters}")private String filters;@Value("{spring.datasource.connectionProperties}")private String connectionProperties;@Bean //声明其为Bean实例@Primary //在同样的DataSource中,首先使用被标注的DataSourcepublic DataSource dataSource() {DruidDataSource datasource = new DruidDataSource();datasource.setUrl(this.dbUrl);datasource.setUsername(username);datasource.setPassword(password);datasource.setDriverClassName(driverClassName);//configurationdatasource.setInitialSize(initialSize);datasource.setMinIdle(minIdle);datasource.setMaxActive(maxActive);datasource.setMaxWait(maxWait);datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);datasource.setValidationQuery(validationQuery);datasource.setTestWhileIdle(testWhileIdle);datasource.setTestOnBorrow(testOnBorrow);datasource.setTestOnReturn(testOnReturn);datasource.setPoolPreparedStatements(poolPreparedStatements);datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);try {datasource.setFilters(filters);} catch (SQLException e) {e.printStackTrace();}datasource.setConnectionProperties(connectionProperties);return datasource;}}
然后程序就自动回使用Druid连接池啦。
结语
用springboot整合连接池的话真的很简单,模式都一样的,但是这里推荐使用Druid
