我正在尝试对表进行批量插入,然后使用新生成id的 s 读回完整的对象。
private List<Customer> saveCustomer(List<Customer> customerList, Long shopId) {
AtomicInteger index = new AtomicInteger();
SqlParameterSource[] paramsArray = new MapSqlParameterSource[customerList.size()];
for (Customer customer : customerList) {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("shop_id", shopId);
params.addValue("customer_name", pallet.getName());
params.addValue("email", pallet.getEmail());
params.addValue("contact_number", pallet.getContactNumber());
paramsArray[index.getAndIncrement()] = params;
}
String sql =
"INSERT INTO \"Customer\" " +
"(shop_id, customer_name, email, contact_number) " +
"VALUES (:shop_id, :customer_name, :email, :contact_number) " +
"RETURNING id, shop_id, customer_name, email, contact_number ";
return namedParameterJdbcTemplate.getJdbcOperations().query(sql, paramsArray, new CustomerRowMapper());
}
但是,这种方法给了我以下错误:org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.springframework.jdbc.core.namedparam.MapSqlParameterSource. Use setObject() with an explicit Types value to specify the type to use. 请参阅下面的堆栈跟踪。
PreparedStatementCallback; bad SQL grammar [INSERT INTO "Customer" (shop_id, customer_name, email, contact_number) VALUES (:shop_id, :customer_name, :email, :contact_number) RETURNING id, shop_id, customer_name, email, contact_number ]; nested exception is org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.springframework.jdbc.core.namedparam.MapSqlParameterSource. Use setObject() with an explicit Types value to specify the type to use.
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO "Customer" (shop_id, customer_name, email, contact_number) VALUES (:shop_id, :customer_name, :email, :contact_number) RETURNING id, shop_id, customer_name, email, contact_number ]; nested exception is org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.springframework.jdbc.core.namedparam.MapSqlParameterSource. Use setObject() with an explicit Types value to specify the type to use.
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1443)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:669)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:700)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:712)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:763)
如果我只想批量插入而不读回它,一切都会好起来的。然后我会用
namedParameterJdbcTemplate.batchUpdate(sql, paramsArray);
但是,我还需要用它们id的 s 读回插入的值,但不确定namedParameterJdbcTemplate我可以使用什么方法。
TLDR:我想做批量插入,然后使用namedParameterJdbcTemplate但找不到正确的方法来读取插入的行。是否namedParameterJdbcTemplate以单一方法提供批量插入和选择?