【问题标题】:Insert batch and Return full object using NamedParameterJdbcTemplate使用 NamedParameterJdbcTemplate 插入批处理并返回完整对象
【发布时间】:2021-03-07 00:21:54
【问题描述】:

我正在尝试对表进行批量插入,然后使用新生成的ids 读取完整的对象。

    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);

但是,我还需要使用 ids 读回插入的值,但不确定我可以使用什么 namedParameterJdbcTemplate 方法。

TLDR: 我想做批量插入,然后使用namedParameterJdbcTemplate 读回插入的行,但找不到正确的方法。 namedParameterJdbcTemplate 是否在单一方法中提供批量插入和选择?

【问题讨论】:

    标签: spring postgresql spring-boot cockroachdb jdbcnamedparametertemplate


    【解决方案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.update 与 GeneratedKeyHolder 参数一起使用。这可能会对您有所帮助:identity from sql insert via jdbctemplate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多