identity from sql insert via jdbctemplate

前端 未结 4 1454
渐次进展
渐次进展 2020-12-02 10:15

Is it possible to get the @@identity from the SQL insert on a Spring jdbc template call? If so, how?

4条回答
  •  醉酒成梦
    2020-12-02 10:51

    How about SimpleJdbcInsert.executeAndReturnKey? It takes two forms, depending on the input:

    (1) The input is a Map

    public java.lang.Number executeAndReturnKey(java.util.Map args)

    Description copied from interface: SimpleJdbcInsertOperations

    Execute the insert using the values passed in and return the generated key. This requires that the name of the columns with auto generated keys have been specified. This method will always return a KeyHolder but the caller must verify that it actually contains the generated keys.

    Specified by:

    executeAndReturnKey in interface SimpleJdbcInsertOperations

    Parameters:

    args - Map containing column names and corresponding value

    Returns:

    the generated key value

    (2) The input is a SqlParameterSource

    public java.lang.Number executeAndReturnKey(SqlParameterSourceparameterSource)

    Description copied from interface: SimpleJdbcInsertOperations

    Execute the insert using the values passed in and return the generated key. This requires that the name of the columns with auto generated keys have been specified. This method will always return a KeyHolder but the caller must verify that it actually contains the generated keys.

    Specified by:

    executeAndReturnKey in interface SimpleJdbcInsertOperations

    Parameters:

    parameterSource - SqlParameterSource containing values to use for insert

    Returns:

    the generated key value.

提交回复
热议问题