我正在尝试使用Spring Boot(Java)通过MyBatis for IBM DB2执行多个INSERT语句。
以XML
<insert id="insertIntoTempTmpCopyTo" parameterType="map">
<foreach collection="list" item="lItem" separator=";">
<foreach collection="data.keyList" item="rec" separator=";">
<if test = "rec.sel and ((rec.keyType eq 'PPP'.toString())">
INSERT INTO SESSION.MP_COPYTO(VP_ENCRP_PRC_RL_ID, PROC_CD, VER_KEY_SET_CD, TRNSLT_TP_CD, SET_INDEX_NUM, SET_MDK_DKI_NUM, PACKET_NUM)
SELECT
'${lItem.EncrpRlId}' as ENCRP_RL_ID,
'${rec.systemTo}' as PROC_CD,
'${rec.keyType}' as KEY_SET_CD,
'${rec.trnsltTpCd}' as TRNSLT_CD,
'${rec.setTo}' as SET_INDEX,
'${rec.dkiTo}' AS SET_NUM,
<if test="lItem.packetNum != null">
${lItem.packetNum} AS P_NUM
</if>
<if test="lItem.packetNum == null">
0 AS P_NUM
</if>
FROM SYSIBM.SYSDUMMY1
</if>
</foreach>
</foreach>
;
</insert>
使用此功能,我可以创建多个插入语句
INSERT INTO SESSION.MP_COPYTO(ENCRP_RL_ID, PROC_CD, KEY_SET_CD, TRNSLT_CD, SET_INDEX, SET_NUM, P_NUM)
SELECT '500137' as ENCRP_RL_ID, 'DB' as PROC_CD, 'MDK' as KEY_SET_CD, '0' as TRNSLT_CD, '1' as SET_INDEX, '001' AS SET_NUM, 766456 AS P_NUM FROM SYSIBM.SYSDUMMY1 ;
INSERT INTO SESSION.MP_COPYTO(ENCRP_RL_ID, PROC_CD, KEY_SET_CD, TRNSLT_CD, SET_INDEX, SET_NUM, P_NUM)
SELECT '500137' as ENCRP_RL_ID, 'DB' as PROC_CD, 'MDK' as KEY_SET_CD, '0' as TRNSLT_CD, '2' as SET_INDEX, '002' AS SET_NUM, 766456 AS P_NUM FROM SYSIBM.SYSDUMMY1 ;
但是当我通过Spring Boot JAVA执行时,我只能插入一行
2019-05-24 09:36:51,094 DEBUG [SimpleAsyncTaskExecutor-1] : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@123536e]
2019-05-24 09:36:51,094 DEBUG [SimpleAsyncTaskExecutor-1] : Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@123536e] from current transaction
2019-05-24 09:36:51,118 DEBUG [SimpleAsyncTaskExecutor-1] : ==> Preparing: INSERT INTO SESSION.MP_COPYTO(ENCRP_RL_ID, PROC_CD, KEY_SET_CD, TRNSLT_CD, SET_INDEX, SET_NUM, P_NUM) SELECT '500137' as ENCRP_RL_ID, 'DB' as PROC_CD, 'MDK' as KEY_SET_CD, '0' as TRNSLT_CD, '1' as SET_INDEX, '001' AS SET_NUM, 766456 AS P_NUM FROM SYSIBM.SYSDUMMY1 ; INSERT INTO SESSION.MP_COPYTO(ENCRP_RL_ID, PROC_CD, KEY_SET_CD, TRNSLT_CD, SET_INDEX, SET_NUM, P_NUM) SELECT '500137' as ENCRP_RL_ID, 'DB' as PROC_CD, 'MDK' as KEY_SET_CD, '0' as TRNSLT_CD, '2' as SET_INDEX, '002' AS SET_NUM, 766456 AS P_NUM FROM SYSIBM.SYSDUMMY1 ;
2019-05-24 09:36:51,118 DEBUG [SimpleAsyncTaskExecutor-1] : ==> Parameters:
2019-05-24 09:36:51,158 DEBUG [SimpleAsyncTaskExecutor-1] : <== Updates: 1
我很困惑为什么会这样。我正在寻找一个将allowMultipleQueries属性设置为true的地方,但找不到。我有什么办法可以在运行时设置它。
那是我能做的事。
任何输入都会有所帮助。
谢谢!
答案 0 :(得分:0)
您没有指定版本,但是Db2可能支持多行插入语法。
不过,根据this thread,似乎有些变化。
我刚刚使用Db2 LUW 11测试了以下语法,并且可以正常工作。
INSERT INTO table (col1, col2) VALUES (?, ?), (?, ?)
使用此语法,该语句可以编写如下:
<insert id="insertIntoTempTmpCopyTo" parameterType="map">
INSERT INTO SESSION.MP_COPYTO(VP_ENCRP_PRC_RL_ID,
PROC_CD, VER_KEY_SET_CD, TRNSLT_TP_CD,
SET_INDEX_NUM, SET_MDK_DKI_NUM, PACKET_NUM) VALUES
<trim suffixOverrides=",">
<foreach collection="list" item="lItem">
<foreach collection="data.keyList" item="rec">
<if test="rec.sel and (rec.keyType eq 'PPP'.toString())">
(#{lItem.EncrpRlId}, #{rec.systemTo}, #{rec.keyType},
#{rec.trnsltTpCd}, #{rec.setTo}, #{rec.dkiTo},
<if test="lItem.packetNum != null">
#{lItem.packetNum}
</if>
<if test="lItem.packetNum == null">
0
</if>
),
</if>
</foreach>
</foreach>
</trim>
</insert>
<trim />
元素删除了多余的逗号。 #{}
而不是${}
。请参阅FAQ。答案 1 :(得分:0)
我对IBM DB2不熟悉,但是您可以将mapper.xml更改为类似的内容
<insert id="insertIntoTempTmpCopyTo" parameterType="map">
INSERT ALL
<foreach collection="list" item="element" index="index" >
<foreach collection="data.keyList" item="rec">
INTO SESSION.MP_COPYTO(VP_ENCRP_PRC_RL_ID, PROC_CD, VER_KEY_SET_CD, TRNSLT_TP_CD, SET_INDEX_NUM, SET_MDK_DKI_NUM, PACKET_NUM)
VALUES
(
#{lItem.EncrpRlId},
#{rec.systemTo},
#{rec.keyType},
#{rec.trnsltTpCd},
#{rec.setTo},
#{rec.dkiTo},
<choose>
<when test="lItem.packetNum != null">
#{lItem.packetNum}
</when>
<otherwise>
0
</otherwise>
</choose>
)
</foreach>
</foreach>
SELECT * FROM dual
</insert>
</mapper>
最后的SELECT * FROM dual
非常重要
或者您将mapper.xml更改为
<insert id="insertIntoTempTmpCopyTo" parameterType="map">
-- Change the delimiter from ';' to '§'
-- @DELIMITER §
-- removed separator=";"
<foreach collection="list" item="lItem">
-- removed separator=";"
<foreach collection="data.keyList" item="rec">
<if test = "rec.sel and ((rec.keyType eq 'PPP'.toString())">
INSERT INTO SESSION.MP_COPYTO(VP_ENCRP_PRC_RL_ID, PROC_CD, VER_KEY_SET_CD, TRNSLT_TP_CD, SET_INDEX_NUM, SET_MDK_DKI_NUM, PACKET_NUM)
SELECT
#{lItem.EncrpRlId} AS ENCRP_RL_ID,
#{rec.systemTo} AS PROC_CD,
#{rec.keyType} AS KEY_SET_CD,
#{rec.trnsltTpCd} AS TRNSLT_CD,
#{rec.setTo} AS SET_INDEX,
#{rec.dkiTo} AS SET_NUM,
<choose>
<when test="lItem.packetNum != null">
#{lItem.packetNum} AS P_NUM
</when>
<otherwise>
0 AS P_NUM
</otherwise>
</choose>
FROM SYSIBM.SYSDUMMY1
-- separator inserted
;
</if>
</foreach>
</foreach>
-- Change the delimiter back to ';'
-- @DELIMITER ;
</insert>
通过将mybatis-delimiter从;
更改为其他内容,该内容不会出现在您的语句中,就像§
一样,您可以执行以分号分隔的sql脚本