springboot中配置druid允许一次执行多条sql

原文:https://blog.csdn.net/jiangjun0130/article/details/77868578

 

1:在配置文件中不需要指定wall防火墙filter。 
配置如下:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/database?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
    username: root
    password: setpay@123
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      # 初始化连接大小
      initial-size: 8
      # 最小空闲连接数
      min-idle: 5
      #最大连接数
      max-active: 64
      #查询超时时间
      query-timeout: 6000
      #事务查询超时时间
      transaction-query-timeout: 6000
      #关闭空闲连接超时时间
      remove-abandoned-timeout: 1800
      # 配置获取连接等待超时的时间
      max-wait: 60000
      ##配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runs-millis: 60000
      ##配置一个连接在池中最小生存的时间,单位是毫秒
      min-victable-idle-time-millis: 300000
      validation-query: select 1
      test-while-idle: true
      test-on-borrow: true
      test-on-return: true
      pool-prepared-statements: true
      max-open-prepared-statements: 300
      verify-server-certificate: false
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      # 合并多个DruidDataSource的监控数据
      use-global-dataSource-stat: true

 

2:在DruidConfig中进行定制化配置。

@Configuration
public class DruidConfig {

    @ConditionalOnClass(DruidDataSource.class)
    @ConditionalOnProperty(name = "spring.datasource.type",
            havingValue = "com.alibaba.druid.pool.DruidDataSource",
            matchIfMissing = true)
    static class Druid extends DruidConfig{

        @Bean
        @ConfigurationProperties("spring.datasource.druid")
        public DruidDataSource dataSource(DataSourceProperties properties){
            DruidDataSource druidDataSource = (DruidDataSource) properties.initializeDataSourceBuilder()
                    .type(DruidDataSource.class).build();
            DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl(properties.determineUrl());
            String validationQuery = databaseDriver.getValidationQuery();
            if(validationQuery != null){
                druidDataSource.setValidationQuery(validationQuery);
            }
            return druidDataSource;
        }

        @Bean
        public WallFilter wallFilter(){
            WallFilter wallFilter = new WallFilter();
            wallFilter.setConfig(wallConfig());
            return wallFilter;
        }

        @Bean
        public WallConfig wallConfig(){
            WallConfig wallConfig = new WallConfig();
            wallConfig.setMultiStatementAllow(true);//允许一次执行多条语句
            wallConfig.setNoneBaseStatementAllow(true);//允许一次执行多条语句
            return wallConfig;
        }
    }
}
--------------------- 
作者:专业矮矬穷 
来源:CSDN 
原文:https://blog.csdn.net/jiangjun0130/article/details/77868578 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

上一篇:Druid手动设置参数错误


下一篇:Spring Boot2.1.9:整合Druid 数据源与监控