> 作為MVC中的M,這里我們應該把代碼更集中,M部門把相關功能都實現掉,然后在C中直接調用就行了。
1、相關配置添加 conf/aap.conf
~~~
appname = beego_blog
httpaddr = 127.0.0.1
httpport = 3000
runmode = dev
# 數據庫配置
dbhost = 127.0.0.1
dbport = 3306
dbuser = root
dbpassword = root
dbname = test
dbcharset = utf8
dbmaxidel = 30
dbmaxcon = 30
~~~
2、處理模型 models/models.go
> 查詢所有
> 查詢(id)
> 創建
> 更新
> 刪除(id)
~~~
package models
import (
"fmt"
"github.com/astaxie/beego/orm"
"time"
)
type Article struct {
Id int `orm="auto;pk"`
Author string `orm="size(16)"`
Title string `orm="size(64)"`
Content string
Create_time int64
}
func Init() {
orm.RegisterModel(new(Article))
}
// 查詢所有
func QueryArticleAll() []Article {
o := orm.NewOrm()
qs := o.QueryTable("article")
var articles []Article
count, err := qs.Filter("id__gt", 0).OrderBy("-id").All(&articles)
if err == nil {
fmt.Printf("count", count)
}
return articles
}
// 查詢(id)
func QueryArticleById(id int) (Article, bool) {
o := orm.NewOrm()
article := Article{Id: id}
err := o.Read(&article)
if err == orm.ErrNoRows {
fmt.Println("查詢不到~")
return article, false
} else if err == orm.ErrMissPK {
fmt.Println("找不到主鍵~")
return article, false
} else {
return article, true
}
}
// 創建
func CreateArticle(author string, title string, content string) (int64, bool) {
o := orm.NewOrm()
article := new(Article)
article.Author = author
article.Title = title
article.Content = content
article.Create_time = time.Now().Unix()
id, err := o.Insert(article)
if err == nil {
fmt.Println("創建成功~")
return id, true
} else {
fmt.Println("創建失敗~")
return id, false
}
}
// 更新
func UpdateArticleById(id int, table string, fields map[string]interface{}) bool {
o := orm.NewOrm()
_, err := o.QueryTable(
table).Filter(
"Id", id).Update(
fields)
if err == nil {
fmt.Println("更新成功~")
return true
}
fmt.Println("更新失敗~")
return false
}
// 刪除(id)
func DeleteArticle(id int) bool {
o := orm.NewOrm()
article := Article{Id: id}
num, err := o.Delete(&article)
if err == nil {
fmt.Println("刪除影響的行:", num)
return true
} else {
return false
}
}
~~~
3、在main.go中引入使用
~~~
package main
import (
"beego_blog/models"
_ "beego_blog/routers"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
func init() {
dbuser := beego.AppConfig.String("dbuser")
dbpassword := beego.AppConfig.String("dbpassword")
dbname := beego.AppConfig.String("dbname")
dbcharset := beego.AppConfig.String("dbcharset")
dbmaxidel, _ := beego.AppConfig.Int("dbmaxidel") // Int會返回兩個值int,err,不使用err就用_代替
dbmaxcon, _ := beego.AppConfig.Int("dbmaxcon")
orm.Debug = true
models.Init()
orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", dbuser+":"+dbpassword+"@/"+dbname+"?charset="+dbcharset, dbmaxidel, dbmaxcon)
}
func main() {
o := orm.NewOrm()
o.Using("default")
// 查詢所有
articles := models.QueryArticleAll()
fmt.Print(articles)
// 查詢(id)
/*article, _ := models.QueryArticleById(8)
fmt.Println(article.Id, article.Author)*/
// 創建
/*id, status := models.CreateArticle("wanger", "sfdasdf", "ccc")
fmt.Println(id, status)*/
// 更新
/*var articleFields map[string]interface{}
articleFields = make(map[string]interface{})
articleFields["author"] = "tom"
articleFields["title"] = "beego"
articleFields["content"] = "hello beego"
status := models.UpdateArticleById(11, "article", articleFields)
fmt.Println(status)*/
// 刪除(id)
/*status := models.DeleteArticle(10)
fmt.Println(status)*/
beego.Run()
}
~~~

參考鏈接:
https://www.cnblogs.com/tudaogaoyang/p/7940697.html