MyBatis bulk insert operation Oracle ORA-00933: SQL command not properly ended

The foreach loop tag inserted, but inserted transported as bulk Mysql exception is generated

Wrong written as

<insert id="insertExpenseItem" parameterType="List" >
    insert into expenseItem values
    <foreach collection="list" item="item" separator="," >
        (
            seq_item.nextval,
            #{item.expId},
            #{item.type},
            #{item.amount},
            #{item.itemDesc}
        )
    </foreach>
</insert>

Captured SQL statement is as follows


- ==> Preparing: insert into expenseItem values ( seq_item.nextval, ?, ?, ?, ? ) , ( seq_item.nextval, ?, ?, ?, ? )
- ==> Parameters: 11(Integer), 1(String), 1111.0(Double), 2(String), 11(Integer), 3(String), 1111.0(Double), 2222(String)

Even if the statement is executed on a pl / sql still will complain! Analysis of this problem is probably the syntax of the Oracle

First, find the relevant information MyBatis of foreach inserted on the degree of your mother

details as follows:

        foreach is mainly used in building in conditions that can be iterated in a set of SQL statements.

        Foreach element attribute mainly item, index, collection, open, separator, close.

        Each item represents a set of elements alias iteration, index specify a name to represent the iterative process, each iteration to the position, open statement to indicate that what began, separator between each represents iterate what symbol as a delimiter, close representation to what end, when using foreach of the most critical and most error-prone is a collection property, which must be specified, but under different circumstances, the value of the property is not the same , there are about three cases:

        1. If one parameter is passed in the parameter type is a List and time, collection attribute value list

        2. If one parameter is passed and the time parameter is an array type array, collection attribute value array

        3. If the argument passed is more, we need to package them into a Map, of course, it can also be packaged as a single parameter map

Ever since there was no top brains, the wording wrong! !

MyBatis Oracle checked the operation of the relevant information

Conclusion: In Oracle versions, there are several points to note:

        In 1.SQL no VALUES;

        2. <foreach> (selece ..... from dual) tags;

        3. <foreach> Properties of separator Tags "UNION ALL", merging the query result set.

The correct wording is as follows:

<insert id="insertExpenseItem" parameterType="List">
        insert into expenseItem(itemId,expId,type,amount,itemDesc)
            select seq_item.nextval itemId,A.*
        from(
            <foreach collection="list" item="item" separator="union all" >
                select
                    #{item.expId} expId,
                    #{item.type} type,
                    #{item.amount} amount,
                    #{item.itemDesc} itemDesc
                from dual
            </foreach>
        )A
</insert>

The perfect solution!

PS:

  Note that the sequence can not write in foeach inside select inside! ! I can copy or imitate carried out in accordance with the correct wording

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160605.htm