首页 > 解决方案 > 使用 SimpleJdbcInsert 插入没有 bean 的表中。

问题描述

我的代码:

SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(dwDatasource).withTableName("DW.MYTABLE1");
Map<String,Object> parameters1 = new HashMap<String,Object>();
parameters1.put("STRING2", "Entry2");
jdbcInsert.execute(parameters1);

我得到:

Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Unable to locate columns for table 'MYTABLE1' so an insert statement can't be generated

注意:如果我要在 jdbctemplate 上设置相同的数据源并执行

jdbcTemplate.update("INSERT INTO DW.MYTABLE1 (STRING2) VALUES ('Entry2'));

有用

标签: spring-bootspring-jdbc

解决方案


定义 SimpleJdbcInsert 时,除了表名,还需要指定列:

String[] columns = {"column1","column2"}

SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(dataSource)
                .usingColumns(columns)
                .withTableName(table);

推荐阅读