SpringBoot 整合 druid数据源

1.引入maven 依赖

   <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
    </dependency>

2.在application.properties 中配置

spring.datasource.url=jdbc:mysql://127.0.0.1/spring_jpa?useSSL=false&serverTimezone=Hongkong
spring.datasource.username: root
spring.datasource.password: 123456
spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver


#druid连接池
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
#最大活跃数
spring.datasource.maxActive: 20
#初始化数量
spring.datasource.initialSize: 1
#最大连接等待超时时间
spring.datasource.maxWait: 60000
#打开PSCache,并且指定每个连接PSCache的大小
spring.datasource.poolPreparedStatements: true
spring.datasource.maxPoolPreparedStatementPerConnectionSize: 20
#通过connectionProperties属性来打开mergeSql功能;慢SQL记录
#connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.minIdle: 1
spring.datasource.minEvictableIdleTimeMillis: 3
spring.datasource.timeBetweenEvictionRunsMillis: 6000000000
spring.datasource.validationQuery: select 1 from dual
spring.datasource.testWhileIdle: true
spring.datasource.testOnBorrow: false
spring.datasource.testOnReturn: false
#配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
spring.datasource.filters: stat, wall, logback

3.配置bean

package com.example.boottest.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class DruidConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

    /**
     * 配置访问 druid 监控
     */
    @Bean
   public ServletRegistrationBean druidStateViewServlet(){

       ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");

       //初始化参数 initParams

       servletRegistrationBean.addInitParameter("allow","");
       //添加ip黑名单
       servletRegistrationBean.addInitParameter("deny","");

       //设置控制台管理用户
       servletRegistrationBean.addInitParameter("loginUsername","druid");
       servletRegistrationBean.addInitParameter("loginPassword","123456");
       //是否可以重置数据
       servletRegistrationBean.addInitParameter("resetEnable","false");
       return servletRegistrationBean;
   }

   public FilterRegistrationBean statFilter(){
        //创建过滤器
       FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
       //设置过滤器路径
       filterRegistrationBean.addUrlPatterns("/*");
       //忽略过滤的形式
       filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico");
       return filterRegistrationBean;
   }

}

4.启动项目,访问 localhost/druid
SpringBoot 整合 druid数据源_第1张图片

你可能感兴趣的:(SpringBoot,druid,数据源)