# starter-gorm **Repository Path**: go-spring2/starter-gorm ## Basic Information - **Project Name**: starter-gorm - **Description**: starter-gorm - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-04 - **Last Updated**: 2026-05-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # starter-gorm [English](README_EN.md) [仅发布] 该项目仅为最终发布,不要向该项目直接提交代码,开发请关注 [go-spring](https://gitee.com/go-spring2/go-spring) 项目。 ## Installation ### Prerequisites - Go >= 1.12 ### Using go get ``` go get gitee.com/go-spring2/starter-gorm@v1.1.0-rc2 ``` ## Quick Start ``` import "gitee.com/go-spring2/starter-gorm/mysql" ``` `main.go` ``` package main import ( "fmt" "gitee.com/go-spring2/spring-base/log" "gitee.com/go-spring2/spring-core/gs" "gorm.io/gorm" _ "gitee.com/go-spring2/starter-gorm/mysql" ) type runner struct { DB *gorm.DB `autowire:""` } func (r *runner) Run(ctx gs.Context) { var engines []string r.DB.Raw("select engine from engines").Scan(&engines) log.Infof("got mysql engines %v", engines) go gs.ShutDown() } func main() { gs.Object(&runner{}).Export((*gs.AppRunner)(nil)) fmt.Printf("program exited %v\n", gs.Web(false).Run()) } ``` `application.properties` ``` gorm.url=root:@/information_schema?charset=utf8&parseTime=True&loc=Local ``` ## Multi-Database Support starter-gorm 支持连接多个数据库,通过注入 `*GormDBs` 实现。 ### Configuration (配置) 使用 `gorm.db` 配置多个数据库: **Properties 格式:** ```properties gorm.db.primary.url=root:@/main_db?charset=utf8&parseTime=True&loc=Local gorm.db.secondary.url=root:@/cache_db?charset=utf8&parseTime=True&loc=Local gorm.db.reporting.url=root:@/reporting_db?charset=utf8&parseTime=True&loc=Local ``` **YAML 格式:** ```yaml gorm: db: primary: url: root:@/main_db?charset=utf8&parseTime=True&loc=Local secondary: url: root:@/cache_db?charset=utf8&parseTime=True&loc=Local reporting: url: root:@/reporting_db?charset=utf8&parseTime=True&loc=Local ``` ### Usage (使用) 注入 `*GormDBs`,通过 `.DBs` map 访问数据库: ```go import ( StarterMySqlGorm "gitee.com/go-spring2/starter-gorm/mysql" _ "gitee.com/go-spring2/starter-gorm/mysql" ) type MultiDBService struct { DBs *StarterMySqlGorm.GormDBs `autowire:""` } func (s *MultiDBService) DoSomething() { // 使用 primary 数据库 s.DBs.DBs["primary"].First(&user) // 使用 secondary 数据库 s.DBs.DBs["secondary"].Create(&log) // 使用 reporting 数据库 s.DBs.DBs["reporting"].Save(&report) // 遍历所有数据库 for name, db := range s.DBs.DBs { fmt.Printf("database: %s\n", name) _ = db } } ``` ### Mixed MySQL and SQLite 可以同时使用 MySQL 和 SQLite: ```properties # MySQL gorm.db.primary.url=root:@/main_db?charset=utf8 gorm.db.analytics.url=root:@/analytics_db?charset=utf8 # SQLite sqlite-gorm.db.cache.url=file:./cache.db sqlite-gorm.db.session.url=file:./session.db ``` ```go import ( _ "gitee.com/go-spring2/starter-gorm/mysql" StarterSqliteGorm "gitee.com/go-spring2/starter-gorm/sqlite" ) type HybridService struct { MySQLDBs *StarterMySqlGorm.GormDBs `autowire:""` SQLiteDBs *StarterSqliteGorm.GormDBs `autowire:"SqliteGormDBs"` } ``` ### Backward Compatibility (向后兼容) 旧的单数据库配置继续工作: ```properties gorm.url=root:@/mydb?charset=utf8 ``` ```go type Service struct { DB *gorm.DB `autowire:""` } ``` ## Customization