Gorm
1. Ã¥ÂÂå¤Âå·¥ä½Â
é¦Âå Âè¿Âå ¥ç»Â端ä¸Âè½½æÂÂ们éÂÂè¦ÂçÂÂå ï¼Âç¡®ä¿ÂgoÃ¥ÂÂmysqlå®Â裠å®ÂæÂÂï¼Â并设置äºÂç¯å¢ÂÃ¥ÂÂéÂÂï¼Â
go get -u gorm.io/driver/mysql
go get -u gorm.io/gorm
æÂÂ两份å®Âæ¹æÂÂæ¡£æÂÂ对 GORM æ´详ç»ÂçÂÂ讲解ãÂÂ
- Ã¥ÂÂ建 | GORM – The fantastic ORM library for Golang, aims to be developer friendly.ï¼Âä¸ÂæÂÂï¼Â
- go-sql-driver/mysql: Go MySQL Driver is a MySQL driver for Go’s (golang) database/sql package (github.com)
2. è¿ÂæÂ¥æ°æ®åºÂ
è¿ÂéÂÂ以mysql为ä¾Âï¼Âå ¶ä»Âæ°æ®åºÂ请çÂÂå®Âæ¹æÂÂæ¡£ãÂÂ
import (
ÃÂ "gorm.io/driver/mysql"
ÃÂ "gorm.io/gorm"
)
âÂÂ
func main() {
à// Ã¥ÂÂè https://github.com/go-sql-driver/mysql#dsn-data-source-name è·åÂÂ详æÂÂ
ÃÂ dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
ÃÂ db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
}
user -> ç¨æ·åÂÂ
pass -> å¯Âç Â
127.0.0.1 -> mysqlé»Â认æΡå°åÂÂï¼Âä¹Âå°±æ¯localhost
3306 -> mysqlé»Â认端å£
dbname -> æ°æ®åºÂÃ¥ÂÂ称
charset -> Ã¥ÂÂ符éÂÂç¼Âç Â
parseTime -> æÂÂæ°æ®åºÂdatetimeÃ¥ÂÂdateç±»åÂÂ转æ¢为golangçÂÂtime.Timeç±»åÂÂ
loc -> 使ç¨系ç»ÂæΡæ¶åº
gorm.config{}
ä¸Âå¯以è¿Âè¡Âä¸ÂäºÂé«Â级éÂ
Âç½®ï¼Â详æÂÂ
请çÂÂå®Âæ¹æÂÂæ¡£ãÂÂ
3. ç»ÂæÂÂä½Âä¸Âmysql table çÂÂæ å°Â堳系
goçÂÂormæ¡Âæ¶å¯以让æÂÂ们å¨ç¨goè¯Âè¨Âå®Âç°æ°æ®åºÂæÂÂä½ÂçÂÂæ¶åÂÂä¸Â使ç¨sqlè¯Âè¨Âï¼Âç®ÂÃ¥ÂÂæÂÂä¸ÂæÂÂï¼ÂæÂÂé«Âå¼ÂÃ¥ÂÂæÂÂçÂÂãÂÂ
ä¸Âé¢æ¯gormä¸Âgoä¸ÂmysqlçÂÂ对åºÂ堳系
GO | mysql |
---|---|
ç»ÂæÂÂä½ | æ°æ®表 |
ç»ÂæÂÂä½Âå®Âä¾ | æ°æ®衠|
ç»ÂæÂÂä½ÂÃ¥ÂÂ段 | Ã¥ÂÂ段 |
ä¾Âå¦Âï¼Âä¸Â个ç»ÂæÂÂä½Âï¼Â
type Student struct {
ID ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ int
Name ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ string
age ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ int
ExternalCharacter string
}
gorm ä¼Âå°Âå ¶æ å°Â为
CREATE TABLE `students` (
ÃÂ ÃÂ `id` bigint AUTO_INCREMENT,
ÃÂ ÃÂ `name` longtext,
ÃÂ ÃÂ `external_character` longtext,
ÃÂ ÃÂ PRIMARY KEY (`id`)
)
è¿ÂéÂÂå¯以åÂÂç°ï¼Âç»ÂæÂÂä½ÂÃ¥ÂÂ段ä¸ÂçÂÂ大åÂÂé½åÂÂæÂÂäºÂå°ÂÃ¥ÂÂï¼Âageå®ÂÃ¥Â
¨æ¶Â失ï¼ÂStudent
ä¹ÂÃ¥ÂÂæÂÂäºÂstudents
ï¼ÂExternalCharacter
Ã¥ÂÂæÂÂäºÂexternal_character
ãÂÂè¿ÂéÂÂæ¶ÂÃ¥ÂÂå°äºÂgormçÂÂæ å°Âè§Âå®ÂãÂÂ
å ·ä½Âå¯çÂÂ模åÂÂå®Âä¹ | GORM – The fantastic ORM library for Golang, aims to be developer friendly.
ç®ÂÃ¥ÂÂæ¥说ï¼Âå°±æ¯å±è¦Âé¦ÂÃ¥ÂÂæ¯Â大åÂÂï¼Âä¼ÂæÂÂ驼峰å½åÂÂæ³Âç»ÂæÂÂ们å _
符å·ï¼Âé»Â认ID
为主é®ï¼Â表åÂÂä¼ÂÃ¥ÂÂæÂÂå¤Âæ°ãÂÂ
Ã¥ÂÂçÂÂæ°æ®类åÂÂï¼ÂæÂÂæ¾è¿Âæ¯ä¸ÂçÂ¥éÂÂæÂÂå¤Â大æÂÂ以å¾Â大äºÂå»è°ÂãÂÂ
å½Âç¶ï¼Âè¿ÂäºÂæÂÂ们ä¹Âæ¯è½å¤Âä¸Âä¸Âèªå®Âä¹ÂçÂÂãÂÂ
3.1. 表åÂÂï¼Â
å®Âç°TableræÂ¥å£ ä¸ÂçÂÂTableNameå½æ°ãÂÂ
ä¾Âå¦Âï¼ÂéÂÂ对ä¸Âè¿°çÂÂç»ÂæÂÂä½Âï¼ÂæÂÂ们å¯以è¿Âæ ·åÂÂï¼Â
func (s *Student) TableName() string {
return "student"
}
è¿Â样表åÂÂå°±ä¼Â被æ å°Â为studentèÂÂä¸Âæ¯students
3.2 Ã¥ÂÂ段åÂÂï¼Â
æÂÂ们å¯以å¨åÂÂ段åÂÂé¢æÂÂä¸Âgorm tagï¼ÂtagæÂÂå¾Âå¤Âç¨éÂÂï¼Âæ¯Âå¦Âæ Â记主é®ï¼ÂæÂÂå®Ânot nullçÂÂçÂÂï¼Âå ·ä½Â请çÂÂå®Âæ¹æÂÂæ¡£ãÂÂ
è¿ÂéÂÂä»Âç»Âèªå®Âä¹ÂÃ¥ÂÂ段åÂÂï¼Âè¿Âæ¯以ä¸Âè¿°çÂÂç»ÂæÂÂä½Â为ä¾Âï¼Â
type Student struct {
ID ààààààààint `gorm:"column:åºÂå·"`
Name ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ string
age ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ int
ExternalCharacter string `gorm:"column:externalCharacter"`
}
å¦ÂæÂÂæÂÂ们æ¹æÂÂè¿Âæ ·çÂÂè¯Âï¼Âæ å°ÂçÂÂ表就æ¯è¿Âæ ·çÂÂï¼Â
CREATE TABLE `student` (
àà`åºÂå·` bigint AUTO_INCREMENT,
ÃÂ ÃÂ `name` longtext,
ÃÂ ÃÂ `externalCharacter` longtext,
ààPRIMARY KEY (`åºÂå·`)
)
3.2 æ°æ®类åÂÂ
å°Âlongtext
æ¹æ varchar(255)
ï¼Âä¹Âæ¯éÂÂè¿Âtagå®Âç°çÂÂãÂÂéÂÂè¿Âsize:255
æ¥确å®ÂæÂÂ大é¿度
æÂÂbigint
æ¹æÂÂint
åªéÂÂè¦Âå°Âint
æ¹æÂÂint32
å°±è¡ÂäºÂï¼Âå 为goä¸Âï¼Âå±64ä½ÂçÂÂçµèÂÂé»Â认int
æ¯int64
ä¾Âå¦Âï¼Â
type Student struct {
ID ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ int32
Name ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ string
age ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ int
ExternalCharacter string `gorm:"column:externalCharacter; size:255"`
}
æ å°ÂåºæÂ¥çÂÂ表就æ¯è¿Âæ ·çÂÂ
CREATE TABLE `student` (
ÃÂ ÃÂ `id` int AUTO_INCREMENT,
ÃÂ ÃÂ `name` longtext,
ÃÂ ÃÂ `externalCharacter` varchar(255),
ÃÂ ÃÂ PRIMARY KEY (`id`)
)
âÂÂ
tagä¸Âå®Âä¹Âå¤Âç§ÂÃ¥ÂÂ
容ï¼Âä¸Âé´æÂÂ;
å·就è¡ÂäºÂ
4. Ã¥ÂÂ建table
æÂÂ们å¯以éÂÂè¿ db.AutoMigrate
å db.Migrator().CreateTable()
æ¹æ³ÂæÂ¥åÂÂ建tableãÂÂ
AutoMigrate æ¹æ³Âï¼Â
使ç¨ db.AutoMigrate
æ¹æ³Âæ¯ä¸Âç§Âèªå¨åÂÂ建åÂÂè¿Â移æ°æ®åºÂ表çÂÂæ¹å¼ÂãÂÂå®Âä¼Âæ ¹æ®你ç GORM 模åÂÂå®Âä¹Âï¼Âèªå¨æ£ÂæÂ¥æ°æ®åºÂ表æ¯å¦åÂÂå¨ï¼Âå¦ÂæÂÂä¸ÂÃ¥ÂÂå¨åÂÂÃ¥ÂÂ建表ï¼Âå¦ÂæÂÂ表ç»ÂæÂÂæÂÂÃ¥ÂÂÃ¥ÂÂÃ¥ÂÂè¿Âè¡Âè¿Â移ãÂÂè¿Âç§Âæ¹å¼ÂéÂÂç¨äºÂå¼ÂÃ¥ÂÂè¿Âç¨Âä¸ÂçÂÂå¿«éÂÂè¿Â代åÂÂç»´æ¤æ°æ®åºÂç»ÂæÂÂçÂÂæÂÂ
åµãÂÂ
示ä¾Âï¼Â
// èªå¨åÂÂ建åÂÂè¿Â移表
err := db.AutoMigrate(&Student{})
// è¿ÂéÂÂçÂÂdbå°±æ¯ä¸Âè¿°gorm.opençÂÂdbÃ¥ÂÂ
if err != nil {
ààpanic("Ã¥ÂÂ建/è¿Â移表格失败, error = " + err.Error())
}
Migrator().CreateTable æ¹æ³Âï¼Â
使ç¨ db.Migrator().CreateTable
æ¹æ³Âæ¯ä¸Âç§ÂæÂÂå¨åÂÂ建æ°æ®åºÂ表çÂÂæ¹å¼ÂãÂÂä½ éÂÂè¦Âæ¾å¼Âå°æÂÂå®Âè¦ÂÃ¥ÂÂ建çÂÂ表ï¼Âå®Âä¸Âä¼Âæ£ÂæÂ¥æ¯å¦已ç»ÂÃ¥ÂÂå¨ï¼ÂèÂÂæ¯ç´æÂ¥åÂÂ建表ãÂÂè¿Âç§Âæ¹å¼ÂéÂÂç¨äºÂå¨ç¹å®ÂæÂÂ
åµä¸ÂéÂÂè¦ÂæÂÂå¨æ§å¶表åÂÂ建è¿Âç¨ÂçÂÂæÂÂ
åµãÂÂ
示ä¾Âï¼Â
// æÂÂå¨åÂÂ建表
err := db.Migrator().CreateTable(&Student{})
if err != nil {
ààpanic("Ã¥ÂÂ建表格失败, error = " + err.Error())
}
åºå«æ»ç»Âï¼Â
AutoMigrate
æ¹æ³Âèªå¨根æ®模åÂÂå®Âä¹ÂÃ¥ÂÂ建åÂÂè¿Â移表ï¼ÂéÂÂç¨äºÂèªå¨åÂÂÃ¥ÂÂå¿«éÂÂè¿Â代ãÂÂMigrator().CreateTable
æ¹æ³ÂæÂÂå¨åÂÂ建表ï¼ÂéÂÂç¨äºÂéÂÂè¦ÂæÂÂå¨æ§å¶åÂÂ建è¿Âç¨ÂçÂÂæ åµãÂÂ
å¨å®ÂéÂÂ
åºÂç¨ä¸Âï¼Âä½ å¯以根æ®项ç®éÂÂæ±ÂéÂÂæ©åÂÂéÂÂçÂÂæ¹æ³ÂãÂÂéÂÂ常æÂÂ
åµä¸Âï¼Âå¼ÂÃ¥ÂÂÃ¥ÂÂè°Âè¯Âé¶段å¯è½æ´éÂÂÃ¥ÂÂ使ç¨ AutoMigrate
æ¹æ³Âï¼ÂèÂÂç¹å®Âåºæ¯ä¸ÂçÂÂæÂÂå¨æ§å¶å¯è½éÂÂè¦Â使ç¨ Migrator().CreateTable
æ¹æ³ÂãÂÂ
5. å¢Â
以ä¸ÂÃ¥ÂÂç»ÂæÂÂä½Â为ä¾Âï¼Â
type Student struct {
ID ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ int
Name ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ string
Age ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ int
ExternalCharacter string `gorm:"column:externalCharacter; size:255"`
}
âÂÂ
func (s *Student) TableName() string {
return "student"
}
5.1. db.Create(ç»ÂæÂÂä½Â对象)
lisi := Student{2, "lisi", 18, "modest"}
result := db.Create(&lisi)
if result.Error != nil {
ààpanic("æÂÂÃ¥Â
¥åÂÂ段失败, error = " + result.Error.Error())
}
fmt.Println(result.RowsAffected) // è¿ÂÃ¥ÂÂå½±åÂÂçÂÂè¡Âæ° 1
çÂÂäºÂINSERT INTO
student(
name,
age,
externalCharacter,
id) VALUES ('lisi',18,'modest',2)
å½Âç¶ä¹Âå¯以åÂÂæ¶å¢Âæ·»å¤Â个åÂÂ段
ä¾Âå¦Âï¼Â
students := []Student{
ÃÂ {ID: 3, Name: "wangwu", Age: 20, ExternalCharacter: "generous"},
ÃÂ {ID: 4, Name: "xuliu", Age: 21, ExternalCharacter: "liberal"},
}
result := db.Create(&students)
if result.Error != nil {
ààpanic("å¢Âå åÂÂ段失败")
}
fmt.Println(result.RowsAffected) // è¿ÂÃ¥ÂÂå½±åÂÂçÂÂè¡Âæ° 2
çÂÂäºÂINSERT INTO
student(
name,
age,
externalCharacter,
id) VALUES ('wangwu',20,'generous',3),('xuliu',21,'liberal',4)
5.2. db.Select(æÂÂå®ÂÃ¥ÂÂ段).Create(ç»ÂæÂÂä½Â对象)
lisi := Student{2, "lisi", 18, "modest"}
result := db.Select("ID", "Name", "Age").Create(&lisi)
if result.Error != nil {
ààpanic("æÂÂÃ¥Â
¥åÂÂ段失败, error = " + result.Error.Error())
}
fmt.Println(result.RowsAffected) // è¿ÂÃ¥ÂÂå½±åÂÂçÂÂè¡Âæ° 1
çÂÂäºÂINSERT INTO
student(
name,
age,
id) VALUES ('lisi',18,2)
5.3. db.Omit(忽çÂ¥åÂÂ段).Create(ç»ÂæÂÂä½Â对象)
lisi := Student{2, "lisi", 18, "modest"}
result := db.Omit("ExternalCharacter").Create(&lisi)
if result.Error != nil {
ààpanic("æÂÂÃ¥Â
¥åÂÂ段失败, error = " + result.Error.Error())
}
fmt.Println(result.RowsAffected) // è¿ÂÃ¥ÂÂå½±åÂÂçÂÂè¡Âæ° 1
ä¹ÂçÂÂäºÂINSERT INTO
student(
name,
age,
id) VALUES ('lisi',18,2)
5.4. Ã¥ÂÂçÂÂsqlè¯ÂÃ¥ÂÂ¥
lisi := Student{2, "lisi", 18, "modest"}
result := db.Exec("INSERT INTO `student` (`name`,`age`,`externalCharacter`,`id`) VALUES ('lisi',18,'modest',2)")
if result.Error != nil {
ààpanic("æÂÂÃ¥Â
¥åÂÂ段失败, error = " + result.Error.Error())
}
fmt.Println(result.RowsAffected) // è¿ÂÃ¥ÂÂå½±åÂÂçÂÂè¡Âæ° 1
6. æÂÂ¥
ä¹ÂÃ¥ÂÂæÂÂæÂÂçÂÂæÂÂä½Âé½æ¯åºäºÂæÂÂçÂÂè¿Â份表å®Âç°çÂÂãÂÂ
æÂÂçÂÂ表
æÂÂç°å¨çÂÂ表å¦Âä¸Âï¼Â
6.1 where
æÂÂ们è¿ÂéÂÂ使ç¨where
å½æ° 设置æ¡件
func (db *DB) Where(query interface{}, args ...interface{}) (tx *DB)
Ã¥ÂÂæ°说æÂÂ:
Ã¥ÂÂæ°å | 说æ |
---|---|
query | sqlè¯Âå¥çÂÂwhereÃ¥ÂÂÃ¥ÂÂ¥, whereÃ¥ÂÂå¥ä¸Â使ç¨é®å·(?)代æ¿åÂÂæ°å¼ï¼ÂÃ¥ÂÂ表示éÂÂè¿ÂargsÃ¥ÂÂæ°ç»Âå®ÂÃ¥ÂÂæ° |
args | whereÃ¥ÂÂå¥ç»Âå®ÂçÂÂÃ¥ÂÂæ°ï¼Âå¯以ç»Âå®Âå¤Â个åÂÂæ° |
ä¾Âå¦Âdb.Where("id in (?)", []int{1,2,3,4})
Ã¥ÂÂé¢ç selectãÂÂhaving è¯Âå¥è¿Â亠query çÂÂä¹ÂÃ¥ÂÂæ ·éÂÂç¨
6.2 Take
Ã¥ÂÂ走第ä¸Âæ¡æ¥询信æ¯
s1 := Student{}
db.Where("age = 18").Take(&s1)
fmt.Println(s1)
è¿ÂéÂÂæÂÂ们æ¯å ÂÃ¥ÂÂ建äºÂä¸Â个ç»ÂæÂÂä½Â对象ï¼Âæ¥询çÂÂç»ÂæÂÂç´æÂ¥èµÂäºÂäºÂ该对象
è¾Âåºï¼Â
{1 zhangsan 18 humble}
çÂÂäºÂï¼Â
SELECT * FROM `student` WHERE age = 18 LIMIT 1
注æÂÂè¿Â个limit 1ï¼ÂæÂÂè¿ÂéÂÂæÂÂ两个age = 18çÂÂï¼Âä½Âæ¯takeåªåÂÂä¸Â个ï¼Âä¸Â辯第ä¸Â个
6.3 First
æ ¹æ®主é®æ£åºÂæÂÂåºÂÃ¥ÂÂï¼Âæ¥询çÂÂ第ä¸Âæ¡æ°æ®
s1 := Student{}
db.Where("age = 18").First(&s1)
fmt.Println(s1)
è¾Âåºï¼Â
{1 zhangsan 18 humble}
çÂÂäºÂï¼Â
SELECT * FROM `student` WHERE age = 18 ORDER BY `student`.`id` LIMIT 1
6.4 Last
æ ¹æ®主é®åÂÂåºÂæÂÂåºÂÃ¥ÂÂï¼Âæ¥询æÂÂÃ¥ÂÂä¸Âæ¡记å½Â
s1 := Student{}
db.Where("age = 18").Last(&s1)
fmt.Println(s1)
è¾Âåºï¼Â
{5 feixin 18 frantic}
çÂÂäºÂï¼Â
SELECT * FROM `student` WHERE age = 18 ORDER BY `student`.`id` DESC LIMIT 1
6.5 Find
è¿ÂéÂÂå°±ä¸Âæ¯æ¥询ä¸Âæ¡记å½ÂäºÂï¼Âå¯以æ¥询å¤Âæ¡记å½Â
è¿ÂéÂÂå 为å¤Âæ¡记å½Âï¼Âä¸Â个ç»ÂæÂÂä½Â对象å±ä¹Â裠ä¸Âå®Âï¼ÂæÂÂ以éÂÂè¦Âæ¹æÂÂ使ç¨ç»ÂæÂÂä½ÂÃ¥ÂÂçÂÂ(å±ä¹Âä¸ÂçÂ¥éÂÂä¼ÂæÂ¥åºå¤Âå°Âè®°å½Â)
è¿ÂæÂÂä¸Âç¹就æ¯find
å¦ÂæÂÂ没æÂÂæ¾å°记å½Âæ¯ä¸Âä¼ÂæÂÂ¥errçÂÂï¼Âä½Âæ¯takeãÂÂfirstãÂÂlastä¼Âï¼Âå 为ä»Â们é½è¦ÂÃ¥ÂÂèµ°ä¸Âæ¡æ°æ®ãÂÂ
s1 := []Student{}
db.Where("age = 18").Find(&s1)
fmt.Println(s1)
è¾Âåºï¼Â
[{1 zhangsan 18 humble} {5 feixin 18 frantic}]
çÂÂäºÂï¼Â
SELECT * FROM `student` WHERE age = 18
Ã¥ÂÂ举ä¸Â个使ç¨where args
çÂÂä¾ÂÃ¥ÂÂ
s1 := []Student{}
db.Where("id in (?)", []int{1, 2, 3, 4}).Find(&s1)
fmt.Println(s1)
è¾Âåºï¼Â
[{1 zhangsan 18 humble} {2 lisi 19 modest} {3 wangwu 20 generous} {4 xuliu 21 liberal}]
çÂÂäºÂï¼Â
SELECT * FROM `student` WHERE id in (1,2,3,4)
6.6 Pluck
æ¥询ä¸ÂÃ¥ÂÂçÂÂå¼
è¿Â个å½æ°è¿ÂÃ¥ÂÂÃ¥ÂÂçÂÂç±»åÂÂï¼ÂÃ¥ÂÂæ¶éÂÂè¦Âä¸Â个模åÂÂModel
ï¼Âå°±æ¯æÂÂ们ä¹ÂÃ¥ÂÂå®Âä¹ÂçÂÂç»ÂæÂÂä½Â模åÂÂ
col := []string{}
db.Model(&Student{}).Pluck("ExternalCharacter", &col)
for _, i := range col {
ÃÂ ÃÂ fmt.Println(i)
}
è¾Âåºï¼Â
humble
modest
generous
liberal
frantic
çÂÂäºÂï¼Â
SELECT `externalCharacter` FROM `student`
å ³äºÂè¿Â个modelå ¶å®Âä¹ÂÃ¥ÂÂçÂÂæÂÂæÂÂæÂÂä½Âé½å¯以åÂȌÂÂæÂÂè¿Â个modelå½¢å¼Âï¼Âä¸Âè¿Âå¯以çÂÂçÂ¥ï¼Âå¨è¿ÂéÂÂå°±ä¸Âè¡ÂäºÂå 为你è¿ÂéÂÂ没æÂÂä»»ä½ÂæÂÂå ³studentç»ÂæÂÂä½ÂçÂÂä¿¡æ¯ï¼Âå®ÂçÂÂè³é½ä¸ÂçÂ¥éÂÂä½ æ¯åªä¸Â个表ï¼Âé£è¯å®Âå°±æÂ¥æ¾ä¸ÂäºÂå¯ï¼ÂæÂÂ以è¿ÂéÂÂè¦ÂÃ¥ÂÂè¿Â个modelãÂÂ
æ¯Âå¦Âä¹ÂÃ¥ÂÂçÂÂdb.Where("age = 18").Find(&s1)
ä¹Âå¯以åÂÂæÂÂ
db.Model(&Student{}).Where("age = 18").Find(&s1)
ç¶åÂÂè¿ÂéÂÂå¦ÂæÂÂä¸Â使ç¨åÂÂ符串åÂÂçÂÂï¼Â使ç¨ç»ÂæÂÂä½ÂstudentÃ¥ÂÂçÂÂçÂÂè¯Âï¼Âå°±å¯以ä¸Â使ç¨modelï¼Âæ¯Âä¸Â个student对象ä¸Â没å¨æÂ¥æ¾èÂÂå´çÂÂå¼ä¼ÂÃ¥ÂÂæÂÂé»Â认å¼ï¼Âä¹Âå°±æ¯int å 0ï¼ string å 空å¼ãÂÂ
ä¾Âå¦Âï¼Â
col := []Student{}
db.Pluck("ExternalCharacter", &col)
fmt.Println(col)
è¾Âåºï¼Â
[{0 ÃÂ 0 humble} {0 ÃÂ 0 modest} {0 ÃÂ 0 generous} {0 ÃÂ 0 liberal} {0 ÃÂ 0 frantic}]
çÂÂäºÂï¼Â
SELECT `ExternalCharacter` FROM `student`
æÂÂ以ï¼ÂmodelçÂÂä½Âç¨ ok äºÂå ðÂÂÂ
6.7 Select
Pluck
æ¯ ä¸ÂÃ¥ÂÂï¼Âé£å¦ÂæÂÂæÂÂéÂÂè¦Âå¤ÂÃ¥ÂÂä¿¡æ¯å¢ï¼Âselectæ¥帮å¿Â
ä¾Âå¦Âï¼ÂæÂÂæ³è¦Âæ¥询 name
å age
两åÂÂ
result := []Student{}
err = db.Select("Age", "Name").Find(&result).Error
if err != nil {
ààpanic("æ¥询失败, error = " + err.Error())
}
for _, i := range result {
ÃÂ ÃÂ fmt.Println(i.Name, i.Age)
}
ä¹ÂçÂÂ代ç Â没æÂÂå errï¼Âæ±Âï¼ÂæÂÂå¾Âå äºÂï¼ÂÃ¥ÂÂæ£erræ¯è¿Âæ ·å ðÂÂÂ
è¾Âåºï¼Â
zhangsan 18
lisi 19
wangwu 20
xuliu 21
feixin 18
çÂÂäºÂï¼Â
SELECT `age`,`name` FROM `student`
select ä¸Âä¹Âå¯以使ç¨mysqlä¸ÂçÂÂèÂÂéÂÂå½æ°
以 avg
为ä¾Âï¼Â
var averAge float32
err = db.Model(&Student{}).Select("avg(age)").Pluck("age", &averAge).Error
if err != nil{
ààpanic("æ¥询失败, error = " + err.Error())
}
fmt.Println(averAge)
è¾Âåºï¼Â
19.2
çÂÂäºÂï¼Â
SELECT avg(age) FROM `student`
使ç¨æ¹æ³ÂÃ¥ÂÂmysqlä¸Âå·®ä¸Âå¤Âï¼Âå ·ä½ÂçÂÂå®Âæ¹æÂÂæ¡£åÂÂ
6.8 Order
çÂÂÃ¥ÂÂè¯Âå¿ä¹Â大æ¦ÂçÂ¥éÂÂäºÂï¼Âæ¯æÂÂåºÂç¨çÂÂï¼Âå¯以å°ÂæÂÂç´¢ç»ÂæÂÂè¿Âè¡ÂæÂÂåºÂ
ä¾Âå¦ÂæÂÂæÂÂå¹´é¾ÂæÂÂåºÂï¼Â
result := []Student{}
err = db.Order("age asc").Select("Age", "Name").Find(&result).Error
if err != nil {
ààpanic("æ¥询失败, error = " + err.Error())
}
for _, i := range result {
ÃÂ ÃÂ fmt.Println(i.Name, i.Age)
}
è¿Âå ¶å®Âå°±æ¯ä¹ÂÃ¥ÂÂé£个å äºÂä¸Â个order
è¾Âåºï¼Â
zhangsan 18
feixin 18
lisi 19
wangwu 20
xuliu 21
çÂÂäºÂ:
SELECT `age`,`name` FROM `student` ORDER BY age asc
6.9 Limit
å¯以éÂÂå¶æÂ¥æ¾æ¡æ°ï¼Âæ¯Âå¦Âä¸Âé¢è¿Â个ï¼Âå¦ÂæÂÂæ³è¦Âåªæ¾åºåÂÂä¸Âæ¡çÂÂè¯Âï¼Âå¯以è¿Âæ ·åÂÂï¼Â
result := []Student{}
err = db.Limit(3).Order("age asc").Select("Age", "Name").Find(&result).Error
if err != nil {
ààpanic("æ¥询失败, error = " + err.Error())
}
for _, i := range result {
ÃÂ ÃÂ fmt.Println(i.Name, i.Age)
}
è¾Âåºï¼Â
zhangsan 18
feixin 18
lisi 19
çÂÂäºÂï¼Â
SELECT `age`,`name` FROM `student` ORDER BY age asc LIMIT 3
6.10 Offset
è¿Â个å¯以è¿Â滤æÂÂÃ¥ÂÂå 个ï¼Âè¿Âæ¯以ä¸Âé¢çÂÂ为ä¾Âï¼Âå¦ÂæÂÂæÂÂæ³ä¸Âè¦Âzhangsan å feixinï¼ÂÃ¥ÂÂÃ¥ÂÂé¢ä¸Â个ï¼Âé£å¯以è¿Âæ ·åÂÂ:
result := []Student{}
err = db.Offset(2).Limit(3).Order("age asc").Select("Age", "Name").Find(&result).Error
if err != nil {
ààpanic("æ¥询失败, error = " + err.Error())
}
for _, i := range result {
ÃÂ ÃÂ fmt.Println(i.Name, i.Age)
}
è¾Âåºï¼Â
lisi 19
wangwu 20
xuliu 21
çÂÂäºÂï¼Â
SELECT `age`,`name` FROM `student` ORDER BY age asc LIMIT 3 OFFSET 2
æÂÂ没æÂÂä¸Âç§ÂÃ¥ÂÂå¤Âå¥Âå¨ÂçÂÂ赶èÂÂï¼Âæ¯è¿Âæ ·çÂÂ没éÂÂãÂÂð¤Â
6.11 Count
è¿Â个就æ¯è¿ÂÃ¥ÂÂæ¥询ç¸对åºÂäºÂå¤Âå°Âæ¡è¡Âæ°ï¼Âæ¯Âå¦ÂæÂÂçÂÂ表éÂÂæÂÂ两个18å²ÂçÂÂå°Âä¼ÂÃ¥ÂÂï¼ÂæÂ¥æÂ¥ä»Â们çÂÂ
var count int64
err = db.Model(&Student{}).Where("age = 18").Count(&count).Error
// è¿ÂéÂÂè®°å¾Âwhereè¦ÂÃ¥ÂÂå¨countÃ¥ÂÂé¢
if err != nil {
ààpanic("æ¥询失败, error = " + err.Error())
}
fmt.Println(count)
è¾Âåºï¼Â
2
çÂÂäºÂï¼Â
SELECT count(*) FROM `student` WHERE age = 18
6.12 Group 丠Having
Group
å Having
ä¸ÂèÂÂÃ¥ÂÂ使ç¨ï¼ÂÃ¥ÂÂsqlä¸Âä¹Âæ¯ä¸Âæ ·çÂÂï¼Âå 为where
没æ³Â对èÂÂÃ¥ÂÂå½æ°è¿Âè¡ÂæÂÂä½Â
å¨ GORM ä¸Âï¼Â使ç¨ Group
å½æ°æ¶ï¼ÂéÂÂ常éÂÂè¦ÂæÂÂéÂ
 Select
å½æ°ä¸Â起使ç¨ï¼Â以æÂÂå®Âä½ å¸ÂæÂÂå¨åÂÂç»ÂæÂÂä½Âä¸ÂéÂÂæ©çÂÂÃ¥ÂÂ段ãÂÂè¿Âæ¯å 为å¨ SQL æ¥询ä¸Âï¼Â使ç¨ GROUP BY
è¿Âè¡ÂÃ¥ÂÂç»Âæ¶ï¼ÂéÂÂ常éÂÂè¦ÂæÂÂç¡®æÂÂå®ÂÃ¥ÂÂç»ÂÃ¥ÂÂæÂÂéÂÂçÂÂÃ¥ÂÂ段ãÂÂ
æÂÂ以Group
, Having
, Select
ä¸ÂèÂÂ
ä¸Âè¬ä¸Â起使ç¨ãÂÂ
ç®ÂèÂÂè¨Âä¹Âï¼Âå°±æ¯Select
ç¨äºÂèÂÂéÂÂå½æ°ï¼Âç¶åÂÂGroup
Ã¥ÂÂç»Âï¼ÂÃ¥ÂÂç¨Having
è¿Âè¡ÂçÂÂéÂÂ
æ¯Âå¦ÂæÂÂç°å¨è¦Â对æÂÂçÂÂ表ä¸ÂçÂÂÃ¥ÂÂ个 age
çÂÂ人æ°è¿Âè¡Âç»Â计ï¼Âé¦ÂÃ¥Â
ÂæÂÂçÂÂ表ä¸Â18çÂÂæÂÂ两人ï¼Â19,20,21çÂÂÃ¥ÂÂä¸Â人
type Result struct {
ÃÂ ÃÂ Age ÃÂ ÃÂ int
ÃÂ ÃÂ Numbers int
}
var results []Result
err = db.Model(&Student{}).Select("age, count(*) as numbers").
ÃÂ ÃÂ Group("age").Having("numbers > 0").Find(&results).Error
if err != nil {
ààpanic("æ¥询失败, error = " + err.Error())
}
fmt.Println(results)
è¿ÂéÂÂæÂÂæ¯å®Âä¹ÂäºÂä¸Â个Result
ç»ÂæÂÂä½ÂÃ¥ÂÂå¨ç»ÂæÂÂï¼ count(*) as numbers
æ¯计ç®Âè¿Âä¸ÂÃ¥ÂÂçÂÂæ°ç®ï¼Â并åÂÂä¸Â个å«åÂÂ为numbers
ï¼ è¿ÂéÂÂæÂÂæÂÂè¿ÂäºÂé½è¦Âç¨å¼Âå·æ¬起æÂ¥ï¼Âå 为å®ÂéÂÂ
ä¸Âä»Âå°±æ¯è¦Â转åÂÂ为sqlè¯Âå¥ï¼Âå¯以ä»Âç»Â对çÂ
§ä¸Â丠ä¸ÂæÂÂä¸ÂçÂÂçÂÂäºÂÃ¥ÂÂ¥sqlè¯Âå¥ï¼ÂãÂÂæÂÂÃ¥ÂÂèÂÂæ¹æ³Âç¨Find
Ã¥ÂÂç»ÂæÂÂ并èµÂå¼ç»ÂäºÂresults
@注æÂÂè¿ÂéÂÂèµÂå¼ä¹Âæ¯æ å°ÂèµÂå¼ï¼Âä½ ç»ÂæÂÂä½ÂéÂÂé¢çÂÂÃ¥ÂÂ段åÂÂä¸Âå®Âè¦Â大åÂÂãÂÂ
è¾Âåºï¼Â
[{18 2} {19 1} {20 1} {21 1}]
çÂÂäºÂï¼Â
SELECT age, count(*) as numbers FROM `student` GROUP BY `age` HAVING numbers > 0
6.13 Ã¥ÂÂçÂÂsqlè¯ÂÃ¥ÂÂ¥
å°±æ¯ä¸Âé¢çÂÂä¾ÂÃ¥ÂÂï¼Âå¦ÂæÂÂ使ç¨sqlè¯Âå¥çÂÂè¯Âï¼Âä¹Âå¯以è¿Âæ ·åÂÂ
type Result struct {
ÃÂ ÃÂ Age ÃÂ ÃÂ int
ÃÂ ÃÂ Numbers int
}
var results []Result
sql := " SELECT age, count(*) as numbers FROM `student` GROUP BY `age` HAVING numbers > 0"
err = db.Raw(sql).Find(&results).Error // è¿Â个Find åºÂ该è¦Âæ¢æ Scan
if err != nil {
ààpanic("æ¥询失败, error = " + err.Error())
}
fmt.Println(results)
ä¹ÂÃ¥ÂÂå¨å¢Â丠使ç¨ç Exec
æ¹æ³Âæ¯åªè¿ÂÃ¥ÂÂè¯Âå¥æ§è¡ÂæÂÂ
Ã¥Â括Âä¸Âä¼Âè¿ÂÃ¥ÂÂç»ÂæÂÂéÂÂï¼Âè¿ÂéÂÂéÂÂè¦Âæ¥询ç»ÂæÂÂï¼ÂæÂÂ们æ¹ç¨Raw
ç»ÂæÂÂæ¯ä¸Âæ ·çÂÂï¼Âå°±ä¸Â表äºÂï¼Âè¿ÂéÂÂ说ä¸Â丠Find
Ã¥ÂÂScan
Find
æ¹æ³Âï¼ÂFind
æ¹æ³Âç¨äºÂä»Âæ°æ®åºÂä¸Âæ¥询æ°æ®并å°Âç»ÂæÂÂæ å°Âå°æÂÂå®ÂçÂÂç»ÂæÂÂä½ÂÃ¥ÂÂçÂÂä¸ÂãÂÂå®ÂéÂÂç¨äºÂæ£常ç GORM æ¥询æÂÂä½Âï¼Âæ ¹æ®æ¡件ä»Âæ°æ®åºÂä¸Âæ£Âç´¢æ°æ®并è¿Âè¡Âæ å°ÂãÂÂScan
æ¹æ³Âï¼ÂScan
æ¹æ³Âç¨äºÂå°Âç»ÂæÂÂæ«æÂÂå°æÂÂå®ÂçÂÂç»ÂæÂÂä½ÂÃ¥ÂÂçÂÂä¸Âï¼ÂèÂÂä¸ÂéÂÂè¦Âè¿Âè¡Âæ¥询æÂÂä½ÂãÂÂå®ÂéÂÂ常ç¨äºÂæ§è¡ÂÃ¥ÂÂç SQL æ¥询ï¼Â并å°Âæ¥询ç»ÂæÂÂæ å°Âå°ç»ÂæÂÂä½Âä¸ÂãÂÂ
æÂÂæÂÂå°±æ¯Find
Ã¥ÂÂScan
å·®ä¸Âå¤Âï¼Âä½Âæ¯è¿ÂéÂÂæÂÂå¨使ç¨åÂÂçÂÂsqlè¯Âå¥ï¼ÂæÂÂ以åºÂ该使ç¨Scan
7. Ã¥ÂÂ
å 就å¾Âç®ÂÃ¥ÂÂäºÂï¼ÂæÂÂ们å¯以å ÂæÂ¥åºæÂ¥åÂÂå ï¼Âä¹Âå¯以根æ®主é®å é¤
è¿Âæ¯è¿Â张表
ç°å¨æÂÂè¦ÂæÂÂè¿Â个franticçÂÂ家ä¼Âç»Âå é¤
å¯以å Âæ¾å°ä»Â
s := Student{}
err = db.Where("externalCharacter = 'frantic'").Find(&s).Error
if err != nil {
ààpanic("æ¥询失败, error = " + err.Error())
}
err = db.Delete(s).Error
if err != nil {
ààpanic("å é¤失败, error = " + err.Error())
}
å¦ÂæÂÂçÂ¥éÂÂ主é®çÂÂè¯Âä¹Âå¯以ç´æÂÂ¥
err = db.Delete(&Student{}, 5).Error
if err != nil {
ààpanic("å é¤失败, error = " + err.Error())
}
çÂÂäºÂï¼Â
DELETE FROM `student` WHERE `student`.`id` = 5
8. æ¹
è¿Âæ¯è¿Â张表
8.1 Save
æÂÂ们å¯以ç´æ¥对ç»ÂæÂÂä½ÂçÂÂÃ¥ÂÂ段è¿Âè¡Âä¿®æ¹ç¶åÂÂè¿Âè¡Âsave
æÂÂä½Âæ å°Âå°mysqlä¸ÂÃ¥ÂÂæÂ¥æ´æ¹
æ¯Âå¦ÂæÂÂè¿ÂéÂÂæ feixin ç age æ¹为 19
s := Student{}
err = db.Where("id = ?", 5).Find(&s).Error
if err != nil {
ààpanic("æ¥询失败, error = " + err.Error())
}
s.Age = 19
err = db.Save(&s).Error
if err != nil {
ààpanic("æ´æ°失败, error = " + err.Error())
}
çÂÂäºÂï¼Â
UPDATE `student` SET `name`='feixin',`age`=19,`externalCharacter`='frantic' WHERE `id` = 5
8.2 Update
ä¸Âé¢çÂÂä¹Âå¯以è¿Âæ ·åÂÂ
s := Student{}
err = db.Where("id = ?", 5).Find(&s).Error
if err != nil {
ààpanic("æ¥询失败, error = " + err.Error())
}
err = db.Model(&s).Update("age", 19).Error
if err != nil {
ààpanic("æ´æ°失败, error = " + err.Error())
}
çÂÂäºÂï¼Â
UPDATE `student` SET `age`=19 WHERE `id` = 5
8.3 Updates
å¯以æ´æ°å¤ÂÃ¥ÂÂå¼
s := Student{}
err = db.Where("id = ?", 5).Find(&s).Error
if err != nil {
ààpanic("æ¥询失败, error = " + err.Error())
}
err = db.Model(&s).Updates(Student{
ÃÂ ÃÂ Name: "feixin",
ÃÂ ÃÂ Age: ÃÂ 18,
}).Error
if err != nil {
ààpanic("æ´æ°失败, error = " + err.Error())
}
çÂÂäºÂï¼Â
UPDATE `student` SET `name`='feixin',`age`=18 WHERE `id` = 5
8.4 表达å¼Â
æ¯Âå¦ÂæÂÂæÂÂæÂÂæÂÂ人çÂÂå¹´é¾Âé½å 1
err = db.Model(&Student{}).Where("age > ?", 0).Update("age", gorm.Expr("age + ?", 1)).Error
if err != nil {
ààpanic("æ´æ°失败, error = " + err.Error())
}
çÂÂäºÂï¼Â
UPDATE `student` SET `age`=age + 1 WHERE age > 0
å¨ GORM ä¸Âï¼Â使ç¨ Update
æ¹æ³Âæ¶ï¼ÂéÂÂè¦ÂæÂÂä¾Âä¸Â个æ¡件æÂ¥æÂÂå®Âè¦Âæ´æ°åªäºÂè®°å½ÂãÂÂæÂÂ以è¿ÂéÂÂéÂÂè¦ÂÃ¥ÂÂwhere
è¯Âå¥ï¼Âä¸Âç¶ä¼ÂæÂÂ¥errï¼Âä¹ÂÃ¥ÂÂçÂÂ没æÂÂ没æÂÂ使ç¨æ¯å 为Model
éÂÂå¡«çÂÂæ¯ç»ÂæÂÂä½Â对象ï¼Âå®ÂéÂÂ
ä¸Âå°±ç®Âå·²ç»ÂæÂÂå®ÂäºÂæ¡件ãÂÂ
å½Âç¶ï¼Âå¦ÂæÂÂä¸Âæ³使ç¨whereçÂÂè¯Âä¹Âæ¯æÂÂÃ¥ÂÂæ³ÂçÂÂï¼ÂæÂÂ们å¯以å¯ç¨ AllowGlobalUpdate
模å¼Â
err = db.Session(&gorm.Session{AllowGlobalUpdate: true}).Model(&Student{}).Update("age", gorm.Expr("age + ?", 1)).Error
if err != nil {
ààpanic("æ´æ°失败, error = " + err.Error())
}
çÂÂäºÂï¼Â
UPDATE `student` SET `age`=age + 1
9.æ»ç»Â
GORMï¼ÂGo Object Relational Mappingï¼ æ¯ Go ç¼Âç¨Âè¯Âè¨Âä¸ÂçÂÂä¸Â个 ORMï¼Â对象堳系æ å°Âï¼ÂåºÂï¼Âå®Âç¨äºÂç®ÂÃ¥ÂÂå¨ Go åºÂç¨ç¨ÂåºÂä¸Âä¸Â堳系åÂÂæ°æ®åºÂè¿Âè¡Â交äºÂçÂÂè¿Âç¨ÂãÂÂORM æ¯ä¸Âç§Âç¼Âç¨ÂæÂÂæ¯ï¼Âç¨äºÂå°Âæ°æ®åºÂ表åÂÂæ°æ®æ å°Âå°ç¨ÂåºÂä¸ÂçÂÂ对象ï¼Â以便以é¢åÂÂ对象çÂÂæ¹å¼ÂæÂÂä½Âæ°æ®åºÂãÂÂ
GORM çÂÂä½Âç¨ï¼Â
- ç®ÂÃ¥ÂÂæ°æ®åºÂæÂÂä½Âï¼ GORM æÂÂä¾ÂäºÂä¸Âç§Âæ´ç´è§ÂÃ¥ÂÂç®ÂÃ¥ÂÂçÂÂæ¹å¼ÂæÂ¥æ§è¡Âæ°æ®åºÂæÂÂä½Âï¼Â使å¼ÂÃ¥ÂÂ人åÂÂå¯以使ç¨é¢åÂÂ对象çÂÂè¯Âæ³Âï¼ÂèÂÂä¸ÂéÂÂè¦Âç¼ÂÃ¥ÂÂ大éÂÂç SQL æ¥询è¯Âå¥ãÂÂ
- æÂÂé«Âå¼ÂÃ¥ÂÂæÂÂçÂÂ@使ç¨ GORM å¯以åÂÂå°ÂæÂÂå¨ç¼Âå SQL æ¥询åÂÂå¤ÂçÂÂæ°æ®åºÂè¿ÂæÂ¥çÂÂå·¥ä½ÂéÂÂï¼Âä»ÂèÂÂæÂÂé«Âå¼ÂÃ¥ÂÂæÂÂçÂÂãÂÂ
- é¿堠SQL 注堥@GORM ä¼Âèªå¨å°ÂÃ¥ÂÂæ°åÂÂæ¥询åºÂç¨å° SQL æ¥询ä¸Âï¼Âä»ÂèÂÂÃ¥ÂÂå° SQL 注堥çÂÂé£Âé©ãÂÂ
- æ°æ®åºÂæ 堳æ§@GORM æ¯æÂÂå¤Âç§Âæ°æ®åºÂï¼Âå æ¬ MySQLãÂÂPostgreSQLãÂÂSQLite çÂÂï¼Â使å¾ÂåºÂç¨ç¨ÂåºÂå¨ä¸ÂÃ¥ÂÂæ°æ®åºÂä¹Âé´åÂÂæ¢æ´å 容æÂÂãÂÂ
- 模åÂÂå®Âä¹Âï¼ GORM å Â许你éÂÂè¿Âç»ÂæÂÂä½Âå®Âä¹Âæ°æ®模åÂÂï¼Âä»ÂèÂÂå°Âæ°æ®åºÂ表æ å°Âå° Go çÂÂç»ÂæÂÂä½Âä¸ÂãÂÂ
GORM çÂÂä¼Âç¹ï¼Â
- ç®ÂÃ¥ÂÂæ¥询æÂÂä½Âï¼ GORM æÂÂä¾ÂäºÂ丰å¯ÂçÂÂæ¥询æ¹æ³Âï¼Âå æ¾¡件æ¥询ãÂÂæÂÂåºÂãÂÂÃ¥ÂÂ页çÂÂï¼Â使å¾Âæ¥询æÂÂä½ÂÃ¥ÂÂå¾Âç®ÂÃ¥ÂÂæÂÂç¨ãÂÂ
- èªå¨æ å°Âï¼ GORM å¯以èªå¨å°Âæ°æ®åºÂ表çÂÂè®°å½Âæ å°Âå° Go ç»ÂæÂÂä½Âï¼Â大大åÂÂå°ÂäºÂæÂÂå¨çÂÂæ å°Âå·¥ä½ÂãÂÂ
- äºÂå¡æ¯æÂÂï¼ GORM æÂÂä¾ÂäºÂäºÂå¡管çÂÂÃ¥ÂÂè½ï¼Â使你è½å¤Â以ä¸Âç§Âå®Âå ¨çÂÂæ¹å¼Âæ§è¡Âæ°æ®åºÂæÂÂä½ÂãÂÂ
- é©åÂÂï¼ÂHooksï¼Âæ¯æÂÂï¼ GORM æ¯æÂÂå¨模åÂÂ对象çÂÂçÂÂå½å¨æÂÂäºÂ件ä¸Âæ§è¡Âé©åÂÂå½æ°ï¼Âä¾Âå¦Âå¨åÂÂ建ãÂÂæ´æ°ãÂÂå é¤çÂÂæÂÂä½ÂÃ¥ÂÂÃ¥ÂÂæ§è¡Âç¹å®ÂçÂÂé»è¾ÂãÂÂ
GORM çÂÂ缺ç¹ï¼Â
- æ§è½@ç¸对äºÂæÂÂå¨ç¼Âå SQL æ¥询ï¼ÂORM æ¡Âæ¶å¯è½ä¼Âå¼Âå ¥ä¸Âå®ÂçÂÂæ§è½å¼ÂéÂÂãÂÂ尤堶æ¯å¨大è§Â模æ°æ®æÂÂä½Âæ¶ï¼Âä¸ÂäºÂå¤ÂæÂÂæ¥询å¯è½ä¼Âå½±åÂÂæ§è½ãÂÂ
- å¦习æ²线@尽管 GORM è¯Âå¾ç®ÂÃ¥ÂÂæ°æ®åºÂæÂÂä½Âï¼Âä½Âä»Âç¶éÂÂè¦Âå¦习堶 API Ã¥ÂÂç¨æ³ÂãÂÂ对äºÂæ°æÂÂæ¥说ï¼ÂæÂÂæ¡ ORM å¯è½éÂÂè¦Âä¸ÂäºÂæ¶é´ãÂÂ
- çµ活æ§@æÂÂæ¶åÂÂå¤ÂæÂÂçÂÂæ¥询éÂÂæ±Âå¯è½æ æ³Âç´æ¥使ç¨ GORM æÂÂä¾ÂçÂÂæ¹æ³Âï¼ÂéÂÂè¦Âç¼ÂÃ¥ÂÂÃ¥ÂÂ姠SQL æ¥询æ¥满足ç¹å®ÂéÂÂæ±ÂãÂÂ
综åÂÂèÂÂèÂÂï¼ÂGORM å¨大å¤Âæ°æ åµä¸Âå¯以æ¾èÂÂç®ÂÃ¥ÂÂæ°æ®åºÂæÂÂä½Âï¼ÂæÂÂé«Âå¼ÂÃ¥ÂÂæÂÂçÂÂãÂÂç¶èÂÂï¼Âæ ¹æ®项ç®çÂÂç¹å®ÂéÂÂæ±Âï¼Âå¼ÂÃ¥ÂÂ人åÂÂéÂÂè¦ÂæÂÂ衡使ç¨ GORM çÂÂä¼Âå¿åÂÂå£å¿ï¼Â以确ä¿ÂéÂÂæ©äºÂéÂÂÃ¥ÂÂçÂÂ工堷ãÂÂ
好äºÂï¼Âå°è¿ÂéÂÂç®ÂÃ¥ÂÂçÂÂè¿ÂæÂ¥æ°æ®åºÂÃ¥ÂÂå¢Âå æ¹æÂ¥æÂÂä½Âé½åºæ¬å®ÂæÂÂäºÂï¼Âå¯以åÂÂç°gormæÂÂä½Âæ°æ®åºÂæ¯åÂÂÃ¥ÂÂ便å©çÂÂï¼ÂÃ¥ÂÂç§Âå½æ°å¥Âå¨Âå°±å¯以äºÂï¼Âå®Âå¨ä¸Âè¡ÂæÂÂ们è¿Âå¯以使ç¨åÂÂçÂÂsqlè¯Âå¥æÂ¥è¿Âè¡ÂæÂÂä½ÂãÂÂ
å¸ÂæÂÂæÂÂçÂÂæÂÂç« è½起å°ä½Âç¨å¦~ðÂÂÂ