The JdbcTemplate and NamedParameterJdbcTemplate classes from Spring are auto-configurable, and you can bind them via the @Autowire annotation directly to their own beans, as shown in the following example:
Java
@Component public class MyBean { private final JdbcTemplate jdbcTemplate; public MyBean(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void doSomething() { this.jdbcTemplate ... } }
Kotlin
@Component class MyBean(private val jdbcTemplate: JdbcTemplate) { fun doSomething() { jdbcTemplate.execute("delete from customer") } }
You can configure some template properties using the spring.jdbc.template.* properties, as shown in the following example:
Properties
spring.jdbc.template.max-rows=500
Yaml
spring:
jdbc:
template:
max-rows: 500
NamedParameterJdbcTemplate reuses the same
JdbcTemplate instance behind the scenes. If more than one
JdbcTemplate is defined and there is no primary candidate, the
NamedParameterJdbcTemplate will not be automatically configured.
GO TO FULL VERSION