springboot druid数据库连接

druid是阿里云开源高效数据库连接项目

gubpub: https://github.com/alibaba/druid

 

meven配置

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid-spring-boot-starter</artifactId>
   <version>1.1.13</version>
</dependency>

sprinboot 扫描

@Configuration
public class DruidConfig {
    //这个注解读取配置文件前缀为prefix的配置,将外部的配置文件与这里绑定
    @ConfigurationProperties(prefix = "spring.druid")
    //容器的开启与关闭
    @Bean(initMethod = "init",destroyMethod = "close")
    public DruidDataSource dataSource(){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setProxyFilters(Lists.newArrayList(statFilter()));
        return  dataSource;
    }
    //bean注解,成为spring的bean,利用filter将慢sql的日志打印出来
    @Bean
    public Filter statFilter(){
        StatFilter statFilter=new StatFilter();
        //多长时间定义为慢sql,这里定义为5s
        statFilter.setSlowSqlMillis(5000);
        //是否打印出慢日志
        statFilter.setLogSlowSql(true);
        //是否将日志合并起来
        statFilter.setMergeSql(true);
        return  statFilter;
    }
    //这是配置druid的监控
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        return new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
    }

}

yum 数据库配置

spring:
    druid:
        druidClassName: com.mysql.cj.jdbc.Driver
        url: *******
        username: ****
        password: ****
        #最大连接数
        maxActive: 30
        #最小连接数
        minIdle: 5
        #获取连接的最大等待时间
        maxWait: 10000
        #解决mysql8小时的问题
        validationgQuery: SELECT 'X'
        #空闲连接的检查时间间隔
        timeBetweenEvictionRunsMillis: 60000
        #空闲连接最小空闲时间
        minEvictableIdleTimeMillis: 300000

转载一个连接池的配置优化:

https://www.cnblogs.com/sxx1228/p/7125015.html 

 

posted on 2019-06-03 11:08  xiaogui918  阅读(810)  评论(0编辑  收藏  举报

导航