Spring Boot配置druid连接池

1.引入包


    com.alibaba
    druid
    1.0.28
2.application.yml中在spring.datasource中增加配置

type: com.alibaba.druid.pool.DruidDataSource

filters: stat,wall,log4j  #stat放到wall前面则统计拦截的时间,stat放在wall后面则不统计拦截的时间

注意:不要同时配置filters和proxyFilters,filter都是内置的,想通过proxyFilters来定制的话,就不要配置filters。

3.连接池druidDataSource

//使用连接池dataSource
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
    DruidDataSource druidDataSource = new DruidDataSource();
    return druidDataSource;
}
4.如果需要开启wall监控,同时允许multiStatementAllow,就不要在application.yml中配置filter,

自己定义

//使用连接池dataSource
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
    DruidDataSource druidDataSource = new DruidDataSource();
    List filterList=new ArrayList<>();
    filterList.add(wallFilter());
    druidDataSource.setProxyFilters(filterList);
    return druidDataSource;
}
@Bean
public WallFilter wallFilter(){
    WallFilter wallFilter=new WallFilter();
    wallFilter.setConfig(wallConfig());
    return  wallFilter;
}
@Bean
public WallConfig wallConfig(){
    WallConfig config =new WallConfig();
    config.setMultiStatementAllow(true);//允许一次执行多条语句
    config.setNoneBaseStatementAllow(true);//允许非基本语句的其他语句
    return config;
}

其余的配置,请参照:http://blog.csdn.net/xiaoyu411502/article/details/51392237  https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98



你可能感兴趣的:(数据库,mysql)