MyBatis + Druid 启动批量SQL功能

Mysql 连接字符串新增

&allowMultiQueries=true

Druid配置修改

#因为wall会导致无法批量更新,所以这里不配置wall,改为在DruidDataSourceConfig 里面配置支持批量更新的wall
#spring.datasource.druid.filters=stat,wall,config
spring.datasource.druid.filters=stat,config

DruidConfig类中修改

package com.java.core.mapper.config;


import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.sql.SQLException;
import java.util.List;

@Data
@Configuration
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class DruidConfig {
    private String filters;
    private int initialSize;
    private int minIdle;
    private int maxActive;
    private int maxWait;
    private int timeBetweenEvictionRunsMillis;
    private int minEvictableIdleTimeMillis;
    private Boolean testWhileIdle;
    private Boolean testOnBorrow;
    private Boolean testOnReturn;
    private Boolean poolPreparedStatements;
    private int maxPoolPreparedStatementPerConnectionSize;

    public DruidXADataSource getDruidXADataSource() throws SQLException {
        DruidXADataSource xa=new DruidXADataSource();
        xa.setFilters(this.getFilters());
        xa.setInitialSize(this.getInitialSize());
        xa.setMinIdle(this.getMinIdle());
        xa.setMaxActive(this.getMaxActive());
        xa.setMaxWait(this.getMaxWait());
        xa.setTimeBetweenEvictionRunsMillis(this.getTimeBetweenEvictionRunsMillis());
        xa.setMinEvictableIdleTimeMillis(this.getMinEvictableIdleTimeMillis());
        xa.setTestWhileIdle(this.getTestWhileIdle());
        xa.setTestOnBorrow(this.getTestOnBorrow());
        xa.setTestOnReturn(this.getTestOnReturn());
        xa.setPoolPreparedStatements(this.getPoolPreparedStatements());
        xa.setMaxPoolPreparedStatementPerConnectionSize(this.getMaxPoolPreparedStatementPerConnectionSize());
        //配置支持SQL批量更新
        List proxyFilters=xa.getProxyFilters();
        proxyFilters.add(wallFilter());//添加自己new的WallFilter
        xa.setProxyFilters(proxyFilters);
        return xa;
    }
    //配置支持SQL批量更新
    @Bean
    public WallFilter wallFilter(){
        WallFilter wallFilter = new WallFilter();
        wallFilter.setConfig(wallConfig());
        return wallFilter;
    }
    //配置支持SQL批量更新
    @Bean
    public WallConfig wallConfig() {
        WallConfig config = new WallConfig();
        config.setMultiStatementAllow(true);//允许一次执行多条语句
        config.setNoneBaseStatementAllow(true);//允许一次执行多条语句
        return config;
    }

}

你可能感兴趣的:(SpringBoot,mybatis,sql,java)