Using JdbcTemplate

Available

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
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
    private final JdbcTemplate jdbcTemplate;
    public MyBean(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    public void doSomething() {
        this.jdbcTemplate ...
    }
}
Kotlin
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Component
@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.
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet