mybatis foreach 但提交单独的插入
mybatis foreach but submiting individual inserts
以下 mybatis 映射适用于我们支持的所有数据库,但一个除外。这是因为该数据库不允许批量插入方法(Intersystems Cache)。因此,我想提交单独的插入语句而不是一个。我如何构造这个 mybatis 语句,以便它仍然从我的 java.util.List 中读取,但它执行多个插入?
<insert id="bulkInsert" parameterType="java.util.List" >
<foreach collection="list" item="resource" index="index">
INSERT INTO ${prefix}ACT_APP_DEPLOYMENT_RESOURCE(ID_, NAME_, RESOURCE_BYTES_, DEPLOYMENT_ID_) VALUES
(#{resource.id, jdbcType=VARCHAR},
#{resource.name, jdbcType=VARCHAR},
#{resource.bytes, jdbcType=${blobType}},
#{resource.deploymentId, jdbcType=VARCHAR})
</foreach>
</insert>
如果您使用的是 java 版本 8+,您可以像这样在映射器中使用默认方法:
interface MyMapper {
void insertResource(@Param("resource") MyResource resource);
default void bulkInsert(List<MyResource> resources) {
for(MyResource resource:resources) {
insertResource(resource);
}
}
}
并修改映射器xml:
<insert id="insertResource">
INSERT INTO ${prefix}ACT_APP_DEPLOYMENT_RESOURCE(ID_, NAME_, RESOURCE_BYTES_, DEPLOYMENT_ID_) VALUES
(#{resource.id, jdbcType=VARCHAR},
#{resource.name, jdbcType=VARCHAR},
#{resource.bytes, jdbcType=${blobType}},
#{resource.deploymentId, jdbcType=VARCHAR})
</insert>
首先,检查。
另外根据mybatis doc:
The one parameter that might be new to you is ExecutorType. This enumeration defines 3 values:
ExecutorType.SIMPLE: This type of executor does nothing special. It creates a new PreparedStatement for each execution of a statement.
ExecutorType.REUSE: This type of executor will reuse PreparedStatements.
ExecutorType.BATCH: This executor will batch all update statements and demarcate them as necessary if SELECTs are executed between them, to ensure an easy-to-understand behavior.
默认为ExecutorType.SIMPLE,您需要将其更改为ExecutorType.BATCH。
以下 mybatis 映射适用于我们支持的所有数据库,但一个除外。这是因为该数据库不允许批量插入方法(Intersystems Cache)。因此,我想提交单独的插入语句而不是一个。我如何构造这个 mybatis 语句,以便它仍然从我的 java.util.List 中读取,但它执行多个插入?
<insert id="bulkInsert" parameterType="java.util.List" >
<foreach collection="list" item="resource" index="index">
INSERT INTO ${prefix}ACT_APP_DEPLOYMENT_RESOURCE(ID_, NAME_, RESOURCE_BYTES_, DEPLOYMENT_ID_) VALUES
(#{resource.id, jdbcType=VARCHAR},
#{resource.name, jdbcType=VARCHAR},
#{resource.bytes, jdbcType=${blobType}},
#{resource.deploymentId, jdbcType=VARCHAR})
</foreach>
</insert>
如果您使用的是 java 版本 8+,您可以像这样在映射器中使用默认方法:
interface MyMapper {
void insertResource(@Param("resource") MyResource resource);
default void bulkInsert(List<MyResource> resources) {
for(MyResource resource:resources) {
insertResource(resource);
}
}
}
并修改映射器xml:
<insert id="insertResource">
INSERT INTO ${prefix}ACT_APP_DEPLOYMENT_RESOURCE(ID_, NAME_, RESOURCE_BYTES_, DEPLOYMENT_ID_) VALUES
(#{resource.id, jdbcType=VARCHAR},
#{resource.name, jdbcType=VARCHAR},
#{resource.bytes, jdbcType=${blobType}},
#{resource.deploymentId, jdbcType=VARCHAR})
</insert>
首先,检查
另外根据mybatis doc:
The one parameter that might be new to you is ExecutorType. This enumeration defines 3 values:
ExecutorType.SIMPLE: This type of executor does nothing special. It creates a new PreparedStatement for each execution of a statement.
ExecutorType.REUSE: This type of executor will reuse PreparedStatements.
ExecutorType.BATCH: This executor will batch all update statements and demarcate them as necessary if SELECTs are executed between them, to ensure an easy-to-understand behavior.
默认为ExecutorType.SIMPLE,您需要将其更改为ExecutorType.BATCH。