step1:在数据库中建立一个test数据库
里面建立一张tes表
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` varchar(255) DEFAULT NULL,
`create_time` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
下载数据库驱动包
go get github.com/astaxie/beego/orm
go get github.com/go-sql-driver/mysql
路由文件:
beego.Router("/write", &controllers.ApiController{},"*:Write")
beego.Router("/read", &controllers.ApiController{},"*:Read")
beego.Router("/update", &controllers.ApiController{},"*:Update")
beego.Router("/del", &controllers.ApiController{},"*:Del")
在modeles中建立模型
package models
import (
"fmt"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql" // import your used driver
)
type Test struct {
Id int64
Content string
Create_time int64
}
func init() {
orm.RegisterDriver("mysql", orm.DRMySQL)
//连接数据库
orm.RegisterDataBase("default", "mysql", "root:root@tcp(39.139.91.456:3306)/test?charset=utf8")
orm.RegisterModel(new(Test))
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}
控制器
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"math/rand"
"strconv"
"time"
"zigbig/models"
)
type ApiController struct {
beego.Controller
}
//写入数据
func (c *ApiController) Write() {
param:=c.Input().Get("name")
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
if param=="null"||param==""{
c.Data["json"] = map[string]interface{}{"code": 1, "message": "传入的参数为空"}
c.ServeJSON()
return
}
orm:=orm.NewOrm()
test:=models.Test{
Content:param,
Create_time:time.Now().Unix(),
}
id,err:= orm.Insert(&test)
if err!=nil{
c.Data["json"] = map[string]interface{}{"code": 1, "message": "保存数据失败"}
c.ServeJSON()
return
}
if id>0{
c.Data["json"] = map[string]interface{}{"code": 1, "message": "保存数据成功"}
}else{
c.Data["json"] = map[string]interface{}{"code": 0, "message": "保存数据失败"}
}
c.ServeJSON()
return
}
//读取数据
func (c *ApiController) Read() {
id:=c.Input().Get("id")
if id==""||id=="null"{
c.Data["json"] = map[string]interface{}{"code": 1, "message": "没有传递参数id"}
c.ServeJSON()
return
}
temp,_:=strconv.ParseInt(id,10,64)
orm:=orm.NewOrm()
fmt.Println(orm)
u := models.Test{Id:temp}
err:= orm.Read(&u)
if err==nil{
c.Data["json"] = map[string]interface{}{"code": 0, "message": "查询成功","data":u}
}else{
c.Data["json"] = map[string]interface{}{"code": 1, "message": "查询失败","data":u}
}
defer func() {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "err","data":u}
c.ServeJSON()
return
}()
c.ServeJSON()
return
}
//更新数据
func (c *ApiController) Update() {
id:=c.Input().Get("id")
if id==""||id=="null"{
c.Data["json"] = map[string]interface{}{"code": 1, "message": "没有传递参数id"}
c.ServeJSON()
return
}
temp,_:=strconv.ParseInt(id,10,64)
orm:=orm.NewOrm()
fmt.Println(orm)
u := models.Test{Id:temp}
err:= orm.Read(&u)
if err==nil{
u.Content=string(rand.Intn(100))+"zq2019"
if _, err := orm.Update(&u); err == nil {
c.Data["json"] = map[string]interface{}{"code": 0, "message": "更新成功"}
}else{
c.Data["json"] = map[string]interface{}{"code": 0, "message": "更新失败"}
}
}else{
c.Data["json"] = map[string]interface{}{"code": 1, "message": "查询失败","data":u}
}
defer func() {
c.Data["json"] = map[string]interface{}{"code": 1, "message": "err","data":u}
c.ServeJSON()
return
}()
c.ServeJSON()
return
}
//删除数据
func (c *ApiController) Del() {
id:=c.Input().Get("id")
if id==""||id=="null"{
c.Data["json"] = map[string]interface{}{"code": 1, "message": "没有传递参数id"}
c.ServeJSON()
return
}
temp,_:=strconv.ParseInt(id,10,64)
orm:= orm.NewOrm()
if num, err := orm.Delete(&models.Test{Id: temp}); err == nil {
fmt.Println(num)
}
c.Data["json"] = map[string]interface{}{"code": 1, "message": "删除成功"}
c.ServeJSON()
return
}
温馨提醒:使用Orm连接本地mysql驱动
orm.RegisterDataBase(“default”, “mysql”, “root:root@/test?charset=utf8”)
连接远程数据库
orm.RegisterDataBase(“default”, “mysql”, “root:root@tcp(39.139.91.456:3306)/test?charset=utf8”)