CodeGym /Courses /Module 5. Spring /Using JdbcTemplate

Using JdbcTemplate

Module 5. Spring
Level 17 , Lesson 1
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
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION