golang beego crud
本例子介绍下面这些函数的使用方法。
type QuerySeter interface { Filter(string, ...interface{}) QuerySeter Exclude(string, ...interface{}) QuerySeter Limit(interface{}, ...interface{}) QuerySeter OrderBy(...string) QuerySeter Count() (int64, error) Exist() bool All(interface{}, ...string) (int64, error) One(interface{}, ...string) error ... }
package main import ( "fmt" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" ) const ( DRIVER_NAME = "mysql" DATA_SOURCE = "root:root@tcp(localhost:3306)/test?charset=utf8&loc=Asia%2FShanghai" MAX_IDLE_CONN = 5 MAX_OPEN_CONN = 30 ) type User struct { Id int Name string `orm:"size(20)"` Email string `orm:"size(50)"` Age int IsActive bool } func (this User) ToString() string { return fmt.Sprintf("Name:%s\tEmail:%s\tAge:%d\tIsActive:%v", this.Name, this.Email, this.Age, this.IsActive) } func createUsers() { users := []User{ User{Name: "jemy", Email: "jemy@golanghome.com", Age: 25}, User{Name: "john", Email: "john@golanghome.com", Age: 24}, User{Name: "amy", Email: "amy@golanghome.com", Age: 22}, User{Name: "steven", Email: "steven@golanghome.com", Age: 26}, User{Name: "dolphin", Email: "dolphin@golanghome.com", Age: 21}, User{Name: "alex", Email: "alex@golanghome.com", Age: 32}, User{Name: "bob", Email: "bob@golanghome.com", Age: 25}, User{Name: "thomas", Email: "thomas@golanghome.com", Age: 25}, User{Name: "chris", Email: "chris@golanghome.com", Age: 38}, User{Name: "peter", Email: "peter@golanghome.com", Age: 40}, } if num, err := orm.NewOrm().InsertMulti(len(users), users); err != nil { fmt.Println(err) } else { fmt.Printf("Insert %d users' data!\r\n", num) } } func queryUsers() { //find all users var users []User cnt, _ := orm.NewOrm().QueryTable("user").All(&users) for _, user := range users { fmt.Println(user.ToString()) } fmt.Println() //only get the user count cnt, _ = orm.NewOrm().QueryTable("user").Count() fmt.Println("All user count:", cnt) fmt.Println() //get jemy var user User err := orm.NewOrm().QueryTable("user").Filter("Name", "jemy").One(&user) if err == nil { fmt.Println(user.ToString()) } fmt.Println() //multiple condition err = orm.NewOrm().QueryTable("user").Filter("Name", "jemy").Filter("Age", 25).One(&user) if err == nil { fmt.Println(user.ToString()) } fmt.Println() //get age larger than 25 cnt, err = orm.NewOrm().QueryTable("user").Filter("Age__gt", 25).All(&users) if err == nil { fmt.Printf("There are %d person whose age are larger than %d\r\n", cnt, 25) for _, user := range users { fmt.Println(user.ToString()) } } fmt.Println() //get age not equal to 25 cnt, err = orm.NewOrm().QueryTable("user").Exclude("Age", 25).All(&users) if err == nil { fmt.Printf("There are %d person whose age is not %d\r\n", cnt, 25) for _, user := range users { fmt.Println(user.ToString()) } } fmt.Println() //get age not equal to 25 and not john cnt, err = orm.NewOrm().QueryTable("user").Exclude("Age", 25).Exclude("Name", "john").All(&users) if err == nil { fmt.Printf("There are %d person exclude %s whose age is not %d\r\n", cnt, "john", 25) for _, user := range users { fmt.Println(user.ToString()) } } fmt.Println() //check user exists exists := orm.NewOrm().QueryTable("user").Filter("Name", "alex").Exist() fmt.Println("Alex Exists?", exists) fmt.Println() //get all user order by age cnt, err = orm.NewOrm().QueryTable("user").OrderBy("-Age").All(&users) if err == nil { for _, user := range users { fmt.Println(user.ToString()) } } fmt.Println() //get user limit cnt, err = orm.NewOrm().QueryTable("user").Limit(6).OrderBy("-Name").All(&users) if err == nil { for _, user := range users { fmt.Println(user.ToString()) } } fmt.Println() //get user limit,offset cnt, err = orm.NewOrm().QueryTable("user").Limit(6, 4).OrderBy("-Name").All(&users) if err == nil { for _, user := range users { fmt.Println(user.ToString()) } } fmt.Println() } func registerDB() { orm.Debug = true orm.RegisterDataBase("default", DRIVER_NAME, DATA_SOURCE, MAX_IDLE_CONN, MAX_OPEN_CONN) orm.RegisterModel(new(User)) orm.RunCommand() } func main() { registerDB() //createUsers() queryUsers() }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。