Merge pull request #7960 from kazuki43zoo:jdbc-template-properties
* pr/7960: Polish "Allow to customize the JdbcTemplate" Allow to customize the JdbcTemplate
This commit is contained in:
commit
d4665d02b2
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Configuration properties for JDBC.
|
||||
*
|
||||
* @author Kazuki Shimizu
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.jdbc")
|
||||
public class JdbcProperties {
|
||||
|
||||
private final Template template = new Template();
|
||||
|
||||
public Template getTemplate() {
|
||||
return this.template;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code JdbcTemplate} settings.
|
||||
*/
|
||||
public static class Template {
|
||||
|
||||
/**
|
||||
* 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 int fetchSize = -1;
|
||||
|
||||
/**
|
||||
* Maximum number of rows. Use -1 to use the JDBC driver's default configuration.
|
||||
*/
|
||||
private int maxRows = -1;
|
||||
|
||||
/**
|
||||
* Query timeout in seconds. Use -1 to use the JDBC driver's default
|
||||
* configuration.
|
||||
*/
|
||||
private int queryTimeout = -1;
|
||||
|
||||
public int getFetchSize() {
|
||||
return this.fetchSize;
|
||||
}
|
||||
|
||||
public void setFetchSize(int fetchSize) {
|
||||
this.fetchSize = fetchSize;
|
||||
}
|
||||
|
||||
public int getMaxRows() {
|
||||
return this.maxRows;
|
||||
}
|
||||
|
||||
public void setMaxRows(int maxRows) {
|
||||
this.maxRows = maxRows;
|
||||
}
|
||||
|
||||
public int getQueryTimeout() {
|
||||
return this.queryTimeout;
|
||||
}
|
||||
|
||||
public void setQueryTimeout(int queryTimeout) {
|
||||
this.queryTimeout = queryTimeout;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
|
@ -46,6 +47,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
|||
@ConditionalOnClass({ DataSource.class, JdbcTemplate.class })
|
||||
@ConditionalOnSingleCandidate(DataSource.class)
|
||||
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
|
||||
@EnableConfigurationProperties(JdbcProperties.class)
|
||||
public class JdbcTemplateAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
|
|
@ -53,15 +55,23 @@ public class JdbcTemplateAutoConfiguration {
|
|||
|
||||
private final DataSource dataSource;
|
||||
|
||||
JdbcTemplateConfiguration(DataSource dataSource) {
|
||||
private final JdbcProperties properties;
|
||||
|
||||
JdbcTemplateConfiguration(DataSource dataSource, JdbcProperties properties) {
|
||||
this.dataSource = dataSource;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean(JdbcOperations.class)
|
||||
public JdbcTemplate jdbcTemplate() {
|
||||
return new JdbcTemplate(this.dataSource);
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
|
||||
JdbcProperties.Template template = this.properties.getTemplate();
|
||||
jdbcTemplate.setFetchSize(template.getFetchSize());
|
||||
jdbcTemplate.setMaxRows(template.getMaxRows());
|
||||
jdbcTemplate.setQueryTimeout(template.getQueryTimeout());
|
||||
return jdbcTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,6 +62,22 @@ public class JdbcTemplateAutoConfigurationTests {
|
|||
JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class);
|
||||
assertThat(jdbcTemplate.getDataSource()).isEqualTo(
|
||||
this.context.getBean(DataSource.class));
|
||||
assertThat(jdbcTemplate.getFetchSize()).isEqualTo(-1);
|
||||
assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(-1);
|
||||
assertThat(jdbcTemplate.getMaxRows()).isEqualTo(-1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJdbcTemplateWithCustomProperties() throws Exception {
|
||||
load("spring.jdbc.template.fetch-size:100",
|
||||
"spring.jdbc.template.query-timeout:60",
|
||||
"spring.jdbc.template.max-rows:1000");
|
||||
JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class);
|
||||
assertThat(jdbcTemplate).isNotNull();
|
||||
assertThat(jdbcTemplate.getDataSource()).isNotNull();
|
||||
assertThat(jdbcTemplate.getFetchSize()).isEqualTo(100);
|
||||
assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(60);
|
||||
assertThat(jdbcTemplate.getMaxRows()).isEqualTo(1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue