2019-go-02

新增

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

func (this *InsertController) Post() {
/*
Name :="admin"
Pwd :="12346"
Sex :="男"
Age :=20
*/
Name :=this.GetString("Name")
Pwd :=this.GetString("Pwd")
Sex :=this.GetString("Sex")
Age,err:=this.GetInt("Age")
if err!=nil {
this.Ctx.WriteString("非法年龄字段")
return
}
user:=models.Users{Name:Name,Pwd:Pwd,Sex:Sex,Age:Age}
insOrm:=orm.NewOrm()
n,err:=insOrm.Insert(&user)
if err==nil&&n>0 {
this.Ctx.WriteString("数据插入成功")
}else{
this.Ctx.WriteString("数据插入失败")
}
}

读取read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func (this *QueryController)Get()  {

id,err:=this.GetInt("id")
if err!=nil {
this.Ctx.WriteString("id异常")
return
}
user:= models.Users{Id:id} //获取指定id的数据
orm:=orm.NewOrm()
err=orm.Read(&user) //读取数据
if err==nil {
this.Ctx.WriteString("id="+strconv.Itoa(user.Id)+"\nname="+user.Name+"\nsex="+user.Sex)
}else{
this.Ctx.WriteString("查询失败")
}

}

更新 update

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func (this *UpdataController)Get()  {
Id,err:=this.GetInt("Id")
if err!=nil {
this.Ctx.WriteString("id异常")
return
}
Name :=this.GetString("Name")
Pwd :=this.GetString("Pwd")
Sex :=this.GetString("Sex")
Age,err:=this.GetInt("Age")
if err!=nil {
this.Ctx.WriteString("非法年龄字段")
return
}
user:= models.Users{Id:Id,Name:Name,Pwd:Pwd,Age:Age,Sex:Sex}
orm:=orm.NewOrm()
n,err:=orm.Update(&user)
if n>0&&err==nil{
this.Ctx.WriteString("更新成功")
}else{
this.Ctx.WriteString("更新失败")
}
}

删除 delete

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func (this *DeleteController) Get()  {
id,err:=this.GetInt("id")
if err!=nil {
this.Ctx.WriteString("删除失败")
return
}
user:= models.Users{Id:id}
orm:=orm.NewOrm()
n,err:=orm.Delete(&user)
if n>0&&err==nil {
this.Ctx.WriteString("删除成功")
}else{
this.Ctx.WriteString("删除失败")
}

}