• 整合springboot和druid数据源


    整合Druid数据源

    1. 引入数据源

      <!--        导入log4j-->
              <!-- https://mvnrepository.com/artifact/log4j/log4j -->
              <dependency>
                  <groupId>log4j</groupId>
                  <artifactId>log4j</artifactId>
                  <version>1.2.17</version>
              </dependency>
      
    2. 配置数据源

      由于DataSourceProperties文件中没有对应的其他数据源信息,所以我们需要自己配置一个druiddataSource绑定配置文件中的属性

      spring:
        datasource:
          username: root
          password:
          url: jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf8
          driver-class-name: com.mysql.jdbc.Driver
          type: com.alibaba.druid.pool.DruidDataSource
          #   数据源其他配置
          initialSize: 5
          minIdle: 5
          maxActive: 20
          maxWait: 60000
          timeBetweenEvictionRunsMillis: 60000
          minEvictableIdleTimeMillis: 300000
          validationQuery: SELECT 1 FROM DUAL
          testWhileIdle: true
          testOnBorrow: false
          testOnReturn: false
          poolPreparedStatements: true
          #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
          filters: stat,wall,log4j
          maxPoolPreparedStatementPerConnectionSize: 20
          useGlobalDataSourceStat: true
          connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
      
    3. 配置类

      注意配置上数据源之后,运行会出现问题,需要引入log4j或者在filter中去掉log4j

      <!--        导入log4j-->
              <!-- https://mvnrepository.com/artifact/log4j/log4j -->
              <dependency>
                  <groupId>log4j</groupId>
                  <artifactId>log4j</artifactId>
                  <version>1.2.17</version>
              </dependency>
      
      @Configuration
      public class DruidConfig {
      
          @ConfigurationProperties(prefix = "spring.datasource")
          @Bean
          public DataSource druid(){
              return new DruidDataSource();
          }
      
          //配置druid的监控
          //1.配置一个管理后台的servlet
          @Bean
          public ServletRegistrationBean statViewServlet(){
              ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
              Map<String,String> initParams= new HashMap<>();
              //可配置的属性看StatViewServlet类的属性
              initParams.put("loginUsername","admin");
              initParams.put("loginPassword","123456");
              initParams.put("allow",""); //默认允许所有
              bean.setInitParameters(initParams);
              return bean;
          }
          //2.配置一个监控的filter
          @Bean
          public FilterRegistrationBean weStatFilter(){
              FilterRegistrationBean bean = new FilterRegistrationBean();
              bean.setFilter(new WebStatFilter());
              Map<String,String> initParams= new HashMap<>();
              initParams.put("exclusions","*.js,*.css,/druid/*");
              bean.setUrlPatterns(Arrays.asList("/*"));
              bean.setInitParameters(initParams);
              return bean;
          }
      }
      
  • 相关阅读:
    经典SQL语句大全 学者必看
    13个SQL优化技巧
    全面解析SQL SERVER 的左右内连接
    ORM框架
    JPA SQL 的复杂查询createNamedQuery
    SQL 复杂查询
    前端学习(十三)js运算符(笔记)
    前端学习(十二)js数据类型(笔记)
    前端学习(十一)函数(笔记)
    前端学习(十)初识js(笔记)
  • 原文地址:https://www.cnblogs.com/JIATCODE/p/13170012.html
Copyright © 2020-2023  润新知