添加orm查询所有操作
This commit is contained in:
parent
83dfa0900d
commit
eeda01e0ab
@ -5,6 +5,7 @@ import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/prometheus/common/log"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
type OrmController2 struct {
|
||||
@ -13,20 +14,36 @@ type OrmController2 struct {
|
||||
type OrmController2Add struct {
|
||||
beego.Controller
|
||||
}
|
||||
type OrmController2update struct {
|
||||
beego.Controller
|
||||
}
|
||||
type OrmController2Delete struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (t *OrmController2) Get() {
|
||||
o := orm.NewOrm()
|
||||
article := models.Article{Id: 1}
|
||||
err := o.Read(&article)
|
||||
if err != nil {
|
||||
log.Errorf("没有查到")
|
||||
}
|
||||
log.Info("查到数据", article)
|
||||
t.Data["article"] = article
|
||||
article := models.Article{}
|
||||
//err := o.Read(&article)
|
||||
//if err != nil {
|
||||
// log.Errorf("没有查到")
|
||||
//}
|
||||
//log.Info("查到数据", article)
|
||||
//t.Data["article"] = article
|
||||
qs := o.QueryTable(article)
|
||||
var articles []models.Article
|
||||
qs.All(&articles)
|
||||
t.Data["articles"] = articles
|
||||
t.TplName = "test_orm/orm2_list.html"
|
||||
}
|
||||
|
||||
//关闭xsrf
|
||||
//func (x *OrmController2Add) Prepare() {
|
||||
// //controller级别关闭xsrf
|
||||
// x.EnableXSRF = false
|
||||
//}
|
||||
func (t *OrmController2Add) Get() {
|
||||
t.Data["xsrf"] = template.HTML(t.XSRFFormHTML())
|
||||
t.TplName = "test_orm/orm2_add.html"
|
||||
}
|
||||
|
||||
@ -40,5 +57,47 @@ func (t *OrmController2Add) Post() {
|
||||
log.Errorf("插入失败")
|
||||
}
|
||||
log.Info("插入", count, "条")
|
||||
t.TplName = "success.html"
|
||||
t.TplName = "test_orm/success.html"
|
||||
}
|
||||
|
||||
func (t *OrmController2update) Get() {
|
||||
id, _ := t.GetInt64("id")
|
||||
log.Info("id:", id)
|
||||
o := orm.NewOrm()
|
||||
article := models.Article{Id: int(id)}
|
||||
err := o.Read(&article)
|
||||
if err != nil {
|
||||
log.Errorf("没有查到")
|
||||
}
|
||||
log.Info("查到数据", article)
|
||||
t.Data["xsrf"] = template.HTML(t.XSRFFormHTML())
|
||||
t.Data["article"] = article
|
||||
t.TplName = "test_orm/orm2_edit.html"
|
||||
}
|
||||
func (t *OrmController2update) Post() {
|
||||
o := orm.NewOrm()
|
||||
id, _ := t.GetInt64("id")
|
||||
title := t.GetString("title")
|
||||
author := t.GetString("author")
|
||||
article := models.Article{Id: int(id), Title: title, Author: author}
|
||||
count, err := o.Update(&article)
|
||||
if err != nil {
|
||||
log.Errorf("更新失败")
|
||||
} else {
|
||||
log.Info("更新了", count, "条数据")
|
||||
}
|
||||
t.TplName = "test_orm/success.html"
|
||||
}
|
||||
|
||||
func (t *OrmController2Delete) Get() {
|
||||
id, _ := t.GetInt64("id")
|
||||
article := &models.Article{Id: int(id)}
|
||||
o := orm.NewOrm()
|
||||
count, err := o.Delete(article)
|
||||
if err != nil {
|
||||
log.Errorf("删除失败")
|
||||
} else {
|
||||
log.Info("删除了", count, "条数据")
|
||||
}
|
||||
t.TplName = "test_orm/success.html"
|
||||
}
|
||||
|
26
beego_demo1/controllers/test_orm/ormController3.go
Normal file
26
beego_demo1/controllers/test_orm/ormController3.go
Normal file
@ -0,0 +1,26 @@
|
||||
package test_orm
|
||||
|
||||
import (
|
||||
"beego_demo1/models"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/prometheus/common/log"
|
||||
)
|
||||
|
||||
type OrmController3 struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (t *OrmController3) Get() {
|
||||
o := orm.NewOrm()
|
||||
|
||||
//
|
||||
//o.QueryTable("article")
|
||||
article := models.Article{}
|
||||
articles := []models.Article{}
|
||||
qs := o.QueryTable(article)
|
||||
//qs.Filter("title__exact","西游记").One(&article)
|
||||
qs.All(&articles)
|
||||
log.Info(articles)
|
||||
t.TplName = "test_orm/orm3.html"
|
||||
}
|
@ -37,4 +37,9 @@ func init() {
|
||||
beego.Router("/orm1", &test_orm.OrmController1{})
|
||||
beego.Router("/article", &test_orm.OrmController2{})
|
||||
beego.Router("/article/add", &test_orm.OrmController2Add{})
|
||||
beego.Router("/article/edit", &test_orm.OrmController2update{})
|
||||
beego.Router("/article/delete", &test_orm.OrmController2Delete{})
|
||||
|
||||
//exper表达式
|
||||
beego.Router("/orm3", &test_orm.OrmController3{})
|
||||
}
|
||||
|
11
beego_demo1/utils/str_conv_int.go
Normal file
11
beego_demo1/utils/str_conv_int.go
Normal file
@ -0,0 +1,11 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func StrToInt(str string) int {
|
||||
value_64, _ := strconv.ParseInt(str, 10, 64)
|
||||
return *(*int)(unsafe.Pointer(&value_64))
|
||||
}
|
@ -6,10 +6,10 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>文章添加页面</h1>
|
||||
<form action="/author/add" method="post">
|
||||
<form action="/article/add" method="post">
|
||||
{{.xsrf}}
|
||||
<input type="text" name="title"><br>
|
||||
<input type="text" name="author"><br>
|
||||
文章标题:<input type="text" name="title" placeholder="文章标题"><br>
|
||||
文章作者:<input type="text" name="author" placeholder="文章作者"><br>
|
||||
<input type="submit" value="提交">
|
||||
</form>
|
||||
</body>
|
||||
|
17
beego_demo1/views/test_orm/orm2_edit.html
Normal file
17
beego_demo1/views/test_orm/orm2_edit.html
Normal file
@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>文章添加页面</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>文章添加页面</h1>
|
||||
<form action="/article/edit" method="post">
|
||||
{{.xsrf}}
|
||||
<input type="hidden" name="id" value="{{.article.Id}}"><br>
|
||||
文章标题:<input type="text" name="title" value="{{.article.Title}}"><br>
|
||||
文章作者:<input type="text" name="author" value="{{.article.Author}}"><br>
|
||||
<input type="submit" value="提交">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
@ -7,22 +7,22 @@
|
||||
<body>
|
||||
<h1>文章列表</h1>
|
||||
|
||||
<a href="">添加</a> <br>
|
||||
<a href="/article/add">添加</a> <br>
|
||||
<table>
|
||||
<thead>
|
||||
<th>编号</th>
|
||||
<th>标题</th>
|
||||
<th>作者</th>
|
||||
</thead>
|
||||
<tr>
|
||||
{{if .article.Id}}
|
||||
<td>{{.article.Id}}</td>
|
||||
<td>{{.article.Title}}</td>
|
||||
<td>{{.article.Author}}</td>
|
||||
<td><a href="">修改</a></td>
|
||||
<td><a href="">删除</a></td>
|
||||
{{end}}
|
||||
</tr>
|
||||
{{range $i,$v := .articles}}
|
||||
<tr>
|
||||
<td>{{$v.Title}}</td>
|
||||
<td>{{$v.Author}}</td>
|
||||
<td><a href="/article/edit?id={{$v.Id}}">修改</a></td>
|
||||
<td><a href="/article/delete?id={{$v.Id}}">删除</a></td>
|
||||
</tr>
|
||||
<br>
|
||||
{{end}}
|
||||
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
10
beego_demo1/views/test_orm/orm3.html
Normal file
10
beego_demo1/views/test_orm/orm3.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>exper表达式</h1>
|
||||
</body>
|
||||
</html>
|
10
beego_demo1/views/test_orm/success.html
Normal file
10
beego_demo1/views/test_orm/success.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
恭喜,操作成功 <a href="/article">返回</a>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user