在数据库URL后面加上allowMultiQueries=true
url: jdbc:mysql://ip:port/table?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&useLegacyDatetimeCode=false&allowMultiQueries=true
为了使sql生效,如:
<update id="updateFlagById" parameterType="java.util.List">
<foreach collection="list" item="item" separator=";">
update table set del_flag = 1 where id = #{item}
</foreach>
</update>
如果加了allowMultiQueries=true还不生效请添加以下配置:
@Bean
public DataSource clusterDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
// 这个配置不能和 setProxyFilters 同时配置,不然也不会起作用
// try {
// dataSource.setFilters(filters);
// } catch (SQLException e) {
// e.printStackTrace();
// }
List<com.alibaba.druid.filter.Filter> filterList = new ArrayList<>();
filterList.add(wallFilter());
filterList.add(statFilter());
filterList.add(slf4jLogFilter());
dataSource.setProxyFilters(filterList);
return dataSource;
}
@Bean
public StatFilter statFilter(){
StatFilter sf = new StatFilter();
sf.setSlowSqlMillis(1000);
sf.setLogSlowSql(true);
sf.setMergeSql(true);
return sf;
}
@Bean
public Slf4jLogFilter slf4jLogFilter(){
Slf4jLogFilter logFilter = new Slf4jLogFilter();
logFilter.setStatementExecutableSqlLogEnable(true);
return logFilter;
}
@Bean
public WallFilter wallFilter(){
WallFilter wallFilter = new WallFilter();
wallFilter.setConfig(wallConfig());
return wallFilter;
}
@Bean
public WallConfig wallConfig(){
WallConfig config = new WallConfig();
config.setMultiStatementAllow(true);//允许一次执行多条语句
config.setNoneBaseStatementAllow(true); //允许非基本语句的其他语句
return config;
}
配置 | 说明 |
---|---|
filters | 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:监控统计用的filter:stat;日志用的filter:log4;防御sql注入的filter:wall |
proxyFilters | 类型是List<com.alibaba.druid.filter.Filter>,如果同时配置了filters和proxyFilters,是组合关系,并非替换关系 |
如果有大佬知道为什么只加allowMultiQueries=true不起作用,请告诉我一声,抱拳了!