1

我正在尝试对表进行批量插入,然后使用新生成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以单一方法提供批量插入和选择?

4

1 回答 1

-1

从您的方法中可以看出,namedParameterJdbcTemplate您无法执行批处理操作并等待返回。您可以做的是在 1 个 sql 请求中执行语句。如果您的数据库支持这样的语法,只需组合您的值:

INSERT INTO Customer (shop_id, customer_name, email, contact_number)
VALUES
    (value_list_1),
    (value_list_2),
    ...
    (value_list_n);

然后只需使用JDBCTemplate.updateGeneratedKeyHolder 参数。这可能会对您有所帮助:通过 jdbctemplate 来自 sql 插入的身份

于 2020-11-24T09:25:39.400 回答