关于beego:Go-笔记-Beego-之-orm-增删改查
增package mainimport ( "fmt" "github.com/beego/beego/v2/client/orm" _ "github.com/go-sql-driver/mysql" "log" "time")type Account struct { ID int64 Name string Password string Birthday *time.Time Telephone string Email string Addr string Status int8 RoleId int64 DepartmentId int64 CreatedAt *time.Time `orm:"auto_now_add"` UpdatedAt *time.Time `orm:"auto_now"` //DeletedAt *time.Time `orm:"null"` DeletedAt *time.Time Description string Sex bool}func main() { orm.Debug = true // 打印 sql 。可能影响性能。生产不倡议应用 driverName := "mysql" // 驱动名 databaseName := "default" dsn := "golang:123456@tcp(192.168.100.20:33060)/cmdb?charset=utf8mb4&parseTime=true" // 注册数据库驱动到 orm // 参数:自定义的数据库类型名,驱动类型(orm 中提供的) orm.RegisterDriver(driverName, orm.DRMySQL) // 参数:beego 必须指定默认的数据库名称,应用的驱动名称(orm 驱动类型名),数据库的配置信息,数据库(连接池),连贯(池)名称 err := orm.RegisterDataBase(databaseName, driverName, dsn) if err != nil { log.Fatal(err) } // 定义构造 // 注册模型 orm.RegisterModel(new(Account)) now := time.Now() // 增 // 先创立一个 orm 对象 ormer := orm.NewOrm() account := Account{ Name: "dangdang", Password: "123456", Birthday: &now, // 须要应用指针,所以后面须要先定义个 now 变量 Telephone: "88888888", Email: "aaa@126.com", } id, err := ormer.Insert(&account) fmt.Println(id, err, account)}执行过程如下: ...