Hikari
1、首先集成 hikari springboot默认集成,只需要简单的配置即可
1.1 首先导入包
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
1.2 编写配置文件
# hikari #spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot2 #spring.datasource.username=root #spring.datasource.password=root #spring.datasource.driver-class-name=com.mysql.jdbc.Driver
1.3 编写配置类
package com.archibaldwitwicke.springboot2.chapter05.dao; import org.springframework.context.annotation.Configuration; @Configuration public class DataSourceConfiguration { // Hikari 连接池 // @Bean(name = "dataSource") // public DataSource dataSource(@Autowired Environment environment) { // HikariDataSource ds = new HikariDataSource(); // ds.setJdbcUrl(environment.getProperty("spring.datasource.url")); // ds.setUsername(environment.getProperty("spring.datasource.username")); // ds.setPassword(environment.getProperty("spring.datasource.password")); // ds.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name")); // return ds; // } }
1.4 然后即可即可 jdbctemplate模板使用。
C3P0
1 导入相应包
<dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
2 编写相应配置文件
# c3p0 #c3p0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/springboot2 #c3p0.user=root #c3p0.password=root #c3p0.driverClass=com.mysql.jdbc.Driver #c3p0.minPoolSize=2 #c3p0.maxPoolSize=10 #c3p0.maxIdleTime=1800000 #c3p0.acquireIncrement=3 #c3p0.maxStatements=1000 #c3p0.initialPoolSize=3 #c3p0.idleConnectionTestPeriod=60 #c3p0.acquireRetryAttempts=30 #c3p0.acquireRetryDelay=1000 #c3p0.breakAfterAcquireFailure=false #c3p0.testConnectionOnCheckout=false
3 编写数据源类
package com.archibaldwitwicke.springboot2.chapter05.dao; import org.springframework.context.annotation.Configuration; @Configuration public class DataSourceConfiguration { // c3p0 连接池 // @Bean(name = "dataSource") // @Qualifier(value = "dataSource") // @Primary // @ConfigurationProperties(prefix = "c3p0") // public DataSource dataSource(@Autowired Environment environment) { // return DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build(); // } }
4 然后即可注入 jdbctemplate 操作数据库
Druid
1 导入相应包(需要日志包)
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
2 编写配置文件
# durid # 驱动配置信息 spring.datasource.url = jdbc:mysql://127.0.0.1:3306/springboot2 spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver #连接池的配置信息 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 spring.datasource.maxWait=60000 spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.filters=stat,wall,log4j spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
还需要log4j配置:
log4j.rootCategory=INFO, stdout log4j.rootLogger=info, stdout ### stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n ### set package ### log4j.logger.org.springframework=info log4j.logger.org.apache.catalina=info log4j.logger.org.apache.commons.digester.Digester=info log4j.logger.org.apache.catalina.startup.TldConfig=info log4j.logger.chb.test=debug
3 编写数据源类
package com.archibaldwitwicke.springboot2.chapter05.dao; 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; @Configuration public class DruidDataSourceConfiguration { // private Logger logger = LoggerFactory.getLogger(DruidDataSourceConfiguration.class); @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中,首先使用被标注的DataSource public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(this.dbUrl); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); //configuration datasource.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) { // logger.error("druid configuration initialization filter", e); e.printStackTrace(); } datasource.setConnectionProperties(connectionProperties); return datasource; } }
4 即可使用 jdbctemplate
以上所有数据源,根据需要导入数据驱动, 测试使用的为msyql
原文地址:https://www.cnblogs.com/hfultrastrong/p/8595161.html
时间: 2024-10-07 16:50:42