golang学习之beego增删改查代码实现
博客专区 > caiyezi 的博客 > 博客详情
golang学习之beego增删改查代码实现
caiyezi 发表于1年前
golang学习之beego增删改查代码实现
  • 发表于 1年前
  • 阅读 276
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

记录下使用beego的增删改查实现,数据库使用mysql,完整代码如下:

package main import ( _ "crud_beego/routers" //自动注册路由
    "fmt"
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" ) const ( DRIVER_NAME = "mysql" DATA_SOURCE = "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8&loc=Asia%2FShanghai" MAX_IDLE_CONN = 5 MAX_OPEN_CONN = 30 ) type User struct { Id int Name string Email string Age int isActive bool } func (u *User) TableName() string { return "t_user" } func registerDB() { orm.Debug = true orm.RegisterDataBase("default", DRIVER_NAME, DATA_SOURCE, MAX_IDLE_CONN, MAX_OPEN_CONN) orm.RegisterModel(new(User)) } func (this User) ToString() string { return fmt.Sprintf("Id:%d\tName:%s\tEmail:%s\tAge:%d\tisActive:%v", this.Id, this.Name, this.Email, this.Age, this.isActive) } func createUsers() { users := []User{ User{Name: "adadmin1", Email: "14199@111.com", Age: 12}, User{Name: "adadmin1", Email: "14199@111.com", Age: 12}, User{Name: "adadmin1", Email: "14199@111.com", Age: 12}, User{Name: "adadmin1", Email: "14199@111.com", Age: 12}, User{Name: "adadmin1", Email: "14199@111.com", Age: 12}, User{Name: "adadmin1", Email: "14199@111.com", Age: 12}, User{Name: "adadmin1", Email: "14199@111.com", Age: 12}, } o := orm.NewOrm() if successNums, err := o.InsertMulti(len(users), users); err != nil { fmt.Println("insert fail.....") } else { fmt.Println("success inserted %d datas", successNums) } } //find all users
func listUsers() { var users []User orm.NewOrm().QueryTable("t_user").All(&users) for _, user := range users { fmt.Println(user.ToString()) } } //get user count
func countUser() { cnt, _ := orm.NewOrm().QueryTable("t_user").Count() fmt.Println("All user count:", cnt) } //get the only one user
func getUser() { var user User err := orm.NewOrm().QueryTable("t_user").Filter("Id", 5).One(&user) if err == nil { fmt.Println(user.ToString()) } } //get the only one user
func getUsers() { var users []User _, err := orm.NewOrm().QueryTable("t_user").Filter("name__contains", "awd").All(&users) if err == nil { for _, user := range users { fmt.Println(user.ToString()) } } } //get limit user
func limitUser() { var users []User _, err := orm.NewOrm().QueryTable("t_user").Limit(6).OrderBy("-Name").All(&users) if err == nil { for _, user := range users { fmt.Println(user.ToString()) } } } //get user limit,offset
func limitoffsetUser() { var users []User _, err := orm.NewOrm().QueryTable("t_user").Limit(1, 4).OrderBy("Id").All(&users) if err == nil { for _, user := range users { fmt.Println(user.ToString()) } } } //del user
func delUser() { num, err := orm.NewOrm().QueryTable("t_user").Filter("Id", "5").Delete() fmt.Printf("Affected Num: %s, %s", num, err) } //update user
func updateUser() { num, err := orm.NewOrm().QueryTable("t_user").Filter("name__contains", "awd").Update(orm.Params{ "name": "#########", }) fmt.Printf("Affected Num: %s, %s", num, err) } func main() { registerDB() createUsers() listUsers() countUser() getUser() getUsers() limitoffsetUser() delUser() updateUser() beego.Run() }

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 1
博文 108
码字总数 0
×
caiyezi
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: