我试着用beego来进行木板寻呼。
所以我是这样做的,
在控制器中
var articles []*models.Board
board.Data["startCount"] = startCount
board.Data["endCount"] = endCount
/* Template Test Query
num, err := o.QueryTable("board").Filter("Idx", 21).OrderBy("-idx").All(&articles)
*/
num, err := o.Raw("SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).QueryRows(&articles)
if num == 0 || err == orm.ErrNoRows {
board.Data["user_error"] = err
}
if err != orm.ErrNoRows && num > 0 {
board.Data["nums"] = num
board.Data["articles"] = articles
}
在VEIW文件中
<tbody>
{{ if not .articles }}
<tr>
<td colspan="5">No articles.</td>
</tr>
{{ else }}
{{ $uri := .article_uri}}
{{range $articles := .articles}}
<tr>
<td>{{$articles.Idx}}</td>
<td><a href="{{urlfor "BoardController.Content" ":id" $articles.Idx}}">{{$articles.Title}}</a></td>
<td>{{$articles.Nick}}</td>
<td>{{date $articles.Date "y.m.d H:i"}}</td>
<td>0</td>
</tr>
{{ end }}
{{ end }}
</tbody>
当我使用模板测试查询时,我看到了成功打印。
但我只能看到空表,因为它被更改为O.raw(…).queryrows(&articles)
请有人帮我……
模型
type Board struct{
Idx int64 `orm:"pk";form:"-"`
Id string `form:"id"`
Title string `form:"title"`
Body string `form:"content"`
Nick string `form:"nick"`
Date time.Time `orm:"auto_now_add;type(datetime)"`
}
最佳答案
在控制器中
```
o := orm.NewOrm()
var maps []orm.Params
num, err := o.Raw("SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).Values(&maps)
fmt.Println(maps)
if num == 0 || err == orm.ErrNoRows {
board.Data["user_error"] = err
}
if err != orm.ErrNoRows && num > 0 {
board.Data["nums"] = num
board.Data["articles"] = articles
}
```
价值平面图
```
var list orm.ParamsList
num, err = o.Raw(""SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).ValuesFlat(&list)
if err == nil && num > 0 {
fmt.Println(list) // []{"1","2","3",...}
}
```
请参见演示URLhttps://beego.me/docs/mvc/model/rawsql.md#queryrows
但是我喜欢用JSON所以
```
o := orm.NewOrm()
var maps []orm.Params
o.Raw("SELECT board.* FROM(SELECT @ROWNUM := @ROWNUM + 1 AS ROWNUM, board.* FROM board, (SELECT @ROWNUM := 0) R) board WHERE board.ROWNUM > ?;", 0).Values(&maps)
list := make([]map[string]interface{}, len(maps))
for k, v := range maps {
row := make(map[string]interface{})
row["Idx"]=v["workid"]
row["Id"] = v["droneid"]
row["Title"]=v["starttime"]
row["Body"]=v["worktime"]
row["Nick"]=v["modes"]
row["Date"]=v["tele"]
paths := make([]interface{}, 0)
patharray := strings.Split(v["path"].(string),"],[")
for _, vv := range patharray {
path := make([]string, 2)
vv1 := strings.Replace(vv,"[", "", -1)
vv2 := strings.Replace(vv1,"]","",-1)
//fmt.Println(vv2)
vvarr := strings.Split(vv2,",")
// fmt.Println("++++++++++++++")
// fmt.Println(vvarr)
path[0] = vvarr[0]
path[1] = vvarr[1]
paths = append(paths, path)
}
row["path"] = paths
list[k] = row
}
this.Data["json"] = list
this.ServeJSON();
```
关于go - Beego Raw.QueryRows,模板,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32295038/