You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Jeecg-boot遇‘multi-statement not allow’错误求解决

解决Jeecg-boot中'multi-statement not allow'问题

你的问题根源不是MySQL不支持多语句,而是Jeecg-boot内置的SQL安全拦截器默认禁止了多语句执行(防止SQL注入风险),即便开启了MySQL的allowMultiQueries=true也会被Jeecg的拦截器阻断。以下是三种解决方式:

方案一:改用单语句批量更新(推荐)

将多语句拼接改为单SQL的CASE WHEN批量更新,既规避多语句拦截,又提升执行效率,示例Mapper代码:

<update id="update">
    update eye_product_standard
    <trim prefix="set" suffixOverrides=",">
        <!-- 批量更新name字段 -->
        <trim prefix="name = case" suffix="end,">
            <foreach collection="views" item="item">
                <if test="item.name != null and item.name != ''">
                    when product_id = #{item.productId} and id = #{item.id} then #{item.sku}
                </if>
            </foreach>
        </trim>
        <!-- 批量更新inventory字段 -->
        <trim prefix="inventory = case" suffix="end,">
            <foreach collection="views" item="item">
                <if test="item.inventory != null">
                    when product_id = #{item.productId} and id = #{item.id} then #{item.inventory}
                </if>
            </foreach>
        </trim>
        <!-- 批量更新flag字段 -->
        <trim prefix="flag = case" suffix="end,">
            <foreach collection="views" item="item">
                <if test="item.flag != null">
                    when product_id = #{item.productId} and id = #{item.id} then #{item.flag}
                </if>
            </foreach>
        </trim>
        <!-- 批量更新skuPrice字段 -->
        <trim prefix="skuPrice = case" suffix="end">
            <foreach collection="views" item="item">
                <if test="item.price != null">
                    when product_id = #{item.productId} and id = #{item.id} then #{item.price}
                </if>
            </foreach>
        </trim>
    </trim>
    <!-- 限定更新范围 -->
    where (product_id, id) in
    <foreach collection="views" item="item" open="(" separator="," close=")">
        (#{item.productId}, #{item.id})
    </foreach>
</update>

方案二:配置Jeecg-boot允许多语句执行

如果必须使用多语句批量更新,需调整Jeecg的SQL拦截器配置,同时确保MySQL连接参数正确:

  1. 在application.yml中开启Jeecg允许多语句:
jeecg:
  sql-injector:
    enable: true
    allow-multi-statement: true # 全局允许多语句
    # 可选:仅给指定Mapper放行,更安全
    exclude-mappers:
      - org.jeecg.modules.demo.eyeserver.mapper.eye_product_viewMapper
  1. 确认MySQL数据源URL已添加allowMultiQueries=true(多数据源需给对应数据源配置):
spring:
  datasource:
    dynamic:
      datasource:
        master:
          url: jdbc:mysql://localhost:3306/jeecg-boot?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true

方案三:关闭Jeecg的SQL注入拦截器(不推荐)

仅临时测试使用,生产环境关闭会带来SQL注入风险:

jeecg:
  sql-injector:
    enable: false

内容的提问来源于stack exchange,提问作者tongxiaolan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 09:22:13