Integrate Spanner with Spring Data JDBC (GoogleSQL)

Spring Data JDBC, part of the larger Spring Data family, makes it easier to implement JDBC based repositories in your application. It adds an abstraction layer between your application and your database that makes your application easier to port from one database system to another.

Set up Spring Data JDBC for Spanner GoogleSQL-dialect databases

You can integrate Spanner GoogleSQL-dialect databases with Spring Data JDBC by adding a dialect for Spanner GoogleSQL to your application.

Dialect configuration

Spring Data JDBC selects the database dialect it uses based on the JDBC driver that has been configured as a data source in Spring Data. You must add an additional dialect provider to your application to instruct Spring Data JDBC to use the GoogleSQL dialect when the Spanner JDBC driver is used:

publicclass SpannerDialectProviderimplementsDialectResolver.JdbcDialectProvider{
@Override
publicOptional<Dialect>getDialect(JdbcOperationsoperations){
returnOptional.ofNullable(
operations.execute((ConnectionCallback<Dialect>)SpannerDialectProvider::getDialect));
}
@Nullable
privatestaticDialectgetDialect(Connectionconnection)throwsSQLException{
DatabaseMetaDatametaData=connection.getMetaData();
Stringname=metaData.getDatabaseProductName().toLowerCase(Locale.ENGLISH);
if(name.contains("spanner")){
returnSpannerDialect.INSTANCE;
}
returnnull;
}
}

This dialect provider must be added to the spring.factories file in your application:

org.springframework.data.jdbc.repository.config.DialectResolver$JdbcDialectProvider=org.springframework.data.jdbc.repository.config.DialectResolver.DefaultDialectProvider,com.google.cloud.spanner.sample.SpannerDialectProvider

To see an example, refer to the full working sample application on GitHub.

Dependencies

In your project, add Apache Maven dependencies for Spring Data JDBC and the Spanner JDBC driver.

<dependencies>
<!--SpringDataJDBC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<!--SpannerJDBCdriver-->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner-jdbc</artifactId>
</dependency>
<dependencies>

Data source configuration

Configure application.properties to use the Spanner JDBC driver and connect to a Spanner GoogleSQL-dialect database.

spanner.project=my-project
spanner.instance=my-instance
spanner.database=spring-data-jdbc
spring.datasource.driver-class-name=com.google.cloud.spanner.jdbc.JdbcDriver
spring.datasource.url=jdbc:cloudspanner:/projects/${spanner.project}/instances/${spanner.instance}/databases/${spanner.database}

Full Sample Application

To try this integration with a sample application, see Spring Data JDBC Sample Application with Spanner GoogleSQL.

What's next

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月14日 UTC.