Spring mybatis 往mysql数据添加数据时报错:Cause: java.sql.SQLException: Field ‘id’ doesn’t have a default value
如果使用juit测试会报错:
org.springframework.dao.DataIntegrityViolationException:
Error updating database. Cause: java.sql.SQLException: Field ‘id’ doesn’t have a default value
The error may exist in file [F:\AOrcle\javaweb1\ssm\build\classes\com\jinghangzz\ssm\users\dao\AKingMapper.xml]
The error may involve com.jinghangzz.ssm.users.dao.IAKingDao.saveOne-Inline The error occurred while setting parameters
SQL: insert into a_king (dynaId,name,miaoHao,nianHao,age,status,createTime,updateTime,pubTime) values (?,?,?,?,?,?,?,?,?)
Cause: java.sql.SQLException: Field ‘id’ doesn’t have a default value
; Field ‘id’ doesn’t have a default value; nested exception is java.sql.SQLException: Field ‘id’ doesn’t have a default value
意思是字段id没有默认值,在往数据库写入数据时会主键是不用添加的会自动递增,但是如果没有设置自动递增,在写入数据是获取不到id的值,无法写入数据,让表的主键id设置成自动递增就可以了