springboot 梳理2--配置druid数据源

1. pom.xml添加

<!--德鲁伊数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.18</version>
        </dependency>

 

 

2. yml

spring:
  #默认datasource hikaricp
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf8&&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    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
    #配置监控统计拦截的fi1ters,去掉后监控界面sq1无法统计,'wall'用于防火墙
    filters : stat,wall
    maxPoolPreparedstatementPerconnectionsize: 20
    useG1oba1Datasourcestat: true
    connectionProperties: druid.stat.mergeSq1=true;druid.stat.s1owSq1Mi11is=500

 

 

3. conf层下,DruidConfig

 

@Configuration
public class DruidConfig {
    //把yml里面的设置文件放进去
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }


//可以放其他

}

 

 

里面可以放,配置监控

/**
    *配置监控
    * @return
    */
    @Bean
    public ServletRegistrationBean statviewservlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        HashMap<String, String> map = new HashMap<>(2);
        map.put("loginUsername", "sr");
        map.put("loginPassword", "123456");
        bean.setInitParameters(map);
        return bean;
    }

    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();bean.setFilter(new WebStatFilter());
        HashMap<String,String> map = new HashMap<>(8);
        map.put("exclusions " , "*.js ");
        map.put("exclusions " , "*.css ");
        map.put("exclusions " , "*.html ");
        map.put("exclusions " , "*.jpg ");
        bean.setInitParameters (map);
        bean.setUrlPatterns (Arrays.asList("/*"));

return bean; }

 

posted @ 2021-01-29 14:02  Master_Sun  阅读(236)  评论(0编辑  收藏  举报