对象的 CRUD(增删改查) 操作 ~~~ var DB orm.Ormer DB = orm.NewOrm() 创建一个数据库链接 Ormer DB.Insert() 插入一条数据,返回自增 id DB.InsertMulti() 同时插入多条数据,返回插入的条数 DB.Update() 修改操作,返回值为受影响的行数 DB.Read() 从数据库读取数据 DB.ReadOrCreate() 尝试从数据库读取,不存在的话就创建一个 DB.Delete() 删除操作,返回值为受影响的行数 ~~~ 代码实例: ~~~ CREATE TABLE `person` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(260) DEFAULT NULL, `sex` varchar(260) DEFAULT NULL, `email` varchar(260) DEFAULT NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 ~~~ ~~~ package main import ( "fmt" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" ) /* +----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+ | user_id | int(11) | NO | PRI | NULL | auto_increment | | username | varchar(260) | YES | | NULL | | | sex | varchar(260) | YES | | NULL | | | email | varchar(260) | YES | | NULL | | +----------+--------------+------+-----+---------+----------------+ */ // struct 字段,首字母必须大写 type Person struct { User_id int `orm:"pk"` Username string Sex string Email string } func init() { maxIdle := 30 maxConn := 30 // 创建连接池 err := orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/test?charset=utf8", maxIdle, maxConn) if err != nil { fmt.Println("connect mysql err : ", err) } // 需要在init中注册定义的model orm.RegisterModel(new(Person)) } func main() { var DB orm.Ormer // 创建一个数据库链接 Ormer DB = orm.NewOrm() p0 := Person{ Username: "person0", Sex: "man", Email: "person0@golang.com", } // 插入一条数据,返回自增 id id, err := DB.Insert(&p0) if err != nil { fmt.Println("insert p0 err : ", err) } fmt.Println("id :", id) p1 := []Person{ {Username: "person1", Sex: "man", Email: "person1@golang.com"}, {Username: "person2", Sex: "man", Email: "person2@golang.com"}, {Username: "person3", Sex: "man", Email: "person3@golang.com"}, {Username: "person4", Sex: "man", Email: "person4@golang.com"}, {Username: "person5", Sex: "man", Email: "person5@golang.com"}, {Username: "person6", Sex: "man", Email: "person6@golang.com"}, } // 同时插入多条数据,返回插入的条数 ids, err := DB.InsertMulti(len(p1), p1) if err != nil { fmt.Println("insert p1 err : ", err) } fmt.Println("ids : ", ids) p2 := Person{User_id: 1} // 查询 err = DB.Read(&p2) if err == orm.ErrNoRows { fmt.Println("查询不到") } else if err == orm.ErrMissPK { fmt.Println("找不到主键") } else { fmt.Println(p2) } p3 := Person{Username: "person7"} // 尝试从数据库读取,不存在的话就创建一个 // 三个返回参数依次为:是否新创建的,对象 Id 值,错误 if created, id, err := DB.ReadOrCreate(&p3, "Username"); err == nil { if created { fmt.Println("New Insert an object. Id:", id) } else { fmt.Println("Get an object. Id:", id) } } p4 := Person{User_id: 4} if DB.Read(&p4) == nil { p4.Username = "Update" p4.Sex = "woman" p4.Email = "update@golang.com" // 修改操作,返回值为受影响的行数 if num, err := DB.Update(&p4); err == nil { fmt.Println("update return num : ", num) } } p5 := Person{User_id: 5} // 删除操作,返回值为受影响的行数 if num, err := DB.Delete(&p5); err == nil { fmt.Println("delete return num : ", num) } } ~~~