Java 使用RETURN\u generated\u KEYS标记的jdbcOperations.execute语句以什么顺序返回生成的ID?

Java 使用RETURN\u generated\u KEYS标记的jdbcOperations.execute语句以什么顺序返回生成的ID?,java,mysql,spring,jdbc,spring-batch,Java,Mysql,Spring,Jdbc,Spring Batch,在官方文档中,我没有发现任何关于jdbcOperations.execute方法中返回的生成ID顺序的内容。虽然知道顺序对我来说很重要 我有一个方法,它接受一个没有ID的对象列表,这些对象应该在生成之后填充。如果生成的ID以与对象列表中相同的顺序返回,那么我可以迭代两个列表并将ID分配给对象。否则,我需要使用返回的ID执行另一个select查询,这将影响我的性能 以下是此类方法的示例: public static List<Long> executeBatchInsert(final

在官方文档中,我没有发现任何关于jdbcOperations.execute方法中返回的生成ID顺序的内容。虽然知道顺序对我来说很重要

我有一个方法,它接受一个没有ID的对象列表,这些对象应该在生成之后填充。如果生成的ID以与对象列表中相同的顺序返回,那么我可以迭代两个列表并将ID分配给对象。否则,我需要使用返回的ID执行另一个select查询,这将影响我的性能

以下是此类方法的示例:

public static List<Long> executeBatchInsert(final ParsedSql parsedSql, final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {

    if (batchArgs.length <= 0) {
        return Lists.newArrayList();
    }

    String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);

    BatchPreparedStatementSetter pss = new BatchPreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
            int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]);
            setStatementParameters(values, ps, columnTypes);
        }

        @Override
        public int getBatchSize() {
            return batchArgs.length;
        }
    };

    return jdbcOperations.execute(
            con -> con.prepareStatement(sqlToUse, Statement.RETURN_GENERATED_KEYS),
            (PreparedStatementCallback<List<Long>>) ps -> {
                int batchSize = pss.getBatchSize();
                for (int i = 0; i < batchSize; i++) {
                    pss.setValues(ps, i);
                    ps.addBatch();
                }
                ps.executeBatch();

                List<Long> result = new ArrayList<>();

                // Here I need to be sure that keys order is the same as in SqlParameterSource[] 
                ResultSet keys = ps.getGeneratedKeys();
                while (keys.next()) {
                    result.add(keys.getLong(1));
                }
                return result;
            });
}
public static List executeBatchInsert(final ParsedSql ParsedSql,final SqlParameterSource[]batchArgs,jdbc操作jdbc操作){
if(batchArgs.length con.prepareStatement(sqlToUse,Statement.RETURN\u生成的\u键),
(PreparedStatementCallback)ps->{
int batchSize=pss.getBatchSize();
对于(int i=0;i
我怀疑没有保证的顺序,这完全取决于DB实现。请注意,JDBC规范不要求支持批执行的
返回生成的\u键
,但通常我希望,如果驱动程序支持,该顺序与批执行的顺序相同。因此,第一个生成的keys行与批处理的第一组值匹配。