Integrate Spanner with MyBatis and Spring Boot (PostgreSQL)

MyBatis is a persistence framework with support for custom SQL and advanced mappings. MyBatis eliminates most of the JDBC code and 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 MyBatis and Spring Boot using the Spanner JDBC driver.

You don't need to use PGAdapter for this integration.

Dependencies

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


  
  
    org.mybatis.spring.boot
    mybatis-spring-boot-starter
  
  
    org.mybatis.dynamic-sql
    mybatis-dynamic-sql
  

  
  
    com.google.cloud
    google-cloud-spanner-jdbc
  

Data source configuration

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

# This profile uses a Spanner PostgreSQL database.

spanner.project=my-project
spanner.instance=my-instance
spanner.database=mybatis-sample

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 MyBatis Sample Application with Spanner PostgreSQL.

What's next