using foreach to do batch insert with mybatis

前端 未结 3 969
南方客
南方客 2021-01-03 15:16

I am using mybatis and i would like to insert an ArrayList to some table.
all right using foreach in mapper, well this ends up with oracle exception ORA_00933 .
t

相关标签:
3条回答
  • 2021-01-03 15:50

    Try extracting the foreach (and change the separator):

    <insert id="batchInsert" parameterType="java.util.List">
      <foreach collection="list" item="model" index="index" separator=";">
        insert into SYS_ROLES_PERMISSIONGROUP
        (role_id, permissiongroup_id)
        values   
        (#{model.role_id}, #{model.permissiongroup_id})
      </foreach>
    </insert>
    

    I think that your current code creates a new role of values for each element, but a single insert statement (which is not what you want, you want an insert for each element)

    0 讨论(0)
  • 2021-01-03 15:52

    Insert inside Mybatis foreach is not batch, this is a single (could become giant) SQL statement and that brings drawbacks:

    • some database such as Oracle here does not support.
    • in relevant cases: there will be a large number of records to insert and the database configured limit (by default around 2000 parameters per statement) will be hit, and eventually possibly DB stack error if the statement itself become too large.

    Iteration over the collection must not be done in the mybatis XML. Just execute a simple Insert statement in a Java Foreach loop. The most important thing is the session Executor type.

    SqlSession session = sessionFactory.openSession(ExecutorType.BATCH);
    for (Model model : list) {
        session.insert("insertStatement", model);
    }
    session.flushStatements();
    

    I event think that here it will be enough to use ExecutorType.REUSE without flushing statements.

    Unlike default ExecutorType.SIMPLE, the statement will be prepared once and executed for each record to insert.

    0 讨论(0)
  • 2021-01-03 16:10

    Oracle does not support

    insert into xxx values (xxx,xxx),(xxx,xxx)

    maybe you can use insert all like this

        <insert id="batchInsert">
        INSERT ALL
        <foreach collection="list" item="model">
            INTO
            SYS_ROLES_PERMISSIONGROUP (role_id, permissiongroup_id)
            VALUES
            (#{model.role_id}, #{model.permissiongroup_id})
        </foreach>
       </insert>
    
    0 讨论(0)
提交回复
热议问题