zoukankan      html  css  js  c++  java
  • SpringBoot集成Druid不支持多条SQL

    在DataSource 初始化Bean 添加 

    List<Filter> proxyFilters = new ArrayList<Filter>();
    			WallFilter statFilter = new WallFilter();
    			WallConfig config = new WallConfig();
    			config.setMultiStatementAllow(true); // 批量操作
    			statFilter.setConfig(config);
    			proxyFilters.add(statFilter);
    			druidDataSource.setProxyFilters(proxyFilters);

    贴上自己的Config

    package com.wstro.config;
    
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.sql.DataSource;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    import com.alibaba.druid.filter.Filter;
    import com.alibaba.druid.pool.DruidDataSource;
    import com.alibaba.druid.support.http.StatViewServlet;
    import com.alibaba.druid.support.http.WebStatFilter;
    import com.alibaba.druid.wall.WallConfig;
    import com.alibaba.druid.wall.WallFilter;
    
    /**
     * Druid数据源配置
     * 
     * @author Joey
     * @Email 2434387555@qq.com
     *
     */
    @Configuration
    @EnableTransactionManagement // 启注解事务管理
    public class DataSourceConfig {
    	private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
    
    	@Value("${spring.datasource.url}")
    	private String dbUrl;
    
    	@Value("${spring.datasource.username}")
    	private String username;
    
    	@Value("${spring.datasource.password}")
    	private String password;
    
    	@Value("${spring.datasource.driverClassName}")
    	private String driverClassName;
    
    	@Value("${spring.datasource.initialSize}")
    	private int initialSize;
    
    	@Value("${spring.datasource.minIdle}")
    	private int minIdle;
    
    	@Value("${spring.datasource.maxActive}")
    	private int maxActive;
    
    	@Value("${spring.datasource.maxWait}")
    	private int maxWait;
    
    	@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    	private int timeBetweenEvictionRunsMillis;
    
    	@Value("${spring.datasource.minEvictableIdleTimeMillis}")
    	private int minEvictableIdleTimeMillis;
    
    	@Value("${spring.datasource.validationQuery}")
    	private String validationQuery;
    
    	@Value("${spring.datasource.testWhileIdle}")
    	private boolean testWhileIdle;
    
    	@Value("${spring.datasource.testOnBorrow}")
    	private boolean testOnBorrow;
    
    	@Value("${spring.datasource.testOnReturn}")
    	private boolean testOnReturn;
    
    	@Value("${spring.datasource.poolPreparedStatements}")
    	private boolean poolPreparedStatements;
    
    	@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    	private int maxPoolPreparedStatementPerConnectionSize;
    
    	@Value("${spring.datasource.filters}")
    	private String filters;
    
    	@Value("${spring.datasource.connectionProperties}")
    	private String connectionProperties;
    
    	/**
    	 * 注册DruidServlet
    	 * 
    	 * @return ServletRegistrationBean
    	 */
    	@Bean
    	public ServletRegistrationBean druidServletRegistrationBean() {
    		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
    		servletRegistrationBean.setServlet(new StatViewServlet());
    		servletRegistrationBean.addUrlMappings("/druid/*");
    		// 白名单:
    		servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
    		// IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not
    		// permitted to view this page.
    		// 登录查看信息的账号密码.
    		servletRegistrationBean.addInitParameter("loginUsername", "joey");
    		servletRegistrationBean.addInitParameter("loginPassword", "jay");
    		// 是否能够重置数据.
    		servletRegistrationBean.addInitParameter("resetEnable", "false");
    		return servletRegistrationBean;
    	}
    
    	/**
    	 * 注册DruidFilter拦截
    	 * 
    	 * @return FilterRegistrationBean
    	 */
    	@Bean
    	public FilterRegistrationBean druidFilterRegistrationBean() {
    		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    		filterRegistrationBean.setFilter(new WebStatFilter());
    		Map<String, String> initParams = new HashMap<String, String>();
    		// 设置忽略请求
    		initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
    		filterRegistrationBean.setInitParameters(initParams);
    		filterRegistrationBean.addUrlPatterns("/*");
    		return filterRegistrationBean;
    	}
    
    	/**
    	 * 配置DataSource
    	 * 
    	 * @return DataSource
    	 * @throws SQLException
    	 */
    	@Bean(initMethod = "init", destroyMethod = "close")
    	@Primary
    	public DataSource dataSource() throws SQLException {
    		DruidDataSource druidDataSource = new DruidDataSource();
    		druidDataSource.setUrl(dbUrl);
    		druidDataSource.setUsername(username);
    		druidDataSource.setPassword(password);
    		druidDataSource.setDriverClassName(driverClassName);
    		// configuration
    		druidDataSource.setInitialSize(initialSize);
    		druidDataSource.setMinIdle(minIdle);
    		druidDataSource.setMaxActive(maxActive);
    		druidDataSource.setMaxWait(maxWait);
    		druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    		druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    		druidDataSource.setValidationQuery(validationQuery);
    		druidDataSource.setTestWhileIdle(testWhileIdle);
    		druidDataSource.setTestOnBorrow(testOnBorrow);
    		druidDataSource.setTestOnReturn(testOnReturn);
    		druidDataSource.setPoolPreparedStatements(poolPreparedStatements);
    		druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
    		try {
    			List<Filter> proxyFilters = new ArrayList<Filter>();
    			WallFilter statFilter = new WallFilter();
    			WallConfig config = new WallConfig();
    			config.setMultiStatementAllow(true); // 批量操作
    			statFilter.setConfig(config);
    			proxyFilters.add(statFilter);
    			druidDataSource.setProxyFilters(proxyFilters);
    			druidDataSource.setFilters(filters);
    		} catch (SQLException e) {
    			logger.error("druid configuration initialization filter", e);
    		}
    		druidDataSource.setConnectionProperties(connectionProperties);
    		return druidDataSource;
    	}
    
    }
    

    properties

    #JDBC
    # u6570u636Eu5E93u8BBFu95EEu914Du7F6E
    # u4E3Bu6570u636Eu6E90uFF0Cu9ED8u8BA4u7684
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://192.168.1.88:3306/wstro?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=root
    
    # u4E0Bu9762u4E3Au8FDEu63A5u6C60u7684u8865u5145u8BBEu7F6EuFF0Cu5E94u7528u5230u4E0Au9762u6240u6709u6570u636Eu6E90u4E2D
    # u521Du59CBu5316u5927u5C0FuFF0Cu6700u5C0FuFF0Cu6700u5927
    spring.datasource.initialSize=5
    spring.datasource.minIdle=5
    spring.datasource.maxActive=20
    
    # u914Du7F6Eu83B7u53D6u8FDEu63A5u7B49u5F85u8D85u65F6u7684u65F6u95F4
    spring.datasource.maxWait=60000
    
    # u914Du7F6Eu95F4u9694u591Au4E45u624Du8FDBu884Cu4E00u6B21u68C0u6D4BuFF0Cu68C0u6D4Bu9700u8981u5173u95EDu7684u7A7Au95F2u8FDEu63A5uFF0Cu5355u4F4Du662Fu6BEBu79D2
    spring.datasource.timeBetweenEvictionRunsMillis=60000
    
    # u914Du7F6Eu4E00u4E2Au8FDEu63A5u5728u6C60u4E2Du6700u5C0Fu751Fu5B58u7684u65F6u95F4uFF0Cu5355u4F4Du662Fu6BEBu79D2
    spring.datasource.minEvictableIdleTimeMillis=300000
    spring.datasource.validationQuery=SELECT 1 FROM DUAL
    spring.datasource.testWhileIdle=true
    spring.datasource.testOnBorrow=false
    spring.datasource.testOnReturn=false
    
    # u6253u5F00PSCacheuFF0Cu5E76u4E14u6307u5B9Au6BCFu4E2Au8FDEu63A5u4E0APSCacheu7684u5927u5C0F
    spring.datasource.poolPreparedStatements=true
    spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
    
    # u914Du7F6Eu76D1u63A7u7EDFu8BA1u62E6u622Au7684filtersuFF0Cu53BBu6389u540Eu76D1u63A7u754Cu9762sqlu65E0u6CD5u7EDFu8BA1uFF0C'wall'u7528u4E8Eu9632u706Bu5899
    spring.datasource.filters=stat,wall,log4j
    
    # u901Au8FC7connectPropertiesu5C5Eu6027u6765u6253u5F00mergeSqlu529Fu80FDuFF1Bu6162SQLu8BB0u5F55
    spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;druid.stat.logSlowSql=true;
    
    # u5408u5E76u591Au4E2ADruidDataSourceu7684u76D1u63A7u6570u636E
    #spring.datasource.useGlobalDataSourceStat=true

  • 相关阅读:
    NET(C#)接入Dubbo服务,Zookeeper作为Dubbo服务的注册中心,实现thrift协议访问接口(3)
    zend studio快捷键
    Guid的生成和数据修整(去除空格和小写字符)
    工作中常用Windows快捷键整理(1)-快速关闭网页
    PHP学习笔记(3)-Zend Studio安装和汉化
    PHP学习笔记(2)
    PHP学习笔记(1)
    判断本机ip是电信还是网通
    作业 4/1
    logging 模块
  • 原文地址:https://www.cnblogs.com/zhousiwei/p/10625776.html
Copyright © 2011-2022 走看看