为什么Spring的JDBC模板不使用表的默认值

Gle*_*eeb 9 java mysql sql spring jdbctemplate

我在MYSQL中有一个表,我使用JDBC模板在该表中插入.

其中一列有默认值,我没有在Map<String, Object> parameters地图中指定它.

我得到一个例外Column 'colName' cannot be null.

有人能解释一下吗?

谢谢

*编辑:代码*

contactDetailsInsertTemplate = new SimpleJdbcInsert( dataSource ).withTableName("contactdetails").usingGeneratedKeyColumns("contactcode");
Map<String, Object> parameters = new HashMap<String, Object>();
Number newId = contactDetailsInsertTemplate.executeAndReturnKey(parameters);
Run Code Online (Sandbox Code Playgroud)

Nic*_*ckJ 13

您需要限制SQL插入中指定的列.

请参见SimpleJdbcInsert.usingColumns()

如果不这样做,SQL语句将需要所有列的值,并且不能使用默认值.

例如使用

insert into mytable (col1, col2) values (val1, val2);
Run Code Online (Sandbox Code Playgroud)

代替

insert into mytable values (val1, val2);
Run Code Online (Sandbox Code Playgroud)