Documentation.
Original pull request:
@@ -70,23 +70,89 @@ The Spring Data JDBC repositories support can be activated by an annotation thro
[source, java]
----
@Configuration
-@EnableJdbcRepositories
-class ApplicationConfig {
+@EnableJdbcRepositories // <1>
+class ApplicationConfig extends AbstractJdbcConfiguration { // <2>
@Bean
- public DataSource dataSource() {
+ public DataSource dataSource() { // <3>
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.HSQL).build();
}
+ @Bean
+ NamedParameterJdbcOperations namedParameterJdbcOperations(DataSource dataSource) { // <4>
+
+ return new NamedParameterJdbcTemplate(dataSource);
+ }
+
+ @Bean
+ TransactionManager transactionManager(DataSource dataSource) { // <5>
+ return new DataSourceTransactionManager(dataSource);
+ }
}
----
+<1> `@EnableJdbcRepositories` creates implementations for interfaces derived from `Repository`
+<2> `AbstractJdbcConfiguration` provides various default beans required by Spring Data JDBC
+<3> Creates a `DataSource` connecting to a database. This is required by the following two bean methods.
+<4> Creates the `NamedParameterJdbcOperations` used by Spring Data JDBC to access the database.
+<5> Spring Data JDBC utilizes the transaction management provided by Spring JDBC.
====
The configuration class in the preceding example sets up an embedded HSQL database by using the `EmbeddedDatabaseBuilder` API of `spring-jdbc`.
-We activate Spring Data JDBC repositories by using the `@EnableJdbcRepositories`.
+The `DataSource` is then used to set up `NamedParameterJdbcOperations` and a `TransactionManager.
+We finally activate Spring Data JDBC repositories by using the `@EnableJdbcRepositories`.
If no base package is configured, it uses the package in which the configuration class resides.
+Extending `AbstractJdbcConfiguration` ensures various beans get registered.
+Overwriting its methods can be used to customize the setup (see below).
+
+This configuration can be further simplified by using Spring Boot.
+With Spring Boot a `DataSource` is sufficient once the starter `spring-boot-starter-data-jdbc` is included in the dependencies.
+Everything else is done by Spring Boot.
+
+There are a couple of things one might want to customize in this setup
+
+You can register custom conversions which will be used when writing to or reading from the database by overwriting `jdbcCustomConversions` as demonstrated in the following example:
+====
+[source, java]
+----
+ @Overwrite
+ @Bean
+ public JdbcCustomConversions jdbcCustomConversions() {
+ return new JdbcCustomConversions(new JdbcCustomConversions(Arrays.asList(BooleanToStringConverter.INSTANCE, StringToBooleanConverter.INSTANCE)));
+ }
+
+
+ @WritingConverter
+ enum BooleanToStringConverter implements Converter<Boolean, String> {
+
+ INSTANCE;
+
+ @Override
+ public String convert(Boolean source) {
+ return source != null && source ? "T" : "F";
+ }
+ }
+
+ @ReadingConverter
+ enum StringToBooleanConverter implements Converter<String, Boolean> {
+
+ INSTANCE;
+
+ @Override
+ public Boolean convert(String source) {
+ return source != null && source.equalsIgnoreCase("T") ? Boolean.TRUE : Boolean.FALSE;
+ }
+ }
+----
+====
+
+Spring Data JDBC uses implementations of the interface `Dialect` to encapsulate behavior that is specific to a database or its JDBC driver.
+By default the `AbstractJdbcConfiguration` tries to determine the database in use an register the correct `Dialect`.
+This behavior can be changed by overwriting `dialect(NamedParameterJdbcOperations)`.
+
+Database vendors may provide an implementation of `JdbcDialectProvider` along with a matching entry in the `META-INF/spring-factories` file registering it for the key `org.springframework.data.jdbc.repository.config.JdbcDialectResolver$JdbcDialectProvider`.
+The `JdbcDialectProvider` should detect the database and provide an appropriate `Dialect` implementation for it.
[[jdbc.entity-persistence]]
== Persisting Entities
