Insert a list of objects using MyBatis 3
- by T Soares
I've tried to insert a list in a database but I've got some the error: org.springframework.jdbc.BadSqlGrammarException: SqlSession operation; bad SQL grammar []; nested exception is java.sql.SQLException: ORA-00913: too many values (...).
The code that I've used:
<insert id="insertListMyObject" parameterType="java.util.List" >
INSERT INTO my_table
(ID_ITEM,
ATT1,
ATT2)
VALUES
<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
#{item.idItem, jdbcType=BIGINT},
#{item.att1, jdbcType=INTEGER},
#{item.att2, jdbcType=STRING}
</foreach>
</insert>
My dao cals the method:
SqlSessionTemplate().insert(MAPPER+".insertListMyObject", parameterList);
Where the parameterList is:
List<MyObjects>.
Does someone have a clue about what's this error? Or if does exists a better way to do multiples inserts operation.
Many thanks!