springboot整合druid连接池

springboot整合druid连接池

引入依赖



<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>1.1.5version>
dependency>

配置

spring:
  datasource:
    username: root
    password: ***
    url: jdbc:mysql://192.168.3.45:3306/test?serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMills: 30000
    validationQuery: select 1 from dual
    testWhileIdle: true
    testOnBorrow: false
    poolPreparedStatements: true
    poolPreparedStatementPerConnectionSize: 20
    filters: stat,wall,log4j
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    useGlobalDataSourceStat: true

重写配置类

@Configuration
public class DruidConfig {
    /**
     * 配置监控服务器
     * @return
     */
    @Bean
    public ServletRegistrationBean statViewServle(){
        //后台监控地址
        ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        servletRegistrationBean.addInitParameter("allow","192.168.3.54,192.168.3.45,127.0.0.1");
        servletRegistrationBean.addInitParameter("deny","192.168.3.102");
        //设置没登录用户名以及密码
        servletRegistrationBean.addInitParameter("loginUsername","druid");
        servletRegistrationBean.addInitParameter("loginPassword","123456");
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean filterStat(){
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.png,*.jpg,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

在主类中允许自动配置

可以加入@EnableAutoConfiguration或者@SpringBootApplication

@SpringBootApplication注解等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan

效果

在本项目路径下的/druid/index.html页面下可以利用自己配置的而登陆名以及密码进行登录!

你可能感兴趣的:(springboot整合druid连接池)