定義要連線的資料庫 main.go
package main
import (
_ "newgo/routers"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
func init() {
orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/go?charset=utf8")
}
func main() {
beego.Run()
}
定義路由 router.go
beego.Router("/type", &controllers.ClassifyController{}, "GET:Type")
beego.Router("/classifyinsert", &controllers.ClassifyController{}, "POST:ClassifyInsert")
beego.Router("/classifyupdate", &controllers.ClassifyController{}, "GET:ClassifyUpdate")
beego.Router("/classifysave", &controllers.ClassifyController{}, "POST:ClassifySave")
beego.Router("/classifydel", &controllers.ClassifyController{}, "GET:ClassifyDel")
model書寫
package models
import (
"github.com/astaxie/beego/orm"
)
type Classify struct {
Id int
Name string
Content string
}
func init() {
orm.RegisterModel(new(Classify))
}
//增加操作
func InsertClassify(name string, content string) (int64, error) {
o := orm.NewOrm()
var clasinfo Classify
clasinfo.Name = name
clasinfo.Content = content
id, err := o.Insert(&clasinfo)
return id, err
}
//刪除操作
func ClassifyDel(id int) ([]Classify, int64, error) {
o := orm.NewOrm()
var lists []Classify
num, err := o.QueryTable("classify").Filter("Id", id).Delete()
o.QueryTable("inventory").Filter("Cid", id).Delete()
return lists, num, err
}
//展示
func ClassifyList() ([]Classify, int64, error) {
o := orm.NewOrm()
var lists []Classify
num, err := o.QueryTable("classify").All(&lists)
return lists, num, err
}
//修改
func ClassifyUpdate(id int, name string, content string) ([]Classify, int64, error) {
o := orm.NewOrm()
var lists []Classify
num, err := o.QueryTable("classify").Filter("Id", id).Update(orm.Params{
"name": name,
"content": content,
})
return lists, num, err
}
控制器
package controllers
import (
"fmt"
"newgo/models"
"strings"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego"
)
type ClassifyController struct {
beego.Controller
}
func (c *ClassifyController) Get() {
c.TplName = "index.html"
}
func (c *ClassifyController) Type() {
list, num, err := models.ClassifyList()
if err == nil {
fmt.Println(list)
fmt.Println(num)
}
c.Data["comment"] = list
fmt.Println(list)
c.TplName = "type.html"
}
//新增post
func (c *ClassifyController) ClassifyInsert() {
content := strings.TrimSpace(c.GetString("content"))
name := strings.TrimSpace(c.GetString("name"))
models.InsertClassify(name, content)
c.Ctx.Redirect(302, "/type")
}
//修改get展示
func (c *ClassifyController) ClassifySave() {
id, _ := c.GetInt("id")
fmt.Println(id)
content := strings.TrimSpace(c.GetString("content"))
name := strings.TrimSpace(c.GetString("name"))
models.ClassifyUpdate(id, name, content)
c.Ctx.Redirect(302, "/type")
}
//刪除
func (c *ClassifyController) ClassifyDel() {
id, _ := c.GetInt("id")
models.ClassifyDel(id)
c.Ctx.WriteString("200")
}
//修改post方法
func (c *ClassifyController) ClassifyUpdate() {
id, _ := c.GetInt("id")
o := orm.NewOrm()
type Comment struct {
Id int
Name string
Content string
}
var comments []Comment
var com []Comment
num, err := o.Raw("select * from classify where id=?", id).QueryRows(&comments)
o.Raw("select * from classify").QueryRows(&com)
if err == nil {
fmt.Println(num)
fmt.Println(comments)
fmt.Println(com)
c.Data["updates"] = comments
c.Data["comment"] = com
} else {
fmt.Println("查詢報錯")
}
c.TplName = "type.html"
}