MySQL报错: java.sql.SQLException: Column count doesn't match value count at row 1

项目使用了Spring和MyBatis,在往mysql数据库里插入数据时报错如下:

 

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: java.sql.SQLException: Column count doesn't match value count at row 1
### The error may involve com.chenzhou.examples.erm.dao.addRecord-Inline
### The error occurred while setting parameters
### Cause: java.sql.SQLException: Column count doesn't match value count at row 1
; bad SQL grammar []; nested exception is java.sql.SQLException: Column count doesn't match value count at row 1
	at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:98)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:71)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358)
	at $Proxy6.insert(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:232)
	at com.chenzhou.examples.erm.dao.MySqlBaseDAO.insert(MySqlBaseDAO.java:45)
	at com.chenzhou.examples.erm.dao.TrainRecordDAO.addRecord(TrainRecordDAO.java:40)
	at com.chenzhou.examples.erm.service.impl.TrainRecordServiceImpl.addRecord(TrainRecordServiceImpl.java:45)
……

排查后发现是由于mapper.xml中insert操作的SQL语句里列的数目和后面值的数目不一致

<insert id="addRecord" useGeneratedKeys="true" parameterType="TrainRecord">
	<![CDATA[
		insert into t_train_record (add_time,emp_id,activity_id) 
		values 
		(#{addTime},#{empId},#{activityId},#{flag})
	]]>
</insert>

如上所示,values后面的值其中有一个flag,但是在values前面的括号中没有声明

在前面括号中加上之后就好了,代码如下:

<insert id="addRecord" useGeneratedKeys="true" parameterType="TrainRecord">
	<![CDATA[
		insert into t_train_record (add_time,emp_id,activity_id,flag) 
		values 
		(#{addTime},#{empId},#{activityId},#{flag})
	]]>
</insert>

 

 

你可能感兴趣的:(mysql,exception,数据库,mybatis,错误)