资讯专栏INFORMATION COLUMN

SpringBoot集成Druid不支持多条SQL

leejan97 / 4521人阅读

摘要:在初始化添加批量操作贴上自己的数据源配置启注解事务管理注册白名单黑名单存在共同时,优先于如果满足的话提示登录查看信息的账号密码是否能够重置数据注册拦截设置忽略请求配置批量操作

DataSource初始化Bean 添加

List proxyFilters = new ArrayList();
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 initParams = new HashMap();
        // 设置忽略请求
        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 proxyFilters = new ArrayList();
            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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75478.html

相关文章

  • 单手撸了个springboot+mybatis+druid

    摘要:配置想想,我们需要哪些数据库要用到,数据库连接池要用到桥接器要用到,因此要仓库点我去仓库中找到搜索这些加进去。 本文旨在用最通俗的语言讲述最枯燥的基本知识 最近身边的程序员掀起了学习springboot的热潮,说什么学会了springboot在大街上就可以横着走、什么有了springboot妈妈再也不担心我的编程了、什么BAT都喜欢的框架...听得作者那个心痒痒的,于是找了个时间,下载...

    adie 评论0 收藏0
  • springboot (一)集成tkmapper

    摘要:整合想着每次搭建新项目时框架都要从新搭建,基本常用的也就哪几种,现在就来搭建一种常用的后台框架,以后新开小项目可以直接拿来使用项目整体结构图新建空白项目,选中依赖略,也可以完全根据本人代码操作文件依赖项展示 springboot整合tkMapper 想着每次搭建新项目时框架都要从新搭建,基本常用的也就哪几种,现在就来搭建一种常用的springboot后台框架,以后新开小项目可以直接拿来...

    Shihira 评论0 收藏0
  • SpringBoot+Mybatis配置Druid多数据源

    摘要:多数据源,一般用于对接多个业务上独立的数据库可能异构数据库。这也就导致异构数据库的检查也是类似问题。内容略数据源多数据源,涉及到异构数据库,必须明确指定,否则的转换出错取值内容可参考初始连接数最大连接池数量。 开篇之前,说一句题外话。多数据源和动态数据源的区别。 多数据源,一般用于对接多个业务上独立的数据库(可能异构数据库)。 动态数据源,一般用于大型应用对数据切分。 配置参考 如...

    Songlcy 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<