Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BatchSqlUpdate - how to get auto generated keys

I am using spring BatchSqlUpdate to insert a set of rows. How do I get the auto generated keys for all of the rows inserted?

When doing a single insert I get the keys like this -

SqlUpdate sqlUpdate = new SqlUpdate(dataSource, sqlTemplate.toString());
sqlUpdate.setReturnGeneratedKeys(true);
KeyHolder keyHolder = new GeneratedKeyHolder();
sqlUpdate.update(new Object[] {}, keyHolder);
return keyHolder.getKey().longValue();

Thanks!

like image 821
hese Avatar asked Jun 07 '11 22:06

hese


2 Answers

a template proc class:

public abstract class BatchPreparedStatementSetterWithKeyHolder<T> implements BatchPreparedStatementSetter {
    private final List<T> beans;

    /**
     * @param datas
     * @param returnGeneratedKeys true设置{@linkplain Statement#RETURN_GENERATED_KEYS}
     */
    public BatchPreparedStatementSetterWithKeyHolder(List<T> beans) {
        this.beans = beans;
    }


    @Override
    public void setValues(PreparedStatement ps, int i) throws SQLException {
        setValues(ps, beans.get(i));
    }

    @Override
    public final int getBatchSize() {
        return beans.size();
    }

    public void setPrimaryKey(KeyHolder keyHolder) {
        List<Map<String, Object>> keys = keyHolder.getKeyList();
        for (int i = 0, len = keys.size(); i < len; i++) {
            setPrimaryKey(keys.get(i), beans.get(i));
        }
    }

    protected abstract void setValues(PreparedStatement ps, T bean) throws SQLException;
    protected abstract void setPrimaryKey(Map<String, Object> primaryKey, T bean);
}

a util method batchupdate:

private static void generatedKeys(PreparedStatement ps, KeyHolder keyHolder) throws SQLException {
        List<Map<String, Object>> keys = keyHolder.getKeyList();
        ResultSet rs = ps.getGeneratedKeys();
        if (rs == null) return;

        try {
            keys.addAll(new RowMapperResultSetExtractor<Map<String, Object>>(new ColumnMapRowMapper(), 1).extractData(rs));
        } finally {
            UtilIO.close(rs);
        }
    }

    /**
     * 批量更新
     * @param jdbcTemplate
     * @param sql
     * @param pss
     * @param keyHolder 存储主键,如果要存储主键,就必须传入此对象
     * @return sql执行结果
     * @see JdbcTemplate#batchUpdate(String, org.springframework.jdbc.core.BatchPreparedStatementSetter)
     */
    public static <T> int[] batchUpdateWithKeyHolder(JdbcTemplate jdbcTemplate, final String sql, final BatchPreparedStatementSetterWithKeyHolder<T> pss) {
        return jdbcTemplate.execute(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                return con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            }
        }, new PreparedStatementCallback<int[]>() {
            @Override
            public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                try {
                    int batchSize = pss.getBatchSize();
                    InterruptibleBatchPreparedStatementSetter ipss =
                            (pss instanceof InterruptibleBatchPreparedStatementSetter ?
                            (InterruptibleBatchPreparedStatementSetter) pss : null);
                    int[] result;
                    KeyHolder keyHolder = new GeneratedKeyHolder();

                    try {
                        if (JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
                            for (int i = 0; i < batchSize; i++) {
                                pss.setValues(ps, i);
                                if (ipss != null && ipss.isBatchExhausted(i)) break;
                                ps.addBatch();
                            }
                            result = ps.executeBatch();

                            generatedKeys(ps, keyHolder);
                        } else {
                            List<Integer> rowsAffected = new ArrayList<Integer>();
                            for (int i = 0; i < batchSize; i++) {
                                pss.setValues(ps, i);
                                if (ipss != null && ipss.isBatchExhausted(i)) break;

                                rowsAffected.add(ps.executeUpdate());
                                generatedKeys(ps, keyHolder);
                            }

                            result = UtilObj.tointArray(rowsAffected);
                        }
                    } finally {
                        pss.setPrimaryKey(keyHolder);
                    }

                    return result;
                } finally {
                    if (pss instanceof ParameterDisposer) ((ParameterDisposer) pss).cleanupParameters();
                }
            }
        });
    }

e.x:

UtilJdbc.batchUpdateWithKeyHolder(jdbcTemplate,
                "insert into tbe_vm_node (creator_id, host, ssh_ip, ssh_user, ssh_passwd)" +
                " values (?, ?, ?, ?, ?)",
                new BatchPreparedStatementSetterWithKeyHolder<VmNode>(vmNodes) {
                    @Override
                    protected void setValues(PreparedStatement ps, VmNode vmNode) throws SQLException {
                        UtilJdbc.setValues(ps, vmNode.getCreatorId(), vmNode.getHost(),
                                vmNode.getSshIp(), vmNode.getSshUser(), vmNode.getSshPasswd());
                    }
                    @Override
                    protected void setPrimaryKey(Map<String, Object> primaryKey, VmNode vmNode) {
                        vmNode.setId((Long) primaryKey.get("abc"));
                    }
                });
like image 166
takeseem Avatar answered Sep 16 '22 21:09

takeseem


    KeyHolder keyHolder = new GeneratedKeyHolder();
    BatchSqlUpdate updateBatch = new BatchSqlUpdate();
    updateBatch.setSql("Some INSERT SQL");
    updateBatch.setJdbcTemplate(jdbcTemp);
    updateBatch.setReturnGeneratedKeys(true);
    updateBatch.updateByNamedParam( paramsMap ,keyHolder);
    updateBatch.flush();
like image 32
BrianC Avatar answered Sep 18 '22 21:09

BrianC