Mybatis 批量插入 Oracle 语法:
第一种:
mybatis的mapper的sql配置如下
<insert id="addList" parameterType="java.util.List" useGeneratedKeys="false">
INSERT ALL
<foreach item="item" index="index" collection="list">
INTO T_APPLAUD
(
ID,
USER_ID,
BUSINESS_TYPE,
PRODUCT_ID,
CREATE_TIME
) VALUES
(
#{item.id, jdbcType=NUMERIC},
#{item.userId, jdbcType=VARCHAR},
#{item.businessType, jdbcType=VARCHAR},
#{item.productId, jdbcType=VARCHAR},
#{item.createdTime, jdbcType=NUMERIC}
)
</foreach>
SELECT 1 FROM DUAL
</insert>
第二种:
insert into table(…) (select … from dual) union all (select … from dual)
<insert id="addList" parameterType="java.util.List" useGeneratedKeys="false">
INSERT INTO T_APPLAUD
(
ID,
USER_ID,
BUSINESS_TYPE,
PRODUCT_ID,
CREATE_TIME
)
<foreach item="item" index="index" collection="list" separator="union all">
(
SELECT
#{item.id},
#{item.userId},
#{item.businessType},
#{item.productId},
#{item.createdTime}
FROM DUAL
)
</foreach>
</insert>
解决错误: ORA-00933: SQL 命令未正确结束“ 错误解决方法
谷歌无数次,自己跟踪调试也没结果,最后在stackoverflow
mybatis问答区偶然看到了网友分析(链接见末尾),测试发现解决了,原因竟是mybatis批量插入oracle时需要显式指定为
useGeneratedKeys=“false” 不然报错~~