springboot集成JdbcTemplate+druid

application.yml


  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/risk?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
 druid:
    initial-size: 8
    min-idle: 1
    max-active: 20
    max-wait: 60000
    time-between-eviction-runsMillis: 60000
    min-evictable-idle-timeMillis: 300000
    validation-query: select 'x' FROM DUAL
    test-while-idle: true
    test-on-borrow: false
    test-on-return: false
    pool-prepared-statements: true
    max-open-prepared-statements: 20
    max-pool-prepared-statement-per-connection-size: 20
    filters: stat
    connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    use-global-data-source-stat: true
 
DruidConfig.java
package com.dc.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;


/**
 * @author :llf
 * @date :Created in 2019-11-20 16:00
 * @description:Druid配置
 * @version: v1.0
 */
@Component
public class DruidConfig {

    /***
     *
     * @return DruidDataSource
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        return  new DruidDataSource();
    }

    /***
     *
     * @return ServletRegistrationBean
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean bean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map<String,String> map=new HashMap<>();
        map.put("loginUsername","admin");
        map.put("loginPassword","root");
        map.put("allow","");
        map.put("deny","192.168.14.26");
        bean.setInitParameters(map);
        return bean;

    }


//    @Bean
//    public FilterRegistrationBean filterRegistrationBean(){
//        FilterRegistrationBean bean=new FilterRegistrationBean();
//        bean.setFilter(new WebStatFilter());
//        bean.addUrlPatterns("/*");
//        Map<String,String>map=new HashMap<>();
//        map.put("exclusions","*.png,*.js");
//        bean.setInitParameters(map);
//        return bean;
//    }
}
 
访问/druid进入登录界面

 

 

posted @ 2019-11-22 15:37  _Lawrence  阅读(975)  评论(0编辑  收藏  举报