mysql Column count doesn't match value count at row 1

时间:2023-02-10 20:08:44

今天执行批量插入的操作,发现报了错 mysql Column count doesn't match value count at row 1。

后来发现原因:是由于写的SQL语句里列的数目和后面的值的数目不一致, 比如insert into 表名 (field1,field2,field3) values('a','b')这样前面的是三列,后面却只有二个值,这就会出现这个错误的。 

mysql Column count doesn't match value count at row 1

查看mapper写法

 1 <insert id="insertBatch" useGeneratedKeys="true" parameterType="java.util.List">
 2  INSERT INTO sys_basic_info(  3     <include refid="dbColumns" />
 4  ) VALUES  5     <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
 6  #{item.groupId},  7  #{item.groupName},  8  #{item.groupCode}  9     </foreach>
10 </insert>

原因很容易发现,foreach标签属性open和close配置有误!这样大括号会把整体包起来,正确应为:

 1 <insert id="insertBatch" useGeneratedKeys="true" parameterType="java.util.List">
 2  INSERT INTO sys_basic_info(  3     <include refid="dbColumns" />
 4  ) VALUES  5     <foreach collection="list" item="item" index="index" separator=",">
 6  (  7  #{item.groupId},  8  #{item.groupName},  9  #{item.groupCode} 10  ) 11     </foreach>
12 </insert>