一、直接输出字符串
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"mypro/models"
"strings"
)
type GetMovieInfoController struct {
beego.Controller
}
func (c *GetMovieInfoController) Get() {
ids := []string{"8", "9", "10"}
db := models.ConnectDb();
defer db.Close()
idStr := strings.Join(ids, "','")
sqlText := "select id,movie_name,movie_director from movie_info where id in ('%s')"
sqlText = fmt.Sprintf(sqlText, idStr)
c.Ctx.WriteString(sqlText) //输出sql
c.TplName = "get.tpl"
}
二、模板数据输出
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"mypro/models"
"strings"
)
type GetMovieInfoController struct {
beego.Controller
}
type MovieInfo struct {
Id int64
Movie_name string
Movie_director string
}
func (c *GetMovieInfoController) Get() {
ids := []string{"11", "9", "10"}
//c.Ctx.WriteString(idsstr)
db := models.ConnectDb();
defer db.Close()
idStr := strings.Join(ids, "','")
sqlText := "select id,movie_name,movie_director from movie_info where id in ('%s')"
sqlText = fmt.Sprintf(sqlText, idStr)
//c.Ctx.WriteString(sqlText ) //直接输出sql
rows,err := db.Query(sqlText)
item := MovieInfo{}
list := []MovieInfo{}
for rows.Next(){
var mid int64
var movie_name, movie_director string
err = rows.Scan(&mid,&movie_name,&movie_director)
if err != nil {
panic(err.Error())
}
item.Id = mid
item.Movie_name = movie_name
item.Movie_director = movie_director
list = append(list,item)
}
fmt.Println(list)
c.Data["List"] = list //把数据列表赋值
c.Data["Title"] = "正在进行Beego框架基础学习" //动态赋值
c.TplName = "get.tpl"
}
<body>
<header>
</header>
<h1 style="text-align:center;margin:auto">{{.Title}}</h1>
<footer>
<ul>
//遍历输出
{{ range $MovieInfo := .List }}
<li style="list-style:none; text-align:left">
ID:{{ $MovieInfo.Id }}---电影名称:{{ $MovieInfo.Movie_name }}----电影演员:{{ $MovieInfo.Movie_director }}
</li>
{{end}}
</ul>
</footer>
<div class="backdrop"></div>
<script src="/static/js/reload.min.js"></script>
</body>
三、Json格式输出
通过把要输出的数据放到Data["json"]中,然后调用ServeJSON()进行渲染,就可以把数据进行JSON序列化输出。
beego中ServeJSON()函数定义如下:
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"mypro/models"
"reflect"
"strings"
)
type GetMovieInfoController struct {
beego.Controller
}
type MovieInfo struct {
Id int64
Movie_name string
Movie_director string
}
type JsonData struct {
Code int
Msg string
Data []MovieInfo //data结构体类型
}
func (c *GetMovieInfoController) Get() {
ids := []string{"11", "9", "10"}
db := models.ConnectDb();
defer db.Close()
idStr := strings.Join(ids, "','")
sqlText := "select id,movie_name,movie_director from movie_info where id in ('%s')"
sqlText = fmt.Sprintf(sqlText, idStr)
rows,err := db.Query(sqlText)
item := MovieInfo{}
list := []MovieInfo{} //data结构体类型
for rows.Next(){
var mid int64
var movie_name, movie_director string
err = rows.Scan(&mid,&movie_name,&movie_director)
if err != nil {
panic(err.Error())
}
item.Id = mid
item.Movie_name = movie_name
item.Movie_director = movie_director
list = append(list,item)
}
fmt.Println(reflect.TypeOf(list)) // 类型是[]controllers.MovieInfo
c.Data["json"] = JsonData{200,"获取成功",list}
c.ServeJSON()
c.TplName = "get.tpl"
}
{
"Code": 200,
"Msg": "获取成功",
"Data": [
{
"Id": 9,
"Movie_name": "小丑 Joker",
"Movie_director": "托德·菲利普斯"
},
{
"Id": 10,
"Movie_name": "婚姻故事 Marriage Story",
"Movie_director": "诺亚·鲍姆巴赫"
},
{
"Id": 11,
"Movie_name": "爱尔兰人 The Irishman",
"Movie_director": "马丁·斯科塞斯"
}
]
}
四、xml格式输出
通过把要输出的数据放到Data["xml"]中,然后调用ServeXML()进行渲染,就可以把数据进行XML序列化输出。
beego中ServeXML()函数定义如下:
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"mypro/models"
"reflect"
"strings"
)
type GetMovieInfoController struct {
beego.Controller
}
type MovieInfo struct {
Id int64
Movie_name string
Movie_director string
}
type XmlData struct {
Code int
Msg string
Data []MovieInfo //结构体
}
func (c *GetMovieInfoController) Get() {
ids := []string{"11", "9", "10"}
db := models.ConnectDb();
defer db.Close()
idStr := strings.Join(ids, "','")
sqlText := "select id,movie_name,movie_director from movie_info where id in ('%s')"
sqlText = fmt.Sprintf(sqlText, idStr)
rows,err := db.Query(sqlText)
item := MovieInfo{}
list := []MovieInfo{} //结构体
for rows.Next(){
var mid int64
var movie_name, movie_director string
err = rows.Scan(&mid,&movie_name,&movie_director)
if err != nil {
panic(err.Error())
}
item.Id = mid
item.Movie_name = movie_name
item.Movie_director = movie_director
list = append(list,item)
}
fmt.Println(reflect.TypeOf(list))
c.Data["xml"] = XmlData{200,"获取成功",list}
c.ServeXML()
c.TplName = "get.tpl"
}
<XmlData>
<Code>200</Code>
<Msg>获取成功</Msg>
<Data>
<Id>9</Id>
<Movie_name>小丑 Joker</Movie_name>
<Movie_director>托德·菲利普斯</Movie_director>
</Data>
<Data>
<Id>10</Id>
<Movie_name>婚姻故事 Marriage Story</Movie_name>
<Movie_director>诺亚·鲍姆巴赫</Movie_director>
</Data>
<Data>
<Id>11</Id>
<Movie_name>爱尔兰人 The Irishman</Movie_name>
<Movie_director>马丁·斯科塞斯</Movie_director>
</Data>
</XmlData>