springboot(二十):HikariCP连接池介绍
Posted 沐风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot(二十):HikariCP连接池介绍相关的知识,希望对你有一定的参考价值。
Druid
application.properties
#mybatis-mysql-druid mybatis.mapper-locations="classpath:mapper/*.xml" mybatis.config-location="classpath:mapper/config/mybatis-config.xml" mybatis.type-aliases-package="com.bitauto.oa.purchase.pojo" spring.datasource.url = jdbc:mysql://127.0.0.1:3306/purchase?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver #spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
PurchaseApplication
package com.bitauto.oa.purchase;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@MapperScan("com.bitauto.oa.purchase.mapper")
public class PurchaseApplication {
public static void main(String[] args) {
SpringApplication.run(PurchaseApplication.class, args);
}
@Autowired
private Environment env;
//datasource注入 destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用.
@Bean(initMethod = "init", destroyMethod = "close")
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));//用户名
dataSource.setPassword(env.getProperty("spring.datasource.password"));//密码
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setInitialSize(2);//初始化时建立物理连接的个数
dataSource.setMaxActive(20);//最大连接池数量
dataSource.setMinIdle(0);//最小连接池数量
dataSource.setMaxWait(60000);//获取连接时最大等待时间,单位毫秒。
dataSource.setValidationQuery("SELECT 1");//用来检测连接是否有效的sql
dataSource.setTestOnBorrow(false);//申请连接时执行validationQuery检测连接是否有效
dataSource.setTestWhileIdle(true);//建议配置为true,不影响性能,并且保证安全性。
dataSource.setPoolPreparedStatements(false);//是否缓存preparedStatement,也就是PSCache
return dataSource;
}
//mybatis SQLSession注入
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mapper/*Mapper.xml"));
return sqlSessionFactoryBean.getObject();
}
//事务支持
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}
HikariCP
资料
http://www.pianshen.com/article/161236925/
以上是关于springboot(二十):HikariCP连接池介绍的主要内容,如果未能解决你的问题,请参考以下文章
追光者系列Springboot 2.0选择HikariCP作为默认数据库连接池的五大理由
3_SpringBoot2.x 默认的数据库连接池 HikariCP