解决 druid批量执行sql语句报错问题

项目场景:

项目中启用Druid的统计管理,在执行批量sql时出现此问题


问题描述

java.sql.SQLException: sql injection violation, multi-statement not allow

sql注入冲突,不允许多语句
在mybatis 中使用foreach标签拼接多条sql语句(以)结尾),执行时报错

<update id="update"  parameterType="">
    <foreach collection="list" item="item" index="index">
        update tb_user set isDelete=0  where userId = #{item.userId};
    foreach>  
  update>

原因分析:

wall-filter会拦截多次声明请求的循环sql语句,即使设置为true,还会检测到sql语句间的';'分号会视为sql已经结束,所以有sql循环,第二个sql语句就会报出异常。


解决方案:

追加 allowMultiQueries=true 参数

spring.datasource.qhgyl.url=jdbc:mysql://localhost:13306/ggh?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8&allowMultiQueries=true

关闭 druid 的wall-filter

直接注释掉或者删除掉

#spring.datasource.qhh.filters=wall

你可能感兴趣的:(mybaits,sql,数据库,mybatis,springboot)