为什么Spring的JDBC模板不使用表的默认值
16lz
2021-01-22
I have a table in MYSQL and I am using JDBC Templates to do an insert into that table.
我在MYSQL中有一个表,我使用JDBC模板在该表中插入。
One of the columns has a default value, and I am not specifying it in the Map<String, Object> parameters
map.
其中一列有一个默认值,我没有在Map
I am getting an exception Column 'colName' cannot be null
.
我收到异常列'colName'不能为null。
Can anyone explain this please?
有人能解释一下吗?
Thanks
*Edit: code *
*编辑:代码*
contactDetailsInsertTemplate = new SimpleJdbcInsert( dataSource ).withTableName("contactdetails").usingGeneratedKeyColumns("contactcode"); Map<String, Object> parameters = new HashMap<String, Object>(); Number newId = contactDetailsInsertTemplate.executeAndReturnKey(parameters);
4 个解决方案
#1
12
You need to limit the columns specified in the SQL Insert.
您需要限制SQL Insert中指定的列。
See SimpleJdbcInsert.usingColumns()
If you don't do this, the SQL statement will need values for ALL columns and the default values cannot be used.
如果不这样做,SQL语句将需要所有列的值,并且不能使用默认值。
e.g. use
insert into mytable (col1, col2) values (val1, val2);
instead of
insert into mytable values (val1, val2);
更多相关文章
- 准备语句SQL异常“无输入参数”
- java7 switch语句使用字符串
- java通过poi模板导出excel
- 如何通过Java将Digital Persona的模板保存到数据库中
- Java日志框架——查看“完整的执行的SQL语句”