ç®Âä»Â
å ³äºÂORMï¼ÂObject-Relational Mapping)
ORMÃ¥Â
¶å®ÂæÂÂçÂÂæ¯å°ÂÃ¥Â
³ç³»åÂÂæ°æ®åºÂä¸ÂçÂÂæ°æ®åÂÂé¢åÂÂ对象ç¨ÂåºÂä¸Â对象模åÂÂè¿Âè¡Âæ å°ÂçÂÂæÂÂæ¯ï¼Â
ORMå¯以ç¨æÂ¥èªå¨åÂÂå¤ÂçÂÂSQLè¯Âå¥çÂÂçÂÂæÂÂÃ¥ÂÂæ§è¡Âï¼Âç¨ÂåºÂÃ¥ÂÂå¯以æ´ä¸Â注äºÂä¸Âå¡é»è¾ÂçÂÂå®Âç°èÂÂä¸Âæ¯æ°æ®çÂÂç»ÂèÂÂãÂÂ
为ä»Âä¹ÂéÂÂè¦ÂORMï¼Âï¼ÂÃ¥ÂÂèÂÂgptï¼Âç»ÂÃ¥ÂÂèª身使ç¨è¿Âç¨Âï¼Â
- æÂÂé«Âå¼ÂÃ¥ÂÂæÂÂçÂÂï¼Âèªå¨åÂÂçÂÂæÂÂSQLï¼ÂÃ¥ÂÂå°ÂæÂÂå¨ç¼ÂÃ¥ÂÂSQLæ¶é´ï¼Â
- åªéÂÂè¦Âå®Âä¹Â好模åÂÂï¼Âå¯以èªå¨å¤ÂçÂÂä¸ÂÃ¥ÂÂæ°æ®åºÂä¹Âé´çÂÂå·®å¼Âï¼Â(å¦ÂæÂÂä¼ ç»Âç¼ÂÃ¥ÂÂï¼Âæ¢æ°æ®åºÂç¸å½ÂäºÂéÂÂè¦Âå§åÂÂæÂ¥çÂÂé»è¾ÂéÂÂÃ¥ÂÂä¸ÂéÂÂ)
- æÂÂäºÂç»´æ¤ï¼Âæ¹便æÂÂå±Âï¼Âä¼ ç»Âç¼ÂÃ¥ÂÂSQLå¦ÂæÂÂå¤Âèµ·æÂ¥ï¼ÂæÂÂæÂ¥é®é¢ÂÃ¥ÂÂéÂÂæÂÂæ¶å¾ÂçÂÂè¦ï¼Âï¼Â
- å®Âå ¨ï¼ÂÃ¥ÂÂæ°åÂÂæ¥询é¿å ÂSQL注堥
å ³äºÂGorm
Gormæ¯ä¸Â个åÂ
¨åÂÂè½çÂÂORMæ¡Âæ¶ï¼Â主è¦ÂéÂÂ对Goè¯Âè¨ÂèÂÂå¼ÂÃ¥ÂÂï¼Âæ¯æÂÂå¤ÂçÂÂ主æµÂçÂÂÃ¥Â
³ç³»åÂÂæ°æ®åºÂï¼ÂMySQLãÂÂPostgreSQLãÂÂSQL Server)
以åÂÂä¸ÂäºÂNoSQLæ°æ®åºÂãÂÂç±äºÂè¿Âè¾¹æ¯ç´æ¥使ç¨çÂÂæ¯Gorm 2ï¼ÂÃ¥Â
³äºÂGormçÂÂéÂÂè¦Âç¹æ§å¯以以gormå®Âç½Â为åÂÂï¼Âå¯以ç´æÂ¥åÂÂèÂÂï¼Â
å®Â裠åÂÂ设置
å¿«éÂÂå®Â裠Gorm2ï¼ÂMysql驱å¨
go get gorm.io/gorm
go get gorm.io/driver/mysql
Ã¥ÂÂå§ÂÃ¥ÂÂ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{})
}
å¤Â注ï¼Â
- å®Âä¹ÂDSNï¼ÂData Source Name)è¿ÂæÂ¥æ°æ®åºÂÃ¥ÂÂ符串dsnï¼Â
- ä¼ å ¥MySQL驱å¨ï¼ÂÃ¥ÂÂå§ÂÃ¥ÂÂgormæ°æ®åºÂè¿ÂæÂ¥ï¼Â
è¿Âæ¯æÂÂèª己å®Âä¹ÂÃ¥ÂÂå§ÂÃ¥ÂÂè¿ÂæÂ¥ï¼Â读åÂÂ对åºÂé Âç½®æÂÂ件
type Config struct {
MysqlConfig DBConfig `json:"mysql"`
}
type DBConfig struct {
Username string `json:"username"`
Password string `json:"password"`
Host string `json:"host"`
Port int `json:"port"`
DBName string `json:"prefix"`
}
func initDB() (db *gorm.DB, err error) {
confPath := "conf.json"
if _, err = os.Stat(confPath); err != nil {
return
}
var config Config
if err = configor.Load(&config, confPath); err != nil {
return
}
// æ°建Database Gormè¿ÂæÂÂ¥
mysqlConfig := &config.MysqlConfig
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4",
mysqlConfig.Username, mysqlConfig.Password, mysqlConfig.Host,
mysqlConfig.Port, mysqlConfig.DBName)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
return
}
func main() {
db, err := initDB()
if err != nil {
log.Fatalf(err.Error())
return
}
db = db.Debug()
execSQL(db)
}
å¤Â注ï¼Â
- è¿Âè¾¹æÂÂå¨项ç®ä¸Âå®Âä¹ÂäºÂå ³äºÂæ°æ®åºÂè¿ÂæÂ¥çÂÂç»ÂæÂÂä½Âï¼ÂéÂÂè¿Â读åÂÂé¢Â设好çÂÂconfig.jsonæÂÂ件ï¼Âå 载好é¢Â设çÂÂé Â置信æ¯ï¼Â
- å¦å¤Âè¿Âè¾¹çÂÂdb = db.Debug()ï¼Âå¯以å¼Âå¯dbçÂÂDEBUG模å¼Âï¼Âè½å¤ÂæÂÂå°åºæ§è¡ÂçÂÂSQLè¯Âå¥以åÂÂ对åºÂçÂÂÃ¥ÂÂæ°ï¼Â以æ¹便è°Âè¯Âï¼Â
config.jsonÃ¥ÂÂèÂÂ
{
"mysql": {
"username": "root",
"password": "root",
"host": "127.0.0.1",
"prefix": "GormDemo",
"port": 3306
}
}
å®Âä¹Â模åÂÂ
ç±äºÂGorm模åÂÂçÂÂç»ÂæÂÂä½ÂÃ¥ÂÂçÂÂèÂÂå½¢å¤Âæ°ä½Â为表åÂÂï¼ÂÃ¥ÂÂ段åÂÂçÂÂèÂÂå½¢ä½Â为åÂÂÃ¥ÂÂï¼Âå¦ÂæÂÂMysql表çÂÂ设计éµ循äºÂGORM约å®Âï¼ÂÃ¥ÂÂå¯å°ÂÃ¥ÂÂå¾Âå¤Â代ç Âï¼Â
ä½Âæ¯å®ÂéÂÂ
æÂÂ
Ã¥Â括Âå¾Âå¾Âä¸Âæ¯è¿Âæ ·ï¼Â
type User struct {
ID string `gorm:"column:id" faker:"-"`
Email string `gorm:"column:email" faker:"email"`
Password string `gorm:"column:password" faker:"password"`
PhoneNumber string `gorm:"column:phone_number" faker:"phone_number"`
UserName string `gorm:"column:username" faker:"username"`
FirstName string `gorm:"first_name" faker:"first_name"`
LastName string `gorm:"last_name" faker:"last_name"`
Century string `gorm:"century" faker:"century"`
Date string `gorm:"date" faker:"date"`
}
func (u User) TableName() string {
return "user"
}
å¤Â注ï¼Â
- è¿Âæ¯æÂÂå®Âä¹ÂçÂÂç»ÂæÂÂä½ÂUserï¼ÂéÂÂè¿Âå®Âç°TableræÂ¥å£æÂ¥æ´æ¹é»Â认表åÂÂï¼ÂTableName()æ¹æ³Âä¼Âå°ÂMusic表åÂÂéÂÂÃ¥ÂÂ为user(å¦ÂæÂÂä¸Âä¿®æ¹åÂÂä¼Âé»Â认为users);
- ä¿®æ¹对åºÂÃ¥ÂÂ段对åºÂæ°æ®çÂÂÃ¥ÂÂÃ¥ÂÂï¼Âä¿®æ¹æ Âç¾columnï¼Â
- 模åÂÂä¸ÂçÂÂfakerçÂÂæ Âç¾å¯以忽çÂ¥ï¼Âè¿Â边主è¦Âæ¯çÂÂæÂÂÃ¥ÂÂæ°æ®æÂÂä¼Â使ç¨å°çÂÂï¼Â
CRUD
Ã¥ÂÂ建
Ã¥ÂÂæÂÂè®°å½ÂæÂÂå ¥
// insertOneRow Ã¥ÂÂæÂÂè®°å½ÂæÂÂÃ¥Â
Â¥
func insertOneRow(db *gorm.DB){
var tmpUser *User
// çÂÂæÂÂéÂÂæºæ°æ®
err := faker.FakeData(&tmpUser)
if err != nil {
log.Fatalf(err.Error())
}
result := db.Create(tmpUser)
if result.Error != nil {
log.Fatalf(result.Error.Error())
}
fmt.Println("result RowsAffected: ", result.RowsAffected)
fmt.Printf("%+v\n", tmpUser)
}
å¤Â注ï¼Â
- ç±äºÂæ³çÂÂæÂÂÃ¥ÂÂæµÂè¯Âæ°æ®ï¼ÂæÂÂ以使ç¨äºÂgithub.com/go-faker/fakerçÂÂæÂÂÃ¥ÂÂæ°æ®ï¼ÂæÂÂ場趣å¯以äºÂ解ï¼ÂÃ¥ÂÂÃ¥ÂÂé¢çÂÂç»ÂæÂÂä½Âå®Âä¹ÂTagä¹ÂÃ¥ÂÂå°±è½çÂÂæÂÂ对åºÂçÂÂÃ¥ÂÂæ°æ®ï¼Â
- åºæÂŒÂÂ建åÂÂæ¡记å½Â没å¥好说çÂÂï¼Âåªè¦Âå®Âä¹Â模åÂÂçÂÂç»ÂæÂÂä½Âï¼Â补堠æ°æ®ï¼Âç¶åÂÂæÂÂå ¥å³å¯ï¼Â
æ¹éÂÂè®°å½ÂæÂÂå ¥
// insertRows æ¹éÂÂæÂÂÃ¥Â
Â¥
func insertRows(db *gorm.DB) {
var users []*User
for i := 0; i < 10; i++ {
tmpUser := User{}
err := faker.FakeData(&tmpUser)
if err != nil {
log.Fatal(err.Error())
}
users = append(users, &tmpUser)
}
result := db.Create(users)
if result.Error != nil {
log.Fatalf(result.Error.Error())
}
fmt.Println("RowsAffected: ", result.RowsAffected)
for _, m := range users {
fmt.Printf("%+v\n", m)
}
}
å®Âä¹Âé©åÂÂå½æ°
func (u *User) BeforeCreate(tx *gorm.DB) (err error){
u.ID = uuid.New()
return nil
}
å¤Â注ï¼Â
- å¨æÂÂ堥记å½Âä¹ÂÃ¥ÂÂï¼ÂçÂÂæÂÂuuidï¼Âå ·ä½Âæ ¹æ®å®Âé ä¸Âå¡åºæ¯补堠å¤ÂçÂÂçÂÂä¸Âå¡é»è¾Âï¼Âå æ¬æ£ÂéªÂçÂÂï¼Â
- å®Âæ¹è¿ÂæÂÂä¾ÂæÂÂä¾Âå¾Âå¤Âé¢Âå¤ÂçÂÂé©åÂÂå½æ°ï¼ÂBeforeSaveï¼ÂAfterSaveçÂÂï¼Âå ·ä½ÂÃ¥ÂÂèÂÂå®Âæ¹æÂÂä¾ÂçÂÂæÂÂæ¡£
æ¥询
ç®ÂÃ¥ÂÂæ¥询
func printRecord(u *User, result *gorm.DB){
fmt.Printf("%v\n", u)
fmt.Println(result.Error, result.RowsAffected)
}
func printRecords(u []*User, result *gorm.DB){
for _, u := range u {
fmt.Println(u)
}
fmt.Println(result.Error, result.RowsAffected)
}
func simpleQueryRow(db *gorm.DB){
// æ¥询第ä¸Âæ¡记å½Âï¼Â主é®åÂÂåºÂï¼Â
var firstUser *User
result := db.First(&firstUser)
printRecord(firstUser, result)
// ä»Â
å½ÂæÂÂä¸Â个ID主é®æ¶ï¼Âå¯ç´æÂ¥å®Âä¹ÂUseræ¶æÂÂIDÃ¥ÂÂå§ÂÃ¥ÂÂ
firstIDUser2 := &User{ID: "e8efff22-a497-4a88-be1e-5123eb23ff75"}
result = db.First(&firstIDUser2)
printRecord(firstIDUser2, result)
// æ¥询表ä¸Â第ä¸Âæ¡记å½Âï¼Â没æÂÂæÂÂå®ÂæÂÂåºÂÃ¥ÂÂ段ï¼Â
var firstUser2 *User
result = db.Take(&firstUser2)
printRecord(firstUser2, result)
// æ¥询表ä¸ÂæÂÂÃ¥ÂÂä¸Âæ¡记å½Âï¼Â主é®æÂÂåºÂï¼Â
var lastUser *User
result = db.Last(&lastUser)
printRecord(lastUser, result)
// æ¥询å½ÂÃ¥ÂÂæÂÂæÂÂè®°å½Â
var users []*User
result = db.Find(&users)
printRecords(users, result)
}
å¤Â注ï¼Â
- è¿Âå 个é½æ¯æÂÂç §å®Âæ¹ç»ÂåºçÂÂ示ä¾Âï¼Âæ´çÂÂäºÂä¸Âä¸Âæ¥常æ¯Âè¾Â常ç¨å°çÂÂæ¥询æÂÂä½Â
æ¡件æ¥询
func condQueryRow(db *gorm.DB){
// æ¥询å½ÂÃ¥ÂÂusername为condQueryRowçÂÂ第ä¸Âæ¡记å½Âï¼ÂStructæ¹å¼Âï¼Â
var tmpUser1 *User
result := db.Where(&User{UserName: "qNptxqb"}).First(&tmpUser1)
printRecord(tmpUser1, result)
// æ¥询å½ÂÃ¥ÂÂusername为condQueryRowçÂÂ第ä¸Âæ¡记å½Âï¼ÂMapæ¹å¼Âï¼Â
var tmpUser2 *User
result = db.Where(map[string]interface{}{"username": "qNptxqb"}).First(&tmpUser2)
printRecord(tmpUser2, result)
// æÂÂå®ÂCenturyæ¥询åÂÂ段æ¥询记å½Â
var tmpUser3 []User
result = db.Where(&User{Century: "VII", UserName: "jaQlaFs"}, "Century").Find(&tmpUser3)
printRecords(tmpUser3, result)
// String æ¡件ï¼Âç´æÂ¥åÂÂ表达å¼Â
var tmpUser4 *User
result = db.Where("username = ?", "qNptxqb").First(&tmpUser4)
printRecord(tmpUser4, result)
var users []User
result = db.Where("date > ?", "2010-10-1").Find(&users)
printRecords(users, result)
// OrderæÂÂåºÂï¼Âé»Â认åÂÂåºÂï¼Â
var users2 []User
result = db.Where("date > ?", "2010-10-1").Order("date").Find(&users2)
printRecords(users2, result)
// æ¥询ç¹å®ÂçÂÂÃ¥ÂÂ段ï¼Âä¸Âè¿ÂÃ¥ÂÂæÂÂæÂÂÃ¥ÂÂ段
var tmpUser5 *User
result = db.Select("username", "date").Where("username = ?", "qNptxqb").First(&tmpUser5)
printRecord(tmpUser5, result)
}
å¤Â注ï¼Â
- GormæÂÂä¾ÂäºÂå¾Âå¤Âæ¡件æ¥询çÂÂåºæ¯ï¼Âç®åÂÂ对æÂÂèÂÂè¨ÂåºæÂÂæ¥询ä¸Âå¡é»è¾Âé½è½æ¯æÂÂãÂÂå ·ä½Âå¯è½åºç°ä¸ÂäºÂå¤ÂæÂÂçÂÂsqlï¼ÂÃ¥ÂÂé¢ä¼Âä»Âç»ÂæÂÂä¹Âç´æ¥使ç¨sqlæ¥询ï¼Â
é«Â级æ¥询
type APIUser struct {
ID string `gorm:"primaryKey,column:id"`
UserName string `gorm:"column:username"`
FirstName string `gorm:"first_name"`
LastName string `gorm:"last_name"`
}
func advancedQueryRow(db *gorm.DB){
// æºè½éÂÂæ©åÂÂ段ï¼Âå¦ÂæÂÂç»Â常åªéÂÂè¦Âæ¥询æÂÂäºÂÃ¥ÂÂ段ï¼Âå¯以éÂÂæ°å®Âä¹Âå°Âç»ÂæÂÂä½Â
var apiUser []APIUser
result := db.Model(&User{}).Find(&apiUser)
for _, user := range apiUser{
fmt.Println(user)
}
fmt.Println(result.Error, result.RowsAffected)
// æ«æÂÂç»ÂæÂÂç»Âå®Âå¼map[string]interface{} æÂÂèÂÂ
[]map[string]interface{}
var users []map[string]interface{}
result = db.Model(&User{}).Find(&users)
for _, user := range users{
fmt.Println(user)
}
fmt.Println(result.Error, result.RowsAffected)
// Pluckæ¥询åÂÂ个åÂÂï¼Â并å°Âç»ÂæÂÂæ«æÂÂå°åÂÂçÂÂ
var emails []string
result = db.Model(&User{}).Pluck("email",&emails)
fmt.Println(emails)
fmt.Println(result.Error, result.RowsAffected)
// Countæ¥询
var count int64
result = db.Model(&User{}).Where("date > ?", "2012-10-22").Count(&count)
fmt.Println(count)
fmt.Println(result.Error, result.RowsAffected)
}
å¤Â注ï¼Â
- å®Âä¹Âå°Âç»ÂæÂÂä½Âå¯以å®Âç°å¨è°Âç¨APIæ¶èªå¨éÂÂæ©ç¹å®ÂÃ¥ÂÂ段
- PluckéÂÂÃ¥ÂÂæ¥询åÂÂ个åÂÂï¼Âå¦ÂæÂÂéÂÂè¦Âæ¥询å¤Â个常ç¨åÂÂ段å¯以éÂÂè¿ÂSelectÃ¥ÂÂScan
æ´æ°
常ç¨æ´æ°æÂÂä½Â
func updateRow(db *gorm.DB){
// Saveä¼Âä¿ÂÃ¥ÂÂæÂÂæÂÂÃ¥ÂÂ段ï¼Âå³使åÂÂ段æ¯é¶å¼ï¼Âå¦ÂæÂÂä¿ÂÃ¥ÂÂçÂÂå¼没æÂÂ主é®ï¼Âå°±ä¼ÂÃ¥ÂÂ建ï¼Âå¦åÂÂÃ¥ÂÂæ¯æ´æ°æÂÂå®Âè®°å½Â
result := db.Save(&User{ID: "e8efff22-a497-4a88-be1e-5123eb23ff75", UserName: "zhangsan", Date: "2023-12-12"})
fmt.Println(result.Error, result.RowsAffected)
// æ´æ°åÂÂ个åÂÂ
result = db.Model(&User{}).Where("username = ?", "jaQlaFs").Update("first_name", "zhangsan")
fmt.Println(result.Error, result.RowsAffected)
// æ´æ°å¤Â个åÂÂ
result = db.Model(&User{}).Where("username = ?", "zhangsan").Updates(User{FirstName: "zhangsan2", LastName: "zhangsan3"})
fmt.Println(result.Error, result.RowsAffected)
// æ´æ°æÂÂå®ÂÃ¥ÂÂ(SelectæÂÂå®Âlast_name)
result = db.Model(&User{}).Where("username = ?", "zhangsan").Select("last_name").Updates(User{FirstName: "zhangsan2", LastName: "zhangsan4"})
fmt.Println(result.Error, result.RowsAffected)
}
å¤Â注ï¼Â
- å ·ä½Âä¸ÂäºÂæ´æ°çÂÂæÂÂä½ÂÃ¥ÂÂæ¥询类似ï¼Âå ·ä½ÂåºåÂÂå³å¯ï¼Â
å é¤
常ç¨å é¤æÂÂä½Â
func deleteRows(db *gorm.DB){
// æÂÂå®Âå¹éÂ
ÂÃ¥ÂÂ段å é¤æ°æ®
result := db.Delete(&User{}, map[string]interface{}{"username": "NJrauTj"})
fmt.Println(result.Error, result.RowsAffected)
result = db.Delete(&User{}, "username = ?", "NJrauTj")
fmt.Println(result.Error, result.RowsAffected)
// WhereæÂÂå®ÂÃ¥ÂÂ段å¹éÂ
Âå é¤æ°æ®
result = db.Where("username = ? and phone_number = ?", "jXQKmPv", "574-821-9631").Delete(&User{})
fmt.Println(result.Error, result.RowsAffected)
// æ¹éÂÂå é¤çÂÂ两ç§Âæ¹å¼Â
result = db.Where("email like ?", "%.com%").Delete(&User{})
fmt.Println(result.Error, result.RowsAffected)
result = db.Delete(&User{}, "email like ?", "%.com%")
fmt.Println(result.Error, result.RowsAffected)
}
å¤Â注ï¼Â
- æ®éÂÂå é¤常ç¨åºæ¯å¹é ÂæÂÂå®ÂÃ¥ÂÂæÂÂæ°æ®å é¤以åÂÂæ¹éÂÂå é¤ï¼Âè¯Âæ³ÂÃ¥ÂÂæ´æ°类似ï¼Â
- GormæÂÂæ¡£ä¸ÂæÂÂæ¶ÂÃ¥ÂÂç¦Âç¨åÂ
¨å±Âå é¤ï¼Âå³å½Âæ§è¡Âä¸Â带任ä½Âæ¡件çÂÂæ¹éÂÂå é¤æ¶就ä¼Âè¿ÂÃ¥ÂÂéÂÂ误ï¼Â以åÂÂÃ¥Â
³äºÂå é¤çÂÂé©åÂÂå½æ°æÂÂå®Âç¨
åºæ¯çÂÂÃ¥ÂÂå¦ï¼Âå¯以çÂÂå®Âæ¹æÂÂæ¡£ï¼Âè¿ÂéÂÂä¸ÂÃ¥ÂÂèµÂè¿°ï¼Â
Ã¥ÂÂçÂÂSQLÃ¥ÂÂSQLçÂÂæÂÂå¨
// execSQL æ§è¡ÂÃ¥ÂÂçÂÂSQLè¯ÂÃ¥ÂÂ¥
func execSQL(db *gorm.DB){
// å°Âæ¥询SQLçÂÂç»ÂæÂÂæ å°Âå°æÂÂå®ÂçÂÂÃ¥ÂÂ个åÂÂéÂÂä¸Â
var oneUser User
result := db.Raw("SELECT * FROM user LIMIT 1").Scan(&oneUser)
fmt.Println(oneUser)
fmt.Println(result.Error, result.RowsAffected)
// å°Âæ¥询SQLçÂÂæ¹éÂÂç»ÂæÂÂæ å°Âå°åÂÂ表ä¸Â
var users []User
result = db.Raw("SELECT * FROM user").Scan(&users)
for _, user := range users {
fmt.Println(user)
}
fmt.Println(result.Error, result.RowsAffected)
var updateUser User
result = db.Raw("UPDATE users SET username = ? where id = ?", "toms jobs", "ab6f089b-3272-49b5-858f-a93ed5a43b4f").Scan(&updateUser)
fmt.Println(updateUser)
fmt.Println(result.Error, result.RowsAffected)
// ç´æÂ¥éÂÂè¿ÂExecå½æ°æ§è¡ÂUpdateæÂÂä½Âï¼Âä¸Âè¿ÂÃ¥ÂÂä»»ä½Âæ¥询ç»ÂæÂÂï¼Â
result = db.Exec("UPDATE user SET username = ? where id = ?", "toms jobs", "ab6f089b-3272-49b5-858f-a93ed5a43b4f")
fmt.Println(result.Error, result.RowsAffected)
// DryRun模å¼Âï¼Âå¨ä¸Âæ§è¡ÂçÂÂæÂÂ
åµä¸ÂçÂÂæÂÂSQLÃ¥ÂÂÃ¥Â
¶åÂÂæ°ï¼Âå¯以ç¨äºÂÃ¥ÂÂå¤ÂæÂÂæµÂè¯ÂçÂÂSQL
var tmpUsers []APIUser
stmt := db.Session(&gorm.Session{DryRun: true}).Model(&User{}).Find(&tmpUsers).Statement
fmt.Println(stmt.SQL.String())
fmt.Println(stmt.Vars)
}
å¤Â注ï¼Â
- Scanå½æ°æ¯ä¼Âå°Âæ¥询SQLçÂÂç»ÂæÂÂæ å°Âå°å®Âä¹ÂçÂÂÃ¥ÂÂéÂÂï¼Âå¦ÂæÂÂä¸ÂéÂÂè¦Âè¿ÂÃ¥ÂÂæ¥询ç»ÂæÂÂå¯以ç´æ¥使ç¨Execå½æ°æ§è¡ÂÃ¥ÂÂçÂÂSQLï¼Â
- DryRun模å¼Âï¼Âå¯以ç´æÂ¥çÂÂæÂÂSQLæºå¨åÂÂæ°ï¼Âä½Âæ¯ä¸Âä¼Âç´æÂ¥æ§è¡Âï¼Â
æ»ç»Â
å ³äºÂGormçÂÂCURDæ¥常使ç¨就ä»Âç»Âå°è¿ÂéÂÂï¼Âå¦ÂæÂÂÃ¥ÂÂå¦对GormæÂÂ場趣ï¼Âå¯以æÂ¥å»Gormå®Âç½Âï¼ÂæÂÂè¿ÂéÂÂåªæ¯ç®ÂÃ¥ÂÂä»Âç»Âä¸Âä¸Âå¨æ¥常ä¸Âå¡ç¯å¢Âç»Â常使ç¨çÂÂæÂÂä½Âï¼Âå ·ä½ÂGormä¸ÂæÂÂæÂÂæ¡£å°åÂÂãÂÂ
è³äºÂGormÃ¥ÂÂé¢å¯è½è¿Âä¼Âåºå ç¯ÂæÂÂç« ä»Âç»ÂGorm模åÂÂä¹Âé´çÂÂå ³èÂÂ堳系ï¼Â以åÂÂå ³èÂÂ模å¼Âä¸ÂçÂÂCRUDï¼Âè¿ÂæÂÂå ³äºÂGormçÂÂæ§è½ä¼ÂÃ¥ÂÂï¼Âå ·ä½Âè½æÂÂé Âä¸ÂäºÂ常ç¨çÂÂæÂÂ件ãÂÂ
ä¸Âè¿°æÂÂç« çÂÂæºÂç Âï¼Â