• SpringBoot配置Druib


    1、导入依赖

     1         <!--druib数据源-->
     2         <dependency>
     3             <groupId>com.alibaba</groupId>
     4             <artifactId>druid</artifactId>
     5             <version>1.1.21</version>
     6         </dependency>
     7 
     8         <dependency>
     9             <groupId>org.springframework.boot</groupId>
    10             <artifactId>spring-boot-starter-jdbc</artifactId>
    11         </dependency>
    12 
    13         <!--日志-->
    14         <dependency>
    15             <groupId>log4j</groupId>
    16             <artifactId>log4j</artifactId>
    17             <version>1.2.17</version>
    18         </dependency>
    19         <!--JDBC连接驱动-->
    20         <dependency>
    21             <groupId>mysql</groupId>
    22             <artifactId>mysql-connector-java</artifactId>
    23             <scope>runtime</scope>
    24         </dependency>

    2、配置数据源

     1 spring:
     2   datasource:
     3     username: xxxx
     4     password: xxxx
     5     url: jdbc:mysql://xxxx:3306/yidai?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
     6     driver-class-name: com.mysql.cj.jdbc.Driver
     7     type: com.alibaba.druid.pool.DruidDataSource
     8 
     9     initialSize: 5
    10     minIdle: 5
    11     maxActive: 20
    12     maxWait: 60000
    13     timeBetweenEvictionRunsMillis: 60000
    14     minEvictableIdleTimeMillis: 300000
    15     validationQuery: SELECT 1 FROM DUAL
    16     testWhileIdle: true
    17     testOnBorrow: false
    18     testOnReturn: false
    19     poolPreparedStatements: true
    20     #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    21     filters: stat,wall,log4j
    22     maxPoolPreparedStatementPerConnectionSize: 20
    23     useGlobalDataSourceStat: true
    24     connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

    3、DruibConifg配置类

    package com.example.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;
    
    import javax.servlet.ServletRegistration;
    import javax.sql.DataSource;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author : ppx
     * @date : 20:50 2021/1/31
     */
    
    
    @Configuration
    public class DruidConfig {
    
        @ConfigurationProperties(prefix = "spring.datasource")
        @Bean
        public DataSource druib(){
            return new DruidDataSource();
        }
    
        @Bean
        public ServletRegistrationBean statViewServlet() {
            ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
            Map<String, String> initParams = new HashMap<>();
    
            initParams.put("loginUsername", "admin");
            initParams.put("loginPassword", "123456");
            initParams.put("allow", "");// 默认就是允许所有访问
            initParams.put("deny", "10.18.172.124");
    
            bean.setInitParameters(initParams);
            return bean;
        }
    
    
        @Bean
        public FilterRegistrationBean webStatFilter() {
            FilterRegistrationBean bean = new FilterRegistrationBean();
            bean.setFilter(new WebStatFilter());
    
            Map<String, String> initParams = new HashMap<>();
            initParams.put("exclusions", "*.js,*.css,/druid/*");
    
            bean.setInitParameters(initParams);
    
            bean.setUrlPatterns(Arrays.asList("/*"));
    
            return bean;
        }
    
    
    }

    4、页面访问

     http://localhost:8080/druid/

       

    您的资助是我最大的动力!
    金额随意,欢迎来赏!

    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
    如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

    如果,想给予我更多的鼓励,求打

    因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【皮皮虾】!

    联系或打赏博主【皮皮虾】!

    可以邮件形式询问:【1260866273@qq.com】!

  • 相关阅读:
    CI框架用cookie实现用户自动登录
    php预定义常量
    ubuntu root密码
    jQuery的form中ajaxSubmit参数
    优酷视屏播放去广告链接id_XXX.html内容替换红色
    linux 重启apache:apachectl -k graceful
    python深copy与浅copy的区别
    python3中pathlib库的Path类方法汇总
    unittest使用discover加载不同目录下的case失败,提示Path must be within the project
    自定义HTMLTestRunner报告case名称
  • 原文地址:https://www.cnblogs.com/rppx/p/14354286.html
Copyright © 2020-2023  润新知