beedb mysql_13.5 实现博客的增删改

本文将引导你通过Beego框架构建一个完整的博客系统,涵盖博客浏览、添加、编辑和删除功能。从路由配置到数据库设计,再到Controller、Model和View层的实现,一步步教你如何实现实时的博客管理平台。
摘要由CSDN通过智能技术生成

前面介绍了beego框架实现的整体构思以及部分实现的伪代码,这小节介绍通过beego建立一个博客系统,包括博客浏览、添加、修改、删除等操作。

## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#博客目录)博客目录

博客目录如下所示:

~~~

.

├── controllers

│   ├── delete.go

│   ├── edit.go

│   ├── index.go

│   ├── new.go

│   └── view.go

├── main.go

├── models

│   └── model.go

└── views

├── edit.tpl

├── index.tpl

├── layout.tpl

├── new.tpl

└── view.tpl

~~~

## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#博客路由)博客路由

博客主要的路由规则如下所示:

~~~

//显示博客首页

beego.Router("/", &controllers.IndexController{})

//查看博客详细信息

beego.Router("/view/:id([0-9]+)", &controllers.ViewController{})

//新建博客博文

beego.Router("/new", &controllers.NewController{})

//删除博文

beego.Router("/delete/:id([0-9]+)", &controllers.DeleteController{})

//编辑博文

beego.Router("/edit/:id([0-9]+)", &controllers.EditController{})

~~~

## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#数据库结构)数据库结构

数据库设计最简单的博客信息

~~~

CREATE TABLE entries (

id INT AUTO_INCREMENT,

title TEXT,

content TEXT,

created DATETIME,

primary key (id)

);

~~~

## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#控制器)控制器

IndexController:

~~~

type IndexController struct {

beego.Controller

}

func (this *IndexController) Get() {

this.Data["blogs"] = models.GetAll()

this.Layout = "layout.tpl"

this.TplNames = "index.tpl"

}

~~~

ViewController:

~~~

type ViewController struct {

beego.Controller

}

func (this *ViewController) Get() {

id, _ := strconv.Atoi(this.Ctx.Input.Params(":id"))

this.Data["Post"] = models.GetBlog(id)

this.Layout = "layout.tpl"

this.TplNames = "view.tpl"

}

~~~

NewController

~~~

type NewController struct {

beego.Controller

}

func (this *NewController) Get() {

this.Layout = "layout.tpl"

this.TplNames = "new.tpl"

}

func (this *NewController) Post() {

inputs := this.Input()

var blog models.Blog

blog.Title = inputs.Get("title")

blog.Content = inputs.Get("content")

blog.Created = time.Now()

models.SaveBlog(blog)

this.Ctx.Redirect(302, "/")

}

~~~

EditController

~~~

type EditController struct {

beego.Controller

}

func (this *EditController) Get() {

id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"])

this.Data["Post"] = models.GetBlog(id)

this.Layout = "layout.tpl"

this.TplNames = "new.tpl"

}

func (this *EditController) Post() {

inputs := this.Input()

var blog models.Blog

blog.Id, _ = strconv.Atoi(inputs.Get("id"))

blog.Title = inputs.Get("title")

blog.Content = inputs.Get("content")

blog.Created = time.Now()

models.SaveBlog(blog)

this.Ctx.Redirect(302, "/")

}

~~~

DeleteController

~~~

type DeleteController struct {

beego.Controller

}

func (this *DeleteController) Get() {

id, _ := strconv.Atoi(this.Ctx.Input.Params(":id"))

blog := GetBlog(id int)

this.Data["Post"] = blog

models.DelBlog(blog)

this.Ctx.Redirect(302, "/")

}

~~~

## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#model层)model层

~~~

package models

import (

"database/sql"

"github.com/astaxie/beedb"

_ "github.com/ziutek/mymysql/godrv"

"time"

)

type Blog struct {

Id int `PK`

Title string

Content string

Created time.Time

}

func GetLink() beedb.Model {

db, err := sql.Open("mymysql", "blog/astaxie/123456")

if err != nil {

panic(err)

}

orm := beedb.New(db)

return orm

}

func GetAll() (blogs []Blog) {

db := GetLink()

db.FindAll(&blogs)

return

}

func GetBlog(id int) (blog Blog) {

db := GetLink()

db.Where("id=?", id).Find(&blog)

return

}

func SaveBlog(blog Blog) (bg Blog) {

db := GetLink()

db.Save(&blog)

return bg

}

func DelBlog(blog Blog) {

db := GetLink()

db.Delete(&blog)

return

}

~~~

## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/13.5.md#view层)view层

layout.tpl

~~~

My Blog

#menu {

width: 200px;

float: right;

}

{{.LayoutContent}}

~~~

index.tpl

~~~

Blog posts

{{range .blogs}}

{{.Title}}

from {{.Created}}

Edit

Delete

{{end}}

~~~

view.tpl

~~~

{{.Post.Title}}

{{.Post.Created}}

{{.Post.Content}}

~~~

new.tpl

~~~

New Blog Post

标题:

内容:

~~~

edit.tpl

~~~

Edit {{.Post.Title}}

New Blog Post

标题:

内容:{{.Post.Content}}

~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值