> 作为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://box.kancloud.cn/fc7aa05acc4a11f6a84de6060f7c5711_753x347.jpg)
参考链接:
https://www.cnblogs.com/tudaogaoyang/p/7940697.html