解决mybatis使用<foreach>批量insert异常的解决办法

2022-07-09 13:47:22

解决mybaties 中使用循环不成功的问题

    <foreach collection ="scoreRecordList" item="item" index= "index" separator =";">
        insert into  score_record(fk_theme_id, fk_target_id, fk_dimension_id, fk_voter, score, create_time)
        select
        #{item.fkThemeId},#{item.fkTargetId},#{item.fkDimensionId},#{item.fkVoter},#{item.score},#{item.createTime}
        from DUAL
        where not exists (select id from score_record
                            where fk_target_id=#{item.fkTargetId} and deleted=1 and fk_voter=#{item.fkVoter} and fk_dimension_id=#{item.fkDimensionId})
    </foreach >
</insert>

运行成功不报错,postman测试报错500
insert into score_record(fk_theme_id, fk_target_id, fk_dimension_id, fk_voter, score, create_time)
select
147392159398656,328,348,‘admin’,5,now()
from DUAL
where not exists (
select id from score_record
where fk_target_id=328 and deleted=1 and fk_voter=‘admin’ and fk_dimension_id = 348);

insert into score_record(fk_theme_id, fk_target_id, fk_dimension_id, fk_voter, score, create_time)
select
147392159398656,328,347,‘admin’,5,now()
from DUAL
where not exists (
select id from score_record
where fk_target_id=328 and deleted=1 and fk_voter=‘admin’ and fk_dimension_id = 347)

navicat测试,语句正确结果正确,后来发现是数据库对多个语句拼在一起的操作不支持问题
里插入图片在描述
allowMultiQueries=true 允许多个
1.可以在sql语句后携带分号,实现多语句执行。
2.可以执行批处理,同时发出多个SQL语句。

  • 作者:Chloe_RO
  • 原文链接:https://blog.csdn.net/weixin_43889487/article/details/120653811
    更新时间:2022-07-09 13:47:22