Im August 2020 wurde GORM v2.0 veröffentlicht. Es gibt viele Änderungen und einige Funktionen sind nicht mit v1 kompatibel, daher möchte ich sie in diesem Artikel behandeln. change log link
Change Log
v2.0 - 2020.08
GORM 2.0 ist ein Umschreiben von Grund auf neu, das inkompatible API-Änderungen und viele Verbesserungen einführt
-Leistungsverbesserung
-Verbesserte Modularität
- Context, Batch Insert, Prepared Statment Mode, DryRun Mode, Join Preload, Find To Map, Create From Map,Unterstützt FindInBatches.
- SavePoint/RollbackTo/Unterstützt verschachtelte Transaktionen.
-Benannte Argumente, Gruppenbedingungen, Upsets, Sperren, Optimierer/Index/Unterstützung für Kommentarhinweise, verbesserte Unterabfragen
-Volle Unterstützung für Selbstreferenzbeziehungen, verbesserte Verknüpfungstabelle, Zuordnungsmodus für Batchdaten
-Erstellen/Unterstützung für mehrere Felder zur Verfolgung der Aktualisierungszeiten. UNIX (Millimeter)/Nano) Sekunden Unterstützung wird hinzugefügt.
-Unterstützung für Feldberechtigungen: read-only, write-only, create-only, update-only, ignored
-Neues Plug-In-System: Mehrere Datenbanken, die vom Plug-In Database Resolver gelesen werden/Schreiben Sie Split-Unterstützung, Prometheus-Integration ...
-Neue Hook-API: Integrierte Schnittstelle mit Plugins
-Neue Migration: Beziehungen, Einschränkungen/Sie können einen externen Datenbankschlüssel für die Prüferunterstützung und die erweiterte Indexunterstützung erstellen
-Neuer Logger: Kontextunterstützung, erhöhte Skalierbarkeit
-Einheitliche Namensregeln: Tabellenname, field name, join table name, foreign key, checker, index name
-Unterstützung für besser angepasste Datentypen(e.g: JSON)
v2
main.go
package main
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main(){
dsn := "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local"
v2_db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
}
v1
main.go
package main
import (
"github.com/jinzhu/gorm"
_"github.com/jinzhu/gorm/dialects/mysql"
)
func main(){
v1_db, err := gorm.Open("mysql", "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local")
}
v2
main.go
package main
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main(){
dsn := "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local"
v2_db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
db, err := v2_db.DB()
defer db.Close()
}
v1
main.go
package main
import (
"github.com/jinzhu/gorm"
_"github.com/jinzhu/gorm/dialects/mysql"
)
func main(){
v1_db, err := gorm.Open("mysql", "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local")
defer v1_db.Close()
}
v2
main.go
package main
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type User struct {
Id int
Name string
Age int
Addr string
Pic string
}
func main(){
dsn := "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local"
v2_db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
//Erstellen Sie eine Tabelle mit dem Namen Benutzer
v2_db.Migrator().CreateTable(&User{})
}
v1
main.go
package main
import (
"github.com/jinzhu/gorm"
_"github.com/jinzhu/gorm/dialects/mysql"
)
func main(){
v1_db, err := gorm.Open("mysql", "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local")
//Erstellen Sie eine Tabelle mit dem Namen Benutzer
v1_db.CreateTable(&User{})
}
v2
main.go
package main
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type User struct {
Id int
Name string
Age int
Addr string
Pic string
}
func main(){
dsn := "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local"
v2_db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
//Tabelle löschen
v2_db.Migrator().DropTable(&User{})
}
v1
main.go
package main
import (
"github.com/jinzhu/gorm"
_"github.com/jinzhu/gorm/dialects/mysql"
)
func main(){
v1_db, err := gorm.Open("mysql", "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local")
//Tabelle löschen
v1_db.DropTable("users")
}
v2
main.go
package main
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main(){
dsn := "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local"
v2_db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
//Bestätigung der Tabellenexistenz
b := v2_db.Migrator().HasTable("users")
//Der Rückgabewert ist vom Typ Bool
fmt.Println(b)
}
v1
main.go
package main
import (
"github.com/jinzhu/gorm"
_"github.com/jinzhu/gorm/dialects/mysql"
)
func main(){
v1_db, err := gorm.Open("mysql", "root:@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local")
//Bestätigung der Tabellenexistenz
v1_db.HasTable("users")
//Der Rückgabewert ist vom Typ Bool
fmt.Println(b)
}
Verwandte Funktionen können in Version 2 nicht verwendet werden.
// Related get related associations
func (s *DB) Related(value interface{}, foreignKeys ...string) *DB {
return s.NewScope(s.Value).related(value, foreignKeys...).db
}
v2
//Angeben von LogLevel
db.Logger.LogMode(4)
v1
db.LogMode(Boolescher Wert)
Recommended Posts