From 5ea3ab4595dffe60bb38b49126f32960cef2d4b8 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 1 Jun 2017 10:48:25 +0200 Subject: [PATCH] Polish "Allow to customize the JdbcTemplate" Closes gh-7960 --- .../autoconfigure/jdbc/JdbcProperties.java | 38 +++++++++---------- .../jdbc/JdbcTemplateAutoConfiguration.java | 13 ++----- .../appendix-application-properties.adoc | 5 +++ .../main/asciidoc/spring-boot-features.adoc | 9 +++++ 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java index 21ac651fc5d..ec5a8fd6a04 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java @@ -22,14 +22,12 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * Configuration properties for JDBC. * * @author Kazuki Shimizu - * @since 1.5.0 + * @author Stephane Nicoll + * @since 2.0.0 */ @ConfigurationProperties(prefix = "spring.jdbc") public class JdbcProperties { - /** - * Settings for JdbcTemplate. - */ private final Template template = new Template(); public Template getTemplate() { @@ -37,51 +35,51 @@ public class JdbcProperties { } /** - * {@link org.springframework.jdbc.core.JdbcTemplate} settings. + * {@code JdbcTemplate} settings. */ public static class Template { /** - * Fetch size. - * @see org.springframework.jdbc.core.JdbcTemplate#fetchSize + * Number of rows that should be fetched from the database when more rows are + * needed. Use -1 to use the JDBC driver's default configuration. */ - private Integer fetchSize; + private int fetchSize = -1; /** - * Max row count. - * @see org.springframework.jdbc.core.JdbcTemplate#maxRows + * Maximum number of rows. Use -1 to use the JDBC driver's default configuration. */ - private Integer maxRows; + private int maxRows = -1; /** - * Query timeout in seconds. - * @see org.springframework.jdbc.core.JdbcTemplate#queryTimeout + * Query timeout in seconds. Use -1 to use the JDBC driver's default + * configuration. */ - private Integer queryTimeout; + private int queryTimeout = -1; - public Integer getFetchSize() { + public int getFetchSize() { return this.fetchSize; } - public void setFetchSize(Integer fetchSize) { + public void setFetchSize(int fetchSize) { this.fetchSize = fetchSize; } - public Integer getMaxRows() { + public int getMaxRows() { return this.maxRows; } - public void setMaxRows(Integer maxRows) { + public void setMaxRows(int maxRows) { this.maxRows = maxRows; } - public Integer getQueryTimeout() { + public int getQueryTimeout() { return this.queryTimeout; } - public void setQueryTimeout(Integer queryTimeout) { + public void setQueryTimeout(int queryTimeout) { this.queryTimeout = queryTimeout; } + } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfiguration.java index 4110f6a1542..70b189c6ad5 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfiguration.java @@ -68,17 +68,10 @@ public class JdbcTemplateAutoConfiguration { public JdbcTemplate jdbcTemplate() { JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource); JdbcProperties.Template template = this.properties.getTemplate(); - if (template.getFetchSize() != null) { - jdbcTemplate.setFetchSize(template.getFetchSize()); - } - if (template.getQueryTimeout() != null) { - jdbcTemplate.setQueryTimeout(template.getQueryTimeout()); - } - if (template.getMaxRows() != null) { - jdbcTemplate.setMaxRows(template.getMaxRows()); - } + jdbcTemplate.setFetchSize(template.getFetchSize()); + jdbcTemplate.setMaxRows(template.getMaxRows()); + jdbcTemplate.setQueryTimeout(template.getQueryTimeout()); return jdbcTemplate; - } } diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index a3643a269fd..fc3d6e422ef 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -710,6 +710,11 @@ content into your application; rather pick only the properties that you need. # JOOQ ({sc-spring-boot-autoconfigure}/jooq/JooqAutoConfiguration.{sc-ext}[JooqAutoConfiguration]) spring.jooq.sql-dialect= # Sql dialect to use, auto-detected by default. + # JDBC ({sc-spring-boot-autoconfigure}/jdbc/JdbcProperties.{sc-ext}[JdbcProperties]) + spring.jdbc.template.fetch-size=-1 # Number of rows that should be fetched from the database when more rows are needed. + spring.jdbc.template.max-rows=-1 # Maximum number of rows. + spring.jdbc.template.query-timeout=-1 # Query timeout in seconds. + # JPA ({sc-spring-boot-autoconfigure}/orm/jpa/JpaBaseConfiguration.{sc-ext}[JpaBaseConfiguration], {sc-spring-boot-autoconfigure}/orm/jpa/HibernateJpaAutoConfiguration.{sc-ext}[HibernateJpaAutoConfiguration]) spring.data.jpa.repositories.enabled=true # Enable JPA repositories. spring.jpa.database= # Target database to operate on, auto-detected by default. Can be alternatively set using the "databasePlatform" property. diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index c15cc9b9150..0e479446f38 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -2932,11 +2932,20 @@ you can `@Autowire` them directly into your own beans: } ---- +You can customize some properties of the template using the `spring.jdbc.template.*` +properties: + +[source,properties,indent=0] +---- + spring.jdbc.template.max-rows=500 +---- + TIP: The `NamedParameterJdbcTemplate` reuses the same `JdbcTemplate` instance behind the scene. If more than one `JdbcTemplate` is defined and no primary candidate exists, the `NamedParameterJdbcTemplate` is not auto-configured. + [[boot-features-jpa-and-spring-data]] === JPA and '`Spring Data`' The Java Persistence API is a standard technology that allows you to '`map`' objects to