JDBC
Configuration
JDBC Template requires a DataSource.
It’s thread-safe there the same object can be reused as a singleton.
Every-time a JdbcTemplate is used for executing a SQL query, it requests a connection from the datasource.
Queries
Return type | Name | |
<> | query | generic query |
<T> | queryForObject | retrieves an object (can use RowMapper) |
Map<String,Object> | queryForMap | retrieves just one object properties in a map. |
List<Map<String, Object>> | queryForList | retrieves just multiple object properties in a map. |
int | update | execute insert, update or delete, returns number for rows affected |
void | execute | preferable method to execute DDL statements |
Callbacks
RowMapper<T> | mapRow<ResultSet, rowno> |
ResultSetExtractor<T> | extractData(ResultSet) |
RowCallbackHandler | processRow(ResultSet) |
Tests
@Sql is used to annotate a test class or test method to configure SQL scripts to be run against a given database during integration tests.
@Test
@Sql({"/test-schema.sql", "/test-user-data.sql"})
void userTest() {
// run code that relies on the test schema and test data
}
@SqlConfig
defines metadata that is used to determine how to parse and run SQL scripts configured with the @Sql
annotation.
@Test
@Sql(
scripts = "/test-user-data.sql",
config = @SqlConfig(commentPrefix = "`", separator = "@@")
)
void userTest() {
// run code that relies on the test data
}