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
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)
Insert inside Mybatis foreach is not batch, this is a single (could become giant) SQL statement and that brings drawbacks:
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.
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>