首先需要在配置文件中添加allowMultiQueries=true这段话,如下所示
url: jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowMultiQueries=true
但是光这样还不行,我们需要在数据库配置类中再加一些配置
@Bean(name = "wallFilter")
@DependsOn("wallConfig")
public WallFilter wallFilter() {
WallFilter wallFilter = new WallFilter();
wallFilter.setConfig(wallConfig());
return wallFilter;
}
@Bean(name = "wallConfig")
public WallConfig wallConfig() {
WallConfig config = new WallConfig();
//允许一次执行多条语句
config.setMultiStatementAllow(true);
//允许非基本语句的其他语句
config.setNoneBaseStatementAllow(true);
return config;
}
这样,我们在执行批量更新的时候就不会报sql出错了
题外话,配置文件中的rewriteBatchedStatements=true的作用也是支持批量操作,但是这个配置实现的是诸如我们在写Mybatis的foreach语句时让Mysql执行批量操作而不是一条一条的执行