GORM 是适用于 Go 编程语言的对象关系映射工具。它提供了一个框架,用于将面向对象的网域模型映射到关系型数据库。
您可以使用开源 Spanner Dialect (SpannerDialect) 将 GoogleSQL 方言数据库与 GORM 集成。
设置 GORM 以便与 Spanner GoogleSQL 方言数据库搭配使用
如需在应用中使用 GoogleSQL GORM 方言,请将以下导入语句添加到初始化 GORM 的文件中:
import (
"fmt"
"gorm.io/gorm"
_ "github.com/googleapis/go-sql-spanner"
spannergorm "github.com/googleapis/go-gorm-spanner"
)
dsn := fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId),
db, err := gorm.Open(spannergorm.New(spannergorm.Config{DriverName: "spanner", DSN: dsn}), &gorm.Config{})
```
See the
[GORM with GoogleSQL documentation][go-driver-documentation]
for more connection options for Spanner.
## Use GORM with Spanner GoogleSQL-dialect databases
For more information about the features and recommendations for using
GORM with Spanner, consult the [reference documentation][spanner-gorm-github]
on GitHub.
## What's next {: #whats-next}
* Checkout the [sample application][gorm-gsql-sample-application] using
GORM with GoogleSQL and Spanner.
* Learn more about [GORM][gorm].
* [File a GitHub issue][spanner-gorm-issue] to report a bug or ask a question
about using GORM with Spanner with GoogleSQL.
* Learn more about [Integrate Spanner with GORM (
PostgreSQL dialect)](/spanner/docs/use-gorm-postgresql).