使用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连接参数正确:
- 在
application.yml中开启Jeecg允许多语句:
jeecg: sql-injector: enable: true allow-multi-statement: true # 全局允许多语句 # 可选:仅给指定Mapper放行,更安全 exclude-mappers: - org.jeecg.modules.demo.eyeserver.mapper.eye_product_viewMapper
- 确认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
相关产品推荐
相关产品推荐

