2020-02-07

Spring - Using JdbcTemplate

Spring’s JdbcTemplate and NamedParameterJdbcTemplate classes are auto-configured, and you can
@Autowire them directly into your own beans, as shown in the following example:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
private final JdbcTemplate jdbcTemplate;
@Autowired
public MyBean(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
// ...
}

You can customize some properties of the template by using the spring.jdbc.template.* properties,
as shown in the following example:
spring.jdbc.template.max-rows=500

The NamedParameterJdbcTemplate reuses the same JdbcTemplate instance behind the
scenes. If more than one JdbcTemplate is defined and no primary candidate exists,
the NamedParameterJdbcTemplate is not auto-configured.


No comments:

Post a Comment