link:/src/test/java/io/dibog/spring/jdbc/QueryTest.kt[role=include]
-
JdbcTemplate::queryForObject
expects to return one entity. It will throw anEmptyResultDataAccessException
if the query returns no entity orIncorrectResultSizeDataAccessException
if it throws more than one entity. -
The SQL statement for the prepared statement.
-
Parameter array for the prepared statement.
-
The
RowMapper<T>
to be used to transform the result set into an entity.
link:/src/test/java/io/dibog/spring/jdbc/QueryTest.kt[role=include]
-
JdbcTemplate::query
expects to return zero to many entities. -
The SQL statement for the prepared statement.
-
The
RowMapper<T>
to be used to transform the result set into an entity.
There is also another version of this method which takes an parameter array to parameterize the prepared statement.
link:/src/test/java/io/dibog/spring/jdbc/QueryTest.kt[role=include]
-
JdbcTemplate::queryForList
expects to return zero to more rows with one attribute. -
The SQL statement for the prepared statement selecting just one attribute.
-
The kotlin type of the attribute to be part of the result list.
In the following examples will be showed which inserts new rows into a table
where the ID
attribute is automatically generated and returned. Returning
of generated keys is not supported for all JDBC drivers, so use it with care.
link:/src/test/java/io/dibog/spring/jdbc/InsertingEntitiesWithGeneratedIDsTest.kt[role=include]
-
To be able to create the prepare statement ourself we need to use this method of
JdbcTemplate
to get a jdbc connection. -
This constructor calls creates a prepared statement which can return generated attributes of the insert. You can either specify
Statement.RETURN_GENERATED_KEYS
to return any generated key of the insert, or you can specify a StringArray containing the column names or an IntArray containing the column indices of the returned generated key columns. -
The prepared statement parameters have to be set the usual jdbc way.
-
The returned
Int
should contain the modified rows within the table. In the case of a single insert it should be1
. -
The extension method
singleGeneratedKey
returns the generated key of the columnID
. ThesingleGeneratedKey
method can be found in as extension method in the current project.
link:/src/test/java/io/dibog/spring/jdbc/InsertingEntitiesWithGeneratedIDsTest.kt[role=include]
-
This object will contain after the query the generated key.
-
Again this method is required to access the jdbc connection.
-
Same as before we need to create the prepared statement with the correct constructor call.
-
Again the parameters for the prepared statement have to be set the jdbc way.
-
The keyholder into which the generated key will be inserted.
-
After the call the to the JdbcTemplate query the key can be accessed via the keyholder.
Both methods seems to be of the same length or complexity.
The batch insert returning generated keys is not directly supported by the JdbcTemplate
. In this project
you can find an extension function which supports the GeneratedKeyHolder
of Spring.
link:/src/test/java/io/dibog/spring/jdbc/InsertingEntitiesWithGeneratedIDsTest.kt[role=include]
-
To be able to create the prepare statement ourself we need to use this method of
JdbcTemplate
to get a jdbc connection. -
This constructor calls creates a prepared statement which can return generated attributes of the insert. You can either specify
Statement.RETURN_GENERATED_KEYS
to return any generated key of the insert, or you can specify a StringArray containing the column names or an IntArray containing the column indices of the returned generated key columns. -
The prepared statement parameters have to be set the usual jdbc way.
-
The returned
Int
should contain the modified rows within the table. In the case of a single insert it should be1
. -
The extension method
singleGeneratedKey
returns the generated key of the columnID
. ThesingleGeneratedKey
method can be found in as extension method in the current project.
link:/src/test/java/io/dibog/spring/jdbc/InsertingEntitiesWithGeneratedIDsTest.kt[role=include]
-
To be able to create the prepare statement ourself we need to use this method of
JdbcTemplate
to get a jdbc connection. -
This constructor calls creates a prepared statement which can return generated attributes of the insert. You can either specify
Statement.RETURN_GENERATED_KEYS
to return any generated key of the insert, or you can specify a StringArray containing the column names or an IntArray containing the column indices of the returned generated key columns. -
The prepared statement parameters have to be set the usual jdbc way.
-
The returned
IntArray
should contain the modified rows within the table. -
The extension method
generatedKeys
returns the generated key of the columnID
as a List. ThegeneratedKeys
method can be found in as extension method in the current project.