This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Data Relational 3.4.0!spring-doc.cn

Getting Started

An easy way to bootstrap setting up a working environment is to create a Spring-based project in Spring Tools or from Spring Initializr.spring-doc.cn

First, you need to set up a running database server. Refer to your vendor documentation on how to configure your database for JDBC access.spring-doc.cn

Requirements

Spring Data JDBC requires Spring Framework 6.2.0 and above.spring-doc.cn

In terms of databases, Spring Data JDBC requires a dialect to abstract common SQL functionality over vendor-specific flavours. Spring Data JDBC includes direct support for the following databases:spring-doc.cn

If you use a different database then your application won’t start up. The dialect section contains further detail on how to proceed in such case.spring-doc.cn

Hello World

To create a Spring project in STS:spring-doc.cn

  1. Go to File → New → Spring Template Project → Simple Spring Utility Project, and press Yes when prompted. Then enter a project and a package name, such as org.spring.jdbc.example.spring-doc.cn

  2. Add the following to the pom.xml files dependencies element:spring-doc.cn

    <dependencies>
    
      <!-- other dependency elements omitted -->
    
      <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jdbc</artifactId>
        <version>3.5.0-SNAPSHOT</version>
      </dependency>
    
    </dependencies>
  3. Change the version of Spring in the pom.xml to bespring-doc.cn

    <spring.version>6.2.0</spring.version>
  4. Add the following location of the Spring Milestone repository for Maven to your pom.xml such that it is at the same level as your <dependencies/> element:spring-doc.cn

    <repositories>
      <repository>
        <id>spring-milestone</id>
        <name>Spring Maven MILESTONE Repository</name>
        <url>https://repo.spring.io/milestone</url>
      </repository>
    </repositories>

The repository is also browseable here.spring-doc.cn

Logging

Spring Data JDBC does little to no logging on its own. Instead, the mechanics of JdbcTemplate to issue SQL statements provide logging. Thus, if you want to inspect what SQL statements are run, activate logging for Spring’s NamedParameterJdbcTemplate or MyBatis.spring-doc.cn

You may also want to set the logging level to DEBUG to see some additional information. To do so, edit the application.properties file to have the following content:spring-doc.cn

logging.level.org.springframework.jdbc=DEBUG

Examples Repository

There is a GitHub repository with several examples that you can download and play around with to get a feel for how the library works.spring-doc.cn

Configuration

The Spring Data JDBC repositories support can be activated by an annotation through Java configuration, as the following example shows:spring-doc.cn

Spring Data JDBC repositories using Java configuration
@Configuration
@EnableJdbcRepositories                                                                (1)
class ApplicationConfig extends AbstractJdbcConfiguration {                            (2)

    @Bean
    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. 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).spring-doc.cn

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.spring-doc.cn

There are a couple of things one might want to customize in this setup.spring-doc.cn

Dialects

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 attempts to determine the dialect from the database configuration by obtaining a connection and registering the correct Dialect. You override AbstractJdbcConfiguration.jdbcDialect(NamedParameterJdbcOperations) to customize dialect selection.spring-doc.cn

If you use a database for which no dialect is available, then your application won’t start up. In that case, you’ll have to ask your vendor to provide a Dialect implementation. Alternatively, you can implement your own Dialect.spring-doc.cn

Dialects are resolved by DialectResolver from a JdbcOperations instance, typically by inspecting Connection.getMetaData(). + You can let Spring auto-discover your JdbcDialect by registering a class that implements org.springframework.data.jdbc.repository.config.DialectResolver$JdbcDialectProvider through META-INF/spring.factories. DialectResolver discovers dialect provider implementations from the class path using Spring’s SpringFactoriesLoader. To do so:spring-doc.cn

  1. Implement your own Dialect.spring-doc.cn

  2. Implement a JdbcDialectProvider returning the Dialect.spring-doc.cn

  3. Register the provider by creating a spring.factories resource under META-INF and perform the registration by adding a line
    org.springframework.data.jdbc.repository.config.DialectResolver$JdbcDialectProvider=<fully qualified name of your JdbcDialectProvider>spring-doc.cn