Mybatis combined with Oracle's foreach insert batch insert

Recently, I have been migrating the mysql database project to the oracle database project, and found that some sql is incompatible, such as batch insert in mysql written as follows, mybatis configuration file

xxxMapper.xml file:

 

	<insert id="batchInsert" parameterType="List">
		INSERT INTO USER_ANSWER (
			USER_ANSWER_ID,USER_SERVEY_ID,QUESTION_ID,OPTION_ID,ADD_DATE
		)
		VALUES
		<foreach collection="list" item="record" index="index" separator=",">
			(#{record.USER_ANSWER_ID}, #{record.USER_SERVEY_ID}, #{record.QUESTION_ID}, #{record.OPTION_ID}, #{record.ADD_DATE})
		</foreach>
	</insert>

 

 

        But after running it, it keeps reporting an error, and the error message is as follows:

 

### SQL: INSERT INTO USER_ANSWER (    USER_ANSWER_ID,USER_SERVEY_ID,QUESTION_ID,OPTION_ID,ADD_DATE   )   VALUES         (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)    ,     (?, ?, ?, ?, ?)
### Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command did not end properly

; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00933: SQL command did not end properly
] with root cause
java.sql.SQLSyntaxErrorException: ORA-00933: SQL command did not end properly

 

        Copying the SQL and running it in PL/SQL also reports the same error. As can be seen from the above, the SQL statement executed using batch insert is equivalent to:

INSERT INTO USER_ANSWER (
USER_ANSWER_ID,USER_SERVEY_ID,QUESTION_ID,OPTION_ID,ADD_DATE
)VALUES (?,?,?,?,?),(?,?,?,?,?)

 In oracle, the syntax of insert into xxx values ​​(xxx, xxx), (xxx, xxx) cannot pass.

It is found that this only applies to MySQL, not Oracle, so modify the xml file:

	<insert id="batchInsert" parameterType="List">
		INSERT INTO USER_ANSWER (
			USER_ANSWER_ID,USER_SERVEY_ID,QUESTION_ID,OPTION_ID,ADD_DATE
		)
		SELECT A.*
		FROM (
			<foreach collection="list" item="record" index="index" separator="UNION ALL">
				SELECT
				#{record.USER_ANSWER_ID}, #{record.USER_SERVEY_ID}, #{record.QUESTION_ID}, #{record.OPTION_ID},
				#{record.ADD_DATE}
				FROM dual
			</foreach>
		)A
	</insert>

 

        run through. In the Oracle version, there are a few things to note:

        1. There is no VALUES in SQL;

        2. (selece ..... from dual) in the <foreach> tag;

        3. The attribute of the separator in the <foreach> tag is "UNION ALL", and the query will be merged into the result set.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326020979&siteId=291194637