1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package models
import (
"fmt"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
_ "github.com/go-sql-driver/mysql"
)
func init() {
orm.Debug = true
orm.RegisterDriver("mysql", orm.DRMySQL)
db,_ := beego.AppConfig.String("sqlconn")
orm.RegisterDataBase("default", "mysql", db)
orm.RegisterModel(new(User))
orm.RunSyncdb("default", false, true)
}
type User struct {
Id int `orm:"column(id);pk"`
Name string `orm:"column(name)"`
UserId string `orm:"column(User_id)"`
}
func AddUser(user *User) error{
o:= orm.NewOrm()
_, err := o.Insert(user)
return err
}
func GetUser(user_id string) (*User, bool){
o := orm.NewOrm()
User := User{UserId: user_id}
err := o.Read(&User,"User_id")
if err == orm.ErrNoRows {
fmt.Println("User not find")
return &User, false
} else if err == orm.ErrMissPK {
fmt.Println("cant not find prikey")
return &User, false
} else {
return &User, true
}
}
func DeleteUser(user_id string) bool {
o := orm.NewOrm()
User := User{UserId: user_id}
num, err := o.Delete(&User,"user_id")
if err == nil {
fmt.Println("delete num:", num)
return true
} else {
return false
}
}
func GetUserAll() []User{
o := orm.NewOrm()
qs:= o.QueryTable("user")
var users []User
count, err := qs.All(&users)
if err != nil{
fmt.Println("get all User failed")
}
fmt.Println("the count User is ",count)
return users
}
|