https://gorm.io/zh_CN/docs/index.html
原生sql语句用法

  1. package main
  2. import (
  3. "fmt"
  4. "gorm.io/driver/mysql"
  5. "gorm.io/gorm"
  6. "log"
  7. )
  8. type User struct {
  9. Id int
  10. Name string
  11. Pwd string
  12. }
  13. func main() {
  14. dsn := "root:123456@tcp(127.0.0.1:3306)/d?charset=utf8mb4&parseTime=True&loc=Local"
  15. db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
  16. if err != nil {
  17. log.Fatalln("数据库连接错误", err)
  18. }
  19. var users []User
  20. db.Raw("select * from user").Scan(&users)
  21. fmt.Println(users)
  22. var sum int
  23. db.Raw("select sum(1) from user").Scan(&sum)
  24. fmt.Println(sum)
  25. db.Exec("update user set name = ? where id = ?", "zs", 5)
  26. }

标签

标签名 说明
column 指定 db 列名
type 列数据类型,推荐使用兼容性好的通用类型,例如:所有数据库都支持 bool、int、uint、float、string、time、bytes 并且可以和其他标签一起使用,例如:not null、size, autoIncrement… 像 varbinary(8) 这样指定数据库数据类型也是支持的。在使用指定数据库数据类型时,它需要是完整的数据库数据类型,如:MEDIUMINT UNSIGNED not NULL AUTO_INCREMENT
size 指定列大小,例如:size:256
primaryKey 指定列为主键
unique 指定列为唯一
default 指定列的默认值
precision 指定列的精度
scale 指定列大小
not null 指定列为 NOT NULL
autoIncrement 指定列为自动增长
autoIncrementIncrement 自动步长,控制连续记录之间的间隔
embedded 嵌套字段
embeddedPrefix 嵌入字段的列名前缀
autoCreateTime 创建时追踪当前时间,对于 int 字段,它会追踪秒级时间戳,您可以使用 nano/milli 来追踪纳秒、毫秒时间戳,例如:autoCreateTime:nano
autoUpdateTime 创建/更新时追踪当前时间,对于 int 字段,它会追踪秒级时间戳,您可以使用 nano/milli 来追踪纳秒、毫秒时间戳,例如:autoUpdateTime:milli
index 根据参数创建索引,多个字段使用相同的名称则创建复合索引,查看 索引
获取详情
uniqueIndex 与 index 相同,但创建的是唯一索引
check 创建检查约束,例如 check:age > 13,查看 约束
获取详情
<- 设置字段写入的权限, <-:create 只创建、<-:update 只更新、<-:false 无写入权限、<- 创建和更新权限
-> 设置字段读的权限,->:false 无读权限
- ignore this field, - no read/write permission, -:migration no migrate permission, -:all no read/write/migrate permission
comment 迁移时为字段添加注释
  1. db.First()
  2. db.Take()
  3. db.Last()
  4. db.Find()
  5. //条件
  6. //db.where