1. 程式人生 > 其它 >mybatis 批量插入 Column count doesn‘t match value count at row 1

mybatis 批量插入 Column count doesn‘t match value count at row 1

mybatis 批量插入問題

錯誤的寫法:

INSERT INTO t_csm_customer_product(id, customer_code, product_code) values
<foreach collection="lists" close=")" open="(" index="i" item="cstProduct" separator=",">
    #{cstProduct.id},
    #{cstProduct.customerCode},
    #{cstProduct.productCode}
</foreach>

異常資訊 Error updating database...Cause: java.sql.SQLException: Column count doesn't match value count at row 1

列印sql後發現:

INSERT INTO table(id, name, value) values ( ?,?,?,?,?,?)

整個括號括起來了

我們想要的sql:

INSERT INTO table(id, name, value) values ( ?,?,?),(?,?,?)

調整xml為:

INSERT INTO t_csm_customer_product(id, customer_code, product_code) values
<foreach collection="lists" index="i" item="cstProduct" separator=",">
  (#{cstProduct.id},
  #{cstProduct.customerCode},
  #{cstProduct.productCode})
</foreach>

重點 foreach 內參數 缺少 ()