The first step is to import the Druid file information in the Pom.xml file <!--https://mvnrepository.com/artifact/com.alibaba/druid- <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version >1.1. 8</version> </dependency>
Second, add the data source information in the APPLICATION.YML configuration file spring:datasource:username:root password:root url:jdbc:mysql://localhost:3306/testdriver-class-name:com.mysql.jdbc.Driver type:com.alibaba.druid.pool.druiddatasource# schema:#-Classpath:oa_course.sql Database to execute the script # data source other configuration initialsize:5Minidle:5maxactive: -maxwait:60000Timebetweenevictionrunsmillis:60000Minevictableidletimemillis:300000Validationquery:select1From DUAL testwhileidle:trueTestonborrow:falseTestonreturn:falsepoolpreparedstatements:true# Configure Monitoring Statistics interception filters, remove the post-monitoring interface SQL cannot be counted,'Wall'for Firewall filters:stat,wall,log4j maxpoolpreparedstatementperconnectionsize: -Useglobaldatasourcestat:trueCONNECTIONPROPERTIES:DRUID.STAT.MERGESQL=true;d ruid.stat.slowsqlmillis= -
Some configuration information for normal Druid is not available. We need to write a configuration class manually:
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 classdruidconfig {@ConfigurationProperties (prefix="Spring.datasource") @Bean (name="DataSource") PublicDataSource Druid () {return NewDruiddatasource (); } //Configuring monitoring for Druid//1. Configuring a servlet that manages the background@Bean PublicServletregistrationbean Statviewservlet () {Servletregistrationbean Servletregistrationbean=NewServletregistrationbean (NewStatviewservlet (),"/druid/*"); Map<String,String> Initparas =NewHashmap<>(); Initparas.put ("Loginusername","CBX");//Background Login UserInitparas.put ("Loginpassword","aaa147");//Background Login PasswordInitparas.put (" Allow","");//All access is allowed by defaultservletregistrationbean.setinitparameters (Initparas); returnServletregistrationbean; } //2. Configure a monitor filter@Bean PublicFilterregistrationbean Webstatfilter () {Filterregistrationbean Filterregistrationbean=NewFilterregistrationbean (); Filterregistrationbean.setfilter (NewWebstatfilter ()); Map<String,String> Initparas =NewHashmap<>(); Initparas.put ("Exclusions","*.js,*.css,/druid/*");//which files are not filteredfilterregistrationbean.setinitparameters (Initparas); Filterregistrationbean.seturlpatterns (Arrays.aslist ("/*")); returnFilterregistrationbean; }}
write a test class manually, call the database test 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 classHellocontroller {@AutowiredPrivateJdbcTemplate JdbcTemplate; @ResponseBody @GetMapping ("/query") PublicList<map<string,object>>map () {List<Map<String,Object>> list = Jdbctemplate.queryforlist ("SELECT * from Oa_course"); returnlist; }}
The following interface shows the success of the data source configuration
Springboot_ data Access-Consolidate druid& Configure Data source monitoring