全景网站app,电商网站设计公司皆选亿企邦,建设部网站有建筑施工分包,wordpress新闻小程序GORM#xff08;Go ORM#xff0c;即对象关系映射#xff09;是Go语言中非常流行且功能强大的数据库交互库。它简化了与关系型数据库的交互过程#xff0c;提供了丰富的API来处理各种数据库操作。下面将详细介绍GORM的功能、使用方法和一些高级特性。
1. 安装
首先#…GORMGo ORM即对象关系映射是Go语言中非常流行且功能强大的数据库交互库。它简化了与关系型数据库的交互过程提供了丰富的API来处理各种数据库操作。下面将详细介绍GORM的功能、使用方法和一些高级特性。
1. 安装
首先确保安装了GORM以及相应的数据库驱动
go get -u gorm.io/gorm
go get -u gorm.io/driver/mysql # MySQL驱动示例
2. 连接数据库
连接到数据库时您需要提供数据源名称DSN这通常包括用户名、密码、主机地址、端口和数据库名等信息。对于MySQL可以这样做
import (gorm.io/driver/mysqlgorm.io/gorm
)func main() {dsn : user:passwordtcp(127.0.0.1:3306)/dbname?charsetutf8mb4parseTimeTruelocLocaldb, err : gorm.Open(mysql.Open(dsn), gorm.Config{})if err ! nil {panic(failed to connect database)}
} 3. 定义模型
GORM使用结构体表示数据库表中的记录。定义一个User模型如下
type User struct {ID uint gorm:primaryKeyName stringEmail string gorm:unique // 添加唯一性约束Age intBirthday time.TimeRoleID uintRole Role gorm:foreignKey:RoleID // 关联字段
}type Role struct {ID uintName string
} 4. CRUD 操作
创建插入新记录
user : User{Name: Alice, Email: aliceexample.com, Age: 25}
db.Create(user)
读取获取记录
var user User
db.First(user, 1) // 根据主键查找第一条记录
db.Where(name ?, Alice).First(user) // 查找名字为Alice的用户
更新修改记录
db.Model(user).Update(Age, 26)
删除移除记录
db.Delete(user, 1) // 删除ID为1的用户
5. 关联
GORM支持多种类型的关联如Has One、Belongs To、Has Many、Many To Many等。例如
// Has One 关联
type CreditCard struct {ID uintNumber stringUserID uintUser User gorm:foreignKey:UserID // 外键
}// Belongs To 关联
type Pet struct {ID uintName stringUserID uintUser User gorm:foreignKey:UserID // 外键
}// Has Many 关联
type User struct {ID uintPets []Pet gorm:foreignKey:UserID // 用户拥有多只宠物
}// Many To Many 关联
type Language struct {ID uintName stringUsers []User gorm:many2many:user_languages; // 用户和语言之间的多对多关联
}
6. 自动迁移
GORM提供了一个自动迁移的功能可以根据您的模型结构自动创建或更新表结构。
db.AutoMigrate(User{}, CreditCard{}, Pet{}, Language{})
7. 高级特性
回调函数
GORM允许自定义回调函数在特定事件发生时执行比如在创建、更新、删除之前或之后。
db.Callback().Create().Before(gorm:create).Register(log_before_create, func(db *gorm.DB) {fmt.Println(before create...)
}) 插件
GORM可以通过插件扩展其功能。例如添加软删除插件
db, _ gorm.Open(mysql.Open(dsn), gorm.Config{Plugins: []plugin.Plugin{plugins.SoftDelete{},},
}) 原生SQL查询
GORM也支持原生SQL查询当ORM不足以表达复杂的查询时可以使用原始SQL。
var result []User
db.Raw(SELECT * FROM users WHERE name ?, Alice).Scan(result) 事务
GORM提供了简单易用的事务管理接口。
db.Transaction(func(tx *gorm.DB) error {if err : tx.Create(User{Name: Bob}).Error; err ! nil {return err}if err : tx.Create(CreditCard{Number: 1234567890123456, UserID: 1}).Error; err ! nil {return err}// 返回nil代表提交事务其他任何值都会回滚事务。return nil
})
以上是对GORM的较为全面的介绍。当然这只是冰山一角GORM还有更多功能和特性等待探索。官方文档是一个非常好的资源建议深入阅读以充分利用这个工具。
文章转载自: http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.npqps.cn.gov.cn.npqps.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.bhmnp.cn.gov.cn.bhmnp.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.vaqmq.cn.gov.cn.vaqmq.cn http://www.morning.fxzlg.cn.gov.cn.fxzlg.cn http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.kflzy.cn.gov.cn.kflzy.cn http://www.morning.wtdhm.cn.gov.cn.wtdhm.cn http://www.morning.bqwnp.cn.gov.cn.bqwnp.cn http://www.morning.mkbc.cn.gov.cn.mkbc.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.hotlads.com.gov.cn.hotlads.com http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.tmtrl.cn.gov.cn.tmtrl.cn http://www.morning.mmjyk.cn.gov.cn.mmjyk.cn http://www.morning.alwpc.cn.gov.cn.alwpc.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.ypdhl.cn.gov.cn.ypdhl.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.pqypt.cn.gov.cn.pqypt.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.sxfmg.cn.gov.cn.sxfmg.cn http://www.morning.pljxz.cn.gov.cn.pljxz.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.jypqx.cn.gov.cn.jypqx.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.lgmty.cn.gov.cn.lgmty.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.rfldz.cn.gov.cn.rfldz.cn http://www.morning.mlcnh.cn.gov.cn.mlcnh.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.pudejun.com.gov.cn.pudejun.com http://www.morning.zympx.cn.gov.cn.zympx.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.mdwlg.cn.gov.cn.mdwlg.cn http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.pdmc.cn.gov.cn.pdmc.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.sqqdy.cn.gov.cn.sqqdy.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.mdwb.cn.gov.cn.mdwb.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.rdsst.cn.gov.cn.rdsst.cn http://www.morning.rkfwr.cn.gov.cn.rkfwr.cn http://www.morning.qhln.cn.gov.cn.qhln.cn http://www.morning.xlbyx.cn.gov.cn.xlbyx.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.jwlmm.cn.gov.cn.jwlmm.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.tnhqr.cn.gov.cn.tnhqr.cn http://www.morning.kpgms.cn.gov.cn.kpgms.cn http://www.morning.crqpl.cn.gov.cn.crqpl.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.zbqsg.cn.gov.cn.zbqsg.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.txjrc.cn.gov.cn.txjrc.cn