Integrate Spanner with MyBatis and Spring Boot (GoogleSQL)

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 GoogleSQL-dialect databases

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

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 GoogleSQL-dialect 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 GoogleSQL.

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-12-17 UTC.