將 Spanner 與 MyBatis 和 Spring Boot (PostgreSQL) 整合

MyBatis 是一個持久化架構,支援自訂 SQL 和進階對應。MyBatis 可讓您在應用程式中省去大部分的 JDBC 程式碼,以及手動設定參數和擷取結果的作業。

為 Spanner PostgreSQL 方言資料庫設定 MyBatis

您可以使用 Spanner JDBC 驅動程式,將 Spanner PostgreSQL 方言資料庫與 MyBatis 和 Spring Boot 整合。

您不需要使用 PGAdapter 進行這項整合。

依附元件

在專案中,為 MyBatisSpring BootSpanner JDBC 驅動程式新增 Apache Maven 依附元件。

<dependencies>
  <!-- MyBatis and Spring Boot -->
  <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>

  <!-- Spanner JDBC driver -->
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-spanner-jdbc</artifactId>
  </dependency>
<dependencies>

資料來源設定

設定 application.properties 使用 Spanner JDBC 驅動程式,並連線至 Spanner PostgreSQL 方言資料庫。

# 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}

完整的應用程式範例

如要嘗試這項整合功能的範例應用程式,請參閱「Spring Data MyBatis 範例應用程式,搭配使用 Spanner PostgreSQL」。

後續步驟