SpringBoot_数据访问-整合Druid&配置数据源监控

匿名 (未验证) 提交于 2019-12-03 00:38:01
第一步,在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;     }  }
出现如下界面则说明数据源配置成功

原文:https://www.cnblogs.com/cbxBlog/p/9241188.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!