MybatisPlus 批量更新 异常 java.sql.SQLException: sql injection violation, multi-statement

  • Post author:
  • Post category:java


前言:


使用MybatisPlus批量更新数据库时抛异常:java.sql.SQLException: sql injection violation, multi-statement



找到报错的sql:
    <update id="batchUpdateCategory">
        <foreach collection="list" item="bean" index="index" open="" close="" separator=";">
            update cms_topic
            set category_id = #{bean.categoryId}
            where id = #{bean.id}
        </foreach>
    </update>




原因:多条语句执行被druid拦截了

在这里插入图片描述



两种解决方法:



第一种:更改sql

改成下面这种

    <update id="batchUpdateCategory">
        update cms_topic
        <set>
            category_id = #{categoryId}
        </set>
        <where>
            id in
            <foreach collection="list" item="bean" index="index" open="(" close=")" separator=",">
                #{bean.id}
            </foreach>
        </where>

    </update>



第二种(根本上解决问题):修改配置信息



公司用的是nacos,所以把上图中的wall删除,就可以了,或者

 @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource myDataSource() {
        List filterList=new ArrayList<>();
        filterList.add(wallFilter());
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setProxyFilters(filterList);
        return druidDataSource;
    }

    @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;
    }

参考文章:


https://blog.csdn.net/qq_37141773/article/details/92830379



http://www.xmhzd.com/study/article/view-380.html




觉得有帮助的点个赞吧!!



版权声明:本文为weixin_43864927原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。