需要在插入时加上useGeneratedKeys="false"
原Mapper:
<insert id="insertProductRoleUser" parameterType="list">
INSERT INTO AMC_PRODUCT_ROLE_USER
(user_id, role_id)
<foreach collection="list" item="productRole" index="index" separator="union all">
SELECT
#{productRole.user_id,jdbcType=VARCHAR},
#{productRole.role_id,jdbcType=VARCHAR}
FROM DUAL
</foreach>
</insert>新Mapper:
<insert id="insertProductRoleUser" parameterType="list" useGeneratedKeys="false">
INSERT INTO AMC_PRODUCT_ROLE_USER
(user_id, role_id)
<foreach collection="list" item="productRole" index="index" separator="union all">
SELECT
#{productRole.user_id,jdbcType=VARCHAR},
#{productRole.role_id,jdbcType=VARCHAR}
FROM DUAL
</foreach>
</insert>
本文介绍了一种在MyBatis中进行批量插入时的优化方法,通过在XML Mapper文件中加入useGeneratedKeys=false属性来避免因主键自动生成而导致的性能问题。这种方法特别适用于需要大量插入数据的场景。
9262

被折叠的 条评论
为什么被折叠?



