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 ( ?,?,?),(?,?,?),(?,?,?)......

 

 

问题在于close=")" open="(" 这个是指在整个参数的前后 加上括号,所以我们要去掉这个参数, 然后在循环内 分别加上括号

调整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>

 

posted @ 2021-12-10 09:11  loveCrane  阅读(232)  评论(0编辑  收藏  举报