Integrate Spanner with MyBatis and Spring Boot (PostgreSQL)

MyBatis is a persistence framework with support for custom SQLand advanced mappings. MyBatis eliminates most of the JDBC codeand manual setting of parameters and retrieval of results in your application.

Set up MyBatis for Spanner PostgreSQL-dialect databases

You can integrate Spanner PostgreSQL-dialect databases with MyBatisand Spring Boot using the Spanner JDBC driver.

You don't need to usePGAdapter for this integration.

Dependencies

In your project, add Apache Maven dependencies forMyBatis,Spring Boot, and theSpanner JDBC driver.

<dependencies><!--MyBatisandSpringBoot--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>org.mybatis.dynamic-sql</groupId><artifactId>mybatis-dynamic-sql</artifactId></dependency><!--SpannerJDBCdriver--><dependency><groupId>com.google.cloud</groupId><artifactId>google-cloud-spanner-jdbc</artifactId></dependency><dependencies>

Data source configuration

Configureapplication.properties to use the Spanner JDBC driver andto connect to a Spanner PostgreSQL-dialect database.

# This profile uses a Spanner PostgreSQL database.spanner.project=my-projectspanner.instance=my-instancespanner.database=mybatis-samplespring.datasource.driver-class-name=com.google.cloud.spanner.jdbc.JdbcDriverspring.datasource.url=jdbc:cloudspanner:/projects/${spanner.project}/instances/${spanner.instance}/databases/${spanner.database}

Full sample application

To try this integration with a sample application, seeSpring Data MyBatis Sample Application withSpanner PostgreSQL.

What's next

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

Last updated 2025-07-18 UTC.