zoukankan      html  css  js  c++  java
  • springboot整合Druid数据源

    整合Druid数据源

    引入依赖

    pom.xml文件

     <!--引入druid数据源 -->
       <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid</artifactId>
           <version>1.1.8</version>
       </dependency>

       <!-- https://mvnrepository.com/artifact/log4j/log4j -->
       <!-- 如果 不加入这依赖       配置监控统计拦截的filters时   这个会报错 filters: stat,wall,log4j   -->
       <dependency>
           <groupId>log4j</groupId>
           <artifactId>log4j</artifactId>
           <version>1.2.17</version>
       </dependency>

     

    application.yml配置文件

    spring:
    datasource:
      username: root
      password: 12
      url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
      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
       # 合并多个DruidDataSource的监控数据
       #useGlobalDataSourceStat: true

     

    在config文件夹下创建一个DruidConfig的配置类,实例化Druid Datasource;可以配置后台sql监控功能,或 配置一个web监控的filter(过滤器)

    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.sql.DataSource;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;

    @Configuration
    public class DruidConfig {
       //指定加载appliction.yml文件里面的spring.datasource开头的
       // DruidDataSource类里面的属性与appliction.yml文件里面的spring.datasource开头的对应映射
       @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<>();

           initParams.put("loginUsername","admin");
           initParams.put("loginPassword","123456");
           initParams.put("allow","");//默认就是允许所有访问
           initParams.put("deny","");

           bean.setInitParameters(initParams);
           return bean;
      }


       //2、配置一个web监控的filter
       @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;
      }
    }

     

     

    创建一个UserController类测试,这里用的是JdbcTemplate 进行测试

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;

    import java.util.List;
    import java.util.Map;

    @Controller
    public class UserController {

       @Autowired
       JdbcTemplate jdbcTemplate;


       @ResponseBody
       @GetMapping("/query")
       public Map<String,Object> map(){
           List<Map<String, Object>> list = jdbcTemplate.queryForList("select * FROM user");
           return list.get(0);
      }
    }

     

    启动项目,打开网址:http://localhost:8080/druid 可以通过登录,admin,123456;查看druid数据源状态监控

    img

    img

  • 相关阅读:
    Maven与Eclipse整合使用
    Maven学习总结(五)——聚合与继承【如果想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合】
    Maven的核心概念:maven仅仅定义了抽象的生命周期,具体的任务都是交由插件完成的
    使用Maven构建项目---【重点需要掌握:Jetty和Maven两种项目骨架】
    Maven项目构建过程练习
    maven编码gbk的不可映射字符”解决办法
    Maven
    Spring Aop的方法执行简单模型
    2016年看过的书
    ExcelReader
  • 原文地址:https://www.cnblogs.com/bxbo/p/13498284.html
Copyright © 2011-2022 走看看