Geeks, please accept the hero post of 2021 Microsoft x Intel hacking contest>>>
Today, we performed the batch insert operation and found that an error MySQL column count doesn’t match value count at row 1 was reported
Later, it was found that the reason is that the number of columns in the SQL statement is inconsistent with the number of values after it, such as insert into table name (field1, field2, field3) values (‘a ‘,’b’), so that there are three columns before and only two values after it, which will cause this error
View mapper writing
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>
The reason is easy to find, foreach tag attribute open and close configuration error! In this way, braces will wrap the whole
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>