zoukankan      html  css  js  c++  java
  • SpringBoot_数据访问-整合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>
    第二部,在application.yml配置文件中添加数据源信息
    spring:
      datasource:
        username: root
        password: root
        url: jdbc:mysql://localhost:3306/test
        driver-class-name: com.mysql.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
    #   schema:
    #     - classpath:oa_course.sql 数据库要执行的脚本
    #   数据源其他配置
        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
    正常druid的一些配置信息是不能使用的需要我们手动写一个配置类:
    package com.baoxing.springboot.config;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import com.alibaba.druid.support.http.StatViewFilter;
    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.ServletListenerRegistrationBean;
    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;
    
    /**
     * Created by chengbx on 2018/6/10.
     */
    @Configuration
    public class DruidConfig {
    
        @ConfigurationProperties(prefix = "spring.datasource")
        @Bean(name = "dataSource")
        public DataSource druid(){
            return  new DruidDataSource();
        }
        //配置Druid的监控
        //1.配置一个管理后台的Servlet
        @Bean
        public ServletRegistrationBean statViewServlet(){
            ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
            Map<String,String> initParas = new HashMap<>();
            initParas.put("loginUsername","cbx");//后台登录用户
            initParas.put("loginPassword","aaa147");//后台登录密码
            initParas.put("allow","");//默认允许所有访问
            servletRegistrationBean.setInitParameters(initParas);
            return servletRegistrationBean;
        }
    
        //2.配置一个监控的filter
        @Bean
        public FilterRegistrationBean webstatFilter(){
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
            filterRegistrationBean.setFilter(new WebStatFilter());
            Map<String,String> initParas = new HashMap<>();
            initParas.put("exclusions","*.js,*.css,/druid/*");//哪些文件不进行过滤
    
            filterRegistrationBean.setInitParameters(initParas);
            filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
            return filterRegistrationBean;
        }
    }
    手动写一个测试类,调用数据库测试
    
    package com.baoxing.springboot.controller;
    
    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;
    
    /**
     * Created by chengbx on 2018/6/10.
     */
    @Controller
    public class HelloController {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @ResponseBody
        @GetMapping("/query")
        public List<Map<String,Object>> map(){
            List<Map<String,Object>> list = jdbcTemplate.queryForList("select * from oa_course");
    
            return list;
        }
    
    }
    出现如下界面则说明数据源配置成功

  • 相关阅读:
    李超线段树板子
    蒟蒻的平衡树学习笔记(=.=
    P2254 [NOI2005] 瑰丽华尔兹
    表达式求值学习笔记
    二分和一些其它算法的奇妙组合
    《wwx》的学习总结(题解)
    set的学习笔记
    题解 P4913 【深基16.例3】二叉树深度
    AGC002F Leftmost Ball
    CSP&&NOIP2020 游记
  • 原文地址:https://www.cnblogs.com/cbxBlog/p/9241188.html
Copyright © 2011-2022 走看看