1.背景
mybatis批量插入的mapper
<insert id="saveOrder" parameterType="Order">
<!--增加主表数据-->
insert into t_order (
order_id,
order_money,
order_receiverinfo,
order_paystate,
user_id,
order_time
) values(#{orderId},#{orderMoney},#{orderReceiverinfo},
#{orderPaystate},#{userId},#{orderTime});
<!--新增从表数据-->
insert into t_order_item (
order_id,
product_id,
product_image,
product_name,
product_price,
num)
values
<foreach collection="orderItems" item="item" separator=",">
(#{orderId},#{item.productId},#{item.productImage},
#{item.productName},#{item.productPrice},#{item.num})
</foreach>
</insert>
2..问题
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into t_order_item (
order_id,
product_id,
product_image,
product_name' at line 11
3.原因
jdbc默认不支持批量操作
4.解决方案
在jdbcUrl上 拼接 allowMultiQueries=true 。表示允许批量操作。
仅供参考,希望对遇到同样问题的同学有帮助。